OBJECTIVE-C implementation of MD5 and SHA1 algorithm is relatively simple, you can directly call the system's C/s + + shared library to implement the call
MD5 is the message Digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure complete consistency of information transmission. is one of the most widely used hashing algorithms in computers
The SHA, Secure Hash algorithm (safe hashing algorithm) is a series of cryptographic hash functions issued by the National Institute of Standards and Technology (NIST), a design of the U.S. State Security Agency (NSA).
The use of the following methods:
MD5 Encryption method
code is as follows |
copy code |
- (NSString *) MD5 { const char *CSTR = [self utf8string]; unsigned char D Igest[cc_md5_digest_length]; cc_md5 (CStr, strlen (CStr), Digest); nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2]; for (int i = 0; i < cc_md5_digest_length; i++) &nb sp; [Output appendformat:@ "%02x", Digest[i]]; return output; } |
SHA1 Encryption method
The code is as follows |
Copy Code |
-(nsstring*) SHA1 { const char *CSTR = [self cstringusingencoding:nsutf8stringencoding]; NSData *data = [NSData datawithbytes:cstr length:self.length];
uint8_t Digest[cc_sha1_digest_length];
CC_SHA1 (Data.bytes, Data.length, Digest);
nsmutablestring* output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2];
for (int i = 0; i < cc_sha1_digest_length; i++) [Output appendformat:@ "%02x", Digest[i]];
return output; }
|
Of course can also be combined with BASE64 to use, where the BASE64 encoding using GTMBASE64 implementation, need to import
The code is as follows |
Copy Code |
-(NSString *) sha1_base64 { const char *CSTR = [self cstringusingencoding:nsutf8stringencoding]; NSData *data = [NSData datawithbytes:cstr length:self.length];
uint8_t Digest[cc_sha1_digest_length];
CC_SHA1 (Data.bytes, Data.length, Digest);
NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length]; Base64 = [GTMBase64 encodedata:base64];
NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding]; return output; }
-(NSString *) md5_base64 { const char *CSTR = [self utf8string]; unsigned char digest[cc_md5_digest_length]; CC_MD5 (CStr, strlen (CStr), Digest);
NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length]; Base64 = [GTMBase64 encodedata:base64];
NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding]; return output; } |
By expanding NSString to achieve full functionality, all code
The code is as follows |
Copy Code |
@interface NSString (Encrypto) -(NSString *) MD5; -(NSString *) SHA1; -(NSString *) sha1_base64; -(NSString *) md5_base64; -(NSString *) base64;
@end @implementation NSString (Encrypto) -(nsstring*) SHA1 { const char *CSTR = [self cstringusingencoding:nsutf8stringencoding]; NSData *data = [NSData datawithbytes:cstr length:self.length];
uint8_t Digest[cc_sha1_digest_length];
CC_SHA1 (Data.bytes, Data.length, Digest);
nsmutablestring* output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2];
for (int i = 0; i < cc_sha1_digest_length; i++) [Output appendformat:@ "%02x", Digest[i]];
return output; }
-(NSString *) MD5 { const char *CSTR = [self utf8string]; unsigned char digest[cc_md5_digest_length]; CC_MD5 (CStr, strlen (CStr), Digest);
nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];
for (int i = 0; i < cc_md5_digest_length; i++) [Output appendformat:@ "%02x", Digest[i]];
return output; }
-(NSString *) sha1_base64 { const char *CSTR = [self cstringusingencoding:nsutf8stringencoding]; NSData *data = [NSData datawithbytes:cstr length:self.length];
uint8_t Digest[cc_sha1_digest_length];
CC_SHA1 (Data.bytes, Data.length, Digest);
NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length]; Base64 = [GTMBase64 encodedata:base64];
NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding]; return output; }
-(NSString *) md5_base64 { const char *CSTR = [self utf8string]; unsigned char digest[cc_md5_digest_length]; CC_MD5 (CStr, strlen (CStr), Digest);
NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length]; Base64 = [GTMBase64 encodedata:base64];
NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding]; return output; }
-(NSString *) base64 { NSData * data = [self datausingencoding:nsasciistringencoding allowlossyconversion:yes]; data = [GTMBase64 encodedata:data]; NSString * output = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return output; } @end |
Don't forget to import a CC-related library header file when implementing
Commoncrypto/commondigest.h