#import <Foundation/Foundation.h> @interface nsstring (Hash) @property (readonly) nsstring *md5string; @property ( readonly) NSString *sha1string; @property (readonly) nsstring *sha256string; @property (readonly) NSString *sha512string ;-(NSString *) Hmacsha1stringwithkey: (NSString *) key;-(NSString *) Hmacsha256stringwithkey: (NSString *) key;-( NSString *) Hmacsha512stringwithkey: (NSString *) key;-(NSString *) escapedurlstring; @end
#import "NSSTRING+HASH.H" #import <CommonCrypto/CommonDigest.h> #import <commoncrypto/commonhmac.h>@ Implementation NSString (Hash)-(NSString *) md5string{const char *string = self. Utf8string;int length = (int) strlen (string); unsigned char bytes[cc_md5_digest_length]; CC_MD5 (string, length, bytes); return [self stringfrombytes:bytes length:cc_md5_digest_length];} -(NSString *) sha1string{const char *string = self. Utf8string;int length = (int) strlen (string); unsigned char bytes[cc_sha1_digest_length]; CC_SHA1 (string, length, bytes); return [self stringfrombytes:bytes length:cc_sha1_digest_length];} -(NSString *) sha256string{const char *string = self. Utf8string;int length = (int) strlen (string); unsigned char bytes[cc_sha256_digest_length]; cc_sha256 (string, length, bytes); return [self stringfrombytes:bytes length:cc_sha256_digest_length];} -(NSString *) sha512string{const char *string = self. Utf8string;int length = (int) strlen (string); unsigned char bytes[cc_sha512_digest_length]; cc_sha512(string, length, bytes); return [self stringfrombytes:bytes length:cc_sha512_digest_length];} -(NSString *) Hmacsha1stringwithkey: (NSString *) key{nsdata *keydata = [key datausingencoding:nsutf8stringencoding]; NSData *messagedata = [self datausingencoding:nsutf8stringencoding]; Nsmutabledata *mutabledata = [Nsmutabledata datawithlength:cc_sha1_digest_length]; Cchmac (kCCHmacAlgSHA1, Keydata.bytes, Keydata.length, Messagedata.bytes, Messagedata.length, mutabledata.mutablebytes); return [self stringfrombytes: (unsigned char *) mutabledata.bytes length: (int) Mutabledata.length];} -(NSString *) Hmacsha256stringwithkey: (NSString *) key{nsdata *keydata = [key datausingencoding:nsutf8stringencoding]; NSData *messagedata = [self datausingencoding:nsutf8stringencoding]; Nsmutabledata *mutabledata = [Nsmutabledata datawithlength:cc_sha256_digest_length]; Cchmac (kCCHmacAlgSHA256, Keydata.bytes, Keydata.length, Messagedata.bytes, Messagedata.length, mutabledata.mutablebytes); return [self stringfrombytes: (unsigned char *) mutabledata.bytes length: (int) mutabledata.length];} -(NSString *) Hmacsha512stringwithkey: (NSString *) key{nsdata *keydata = [key datausingencoding:nsutf8stringencoding]; NSData *messagedata = [self datausingencoding:nsutf8stringencoding]; Nsmutabledata *mutabledata = [Nsmutabledata datawithlength:cc_sha512_digest_length]; Cchmac (kCCHmacAlgSHA512, Keydata.bytes, Keydata.length, Messagedata.bytes, Messagedata.length, mutabledata.mutablebytes); return [self stringfrombytes: (unsigned char *) mutabledata.bytes length: (int) Mutabledata.length];} #pragma mark-helpers-(NSString *) Stringfrombytes: (unsigned char *) bytes length: (int) length{nsmutablestring * mutablestring = @ "". mutablecopy;for (int i = 0; i < length; i++) [Mutablestring appendformat:@ "%02x", Bytes[i]];return [ NSString stringwithstring:mutablestring];} -(NSString *) escapedurlstring{nsstring *ret = self; Char *src = (char *) [self utf8string]; if (NULL! = src) {nsmutablestring *tmp = [nsmutablestring String]; int ind = 0; while (Ind < strlen (SRC))//Note:if src is NULL, strlen () would crash. {if (Isalpha (Src[ind]) | | isnumber (Src[ind])) {[TMP appendformat:@ '%c ', src[ind++]]; } else {[tmp appendformat:@ "%%%x", (unsigned char) src[ind++]]; }} RET = tmp; } return ret;} @end
Object-c MD5 and SHA and Hmac-sha