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