在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
介绍: Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用 链接:http://m.111cn.net/art-53370.htm 使用方式如下: -(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加密方式 - (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实现,需要导入 //结合base64的sha1加密 - (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; } //结合base64的md5加密 - (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 注意: 1.实现时候不要忘记导入CC相关的库的头文件 2.在实现时还需要依赖这三个文件:GTMBase64.h GTMBase64.m GTMDefines.h 这三个文件的链接: 第一个:http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87 第二个: gtmbase64.zip
例子二:通过类扩展NSString,实现完整功能,全部代码 .h文件 #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; @end .m文件 // // NSString+Hash.m // // Created by Tom Corwine on 5/30/12. // #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: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: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: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]; } @end |
请发表评论