IOS HmacSHA1 Encryption and MD5 BASE64 encryption--ios Development Series---the knowledge of growing up in the project five

Source: Internet
Author: User
Tags cdata hmac md5 encryption

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.