In the development of the project, some data needs to be encrypted and the server verifies that the request is made by our client!
Scenarios are server-determined, using HmacSHA1 encryption and MD5 BASE64 encryption
The encryption process is more complex
1. Get GMT
2. Use base64 encoding to calculate the MD5 value of the requested data content
3. Set the request format
4. Set the authentication information, need to HMacSHA1 the above 3 content and the request link to encrypt again
After encryption, the above 4 steps obtained by adding to the HTTP request header, a concurrent to the server, the server has been verified before returning to us the information we want
The following code shows the two encryption we use
First is base MD5 encryption
1 //BASE MD5 Encryption2 3+ (NSString *) getmd5_32bit_string: (NSString *) srcstring{4 Const Char*cstr =[srcstring utf8string];5UnsignedCharresult[ -];6 cc_md5 (CStr, strlen (CSTR), result);7NSData *data =[NSData Datawithbytes:result length:cc_md5_digest_length];8data =[GTMBase64 Encodedata:data];9NSString * base64string =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];Ten returnbase64string; One}
Followed by HmacSHA1 encryption
1 //HmacSHA1 Encryption2+ (NSString *) BASE_HMACSHA1: (NSString *) key data: (NSString *) data{3 Const Char*ckey =[key cstringusingencoding:nsasciistringencoding];4 Const Char*cdata =[Data cstringusingencoding:nsasciistringencoding];5 //Sha256:6 //unsigned char chmac[cc_sha256_digest_length];7 //Cchmac (kCCHmacAlgSHA256, Ckey, strlen (Ckey), CDATA, strlen (CDATA), CHMAC);8 9 //SHA1TenUnsignedCharChmac[cc_sha1_digest_length]; One Cchmac (kCCHmacAlgSHA1, Ckey, strlen (Ckey), CDATA, strlen (CDATA), CHMAC); A -NSData *hmac =[[NSData alloc] Initwithbytes:chmac -Lengthsizeof(Chmac)]; the - //The encryption results are BASE64 encoded at once. -NSString *hash = [HMAC base64encodedstringwithoptions:0]; - returnHash; +}
Base64 need to use a library: GTMBase64
HmacSHA1 encryption needs to use the library is: Commoncryptor
IOS HmacSHA1 Encryption and MD5 BASE64 encryption--ios Development Series---the knowledge of growing up in the project five