-(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; } |
SHA1加密方式html
- (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; } |
固然也能夠結合BASE64來使用,這裏的BASE64編碼使用 GTMBase64實現,須要導入算法
- (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,實現完整功能,所有代碼安全
@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 |
實現時候不要忘記導入CC相關的庫的頭文件
CommonCrypto/CommonDigest.happ