Md5/sha1/hmac_sha1

Source: Internet
Author: User
Tags base64 cdata hmac md5 sha1

1, MD5

#import <CommonCrypto/CommonDigest.h>
+ (NSString *) MD5: (NSString *) Input {Const Char*cstr =[input utf8string]; unsignedCharDigest[cc_md5_digest_length]; CC_MD5 (CSTR, (unsignedint) strlen (CSTR), Digest);//The MD5 callnsmutablestring*output = [nsmutablestring stringwithcapacity:cc_md5_digest_length *2];  for(inti =0; i < cc_md5_digest_length; i++) [Output AppendFormat:@"%02x", Digest[i]]; returnoutput;}

2, SHA1

+ (NSString *) SHA1: (NSString *) input{NSData*data =[input datausingencoding:nsutf8stringencoding];    uint8_t Digest[cc_sha1_digest_length]; CC_SHA1 (Data.bytes, (unsignedint) Data.length, Digest); Nsmutablestring*output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length *2];  for(intI=0; i<cc_sha1_digest_length; i++) {[Output AppendFormat:@"%02x", Digest[i]]; }    returnoutput;}

3, HmacSHA1

Additional Base64 transcoding added
+ (NSString *) BASE_HMACSHA1: (NSString *) key data: (NSString *) data{
const char *ckey = [key cstringusingencoding:nsasciistringencoding];
const char *cdata = [data cstringusingencoding:nsasciistringencoding];
SHA256:
unsigned char chmac[cc_sha256_digest_length];
Cchmac (kCCHmacAlgSHA256, Ckey, strlen (Ckey), CDATA, strlen (CDATA), CHMAC);

Sha1
unsigned char chmac[cc_sha1_digest_length];
Cchmac (kCCHmacAlgSHA1, Ckey, strlen (Ckey), CDATA, strlen (CDATA), CHMAC);

NSData *hmac = [[NSData alloc] Initwithbytes:chmac length:sizeof (CHMAC)];

The encryption results are BASE64 encoded at once.
NSString *hash = [HMAC base64encodedstringwithoptions:0];
return hash;
}

Md5/sha1/hmac_sha1

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.