1, new nsstring category, remember to add the header file required for encryption #import <CommonCrypto/CommonDigest.h>, Nsstring+md5.h:
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
@interface nsstring ( MD5)
+ (NSString *) Md5to32bit: (NSString *) str;
@end
2, write the encryption method:
#import "Nsstring+md5.h"
@implementation nsstring (MD5)
+ (NSString *) Md5to32bit: (NSString *) str {
const char *cstr = [str utf8string];
unsigned char digest[cc_md5_digest_length];
CC_MD5 (CStr, strlen (CStr), digest);
nsmutablestring *result = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];
for (int i = 0; i < cc_md5_digest_length i++)
[result appendformat:@ "%02x", Digest[i]];
return result;
}
3, import the header file into the current controller of the string character that needs to be encrypted, and reference the method:
#import "ViewController.h"
#import "nsstring+md5.h"
@interface viewcontroller ()
@end
@ Implementation Viewcontroller
-(void) viewdidload {
[super viewdidload];
NSString *name = @ "Kuture";
Encrypted string
nsstring *encypt = [NSString md5to32bit:name];
NSLog (@ \ Name:%@\n encypt:%@ ", name,encypt);
}
@end
The results of the operation are:
The above is the entire content of this article, I hope you can enjoy.