Objective-c 實現 MD5 和 SHA1 算法相對還是比較簡單的,可以直接調用系統的 C/C++ 共享庫來實現調用
MD5 即 Message Digest Algorithm 5(信息 - 摘要算法 5),用於確保信息傳輸完整一致。是計算機廣泛使用的雜湊算法之一
SHA 即 Secure Hash Algorithm(安全散列算法) 是美國國家安全局 (NSA) 設計,美國國家標準與技術研究院 (NIST) 發布的一系列密碼散列函數。
使用方式如下:
MD5 加密方式
-(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];
NSData *data = [NSData dataWithBytes length.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];
NSData *data = [NSData dataWithBytes length.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 length_MD5_DIGEST_LENGTH];
base64 = [GTMBase64 encodeData];NSString * output = [[NSString alloc] initWithData encoding];
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];
NSData *data = [NSData dataWithBytes length.length];uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity_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];
NSData *data = [NSData dataWithBytes length.length];uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSData * base64 = [[NSData alloc]initWithBytes length_SHA1_DIGEST_LENGTH];
base64 = [GTMBase64 encodeData];NSString * output = [[NSString alloc] initWithData encoding];
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 length_MD5_DIGEST_LENGTH];
base64 = [GTMBase64 encodeData];NSString * output = [[NSString alloc] initWithData encoding];
return output;
} -
(NSString *) base64
{
NSData * data = [self dataUsingEncoding allowLossyConversion];
data = [GTMBase64 encodeData];
NSString * output = [[NSString alloc] initWithData encoding];
return output;
}
@end
實現時不要忘記導入 CC 相關的庫的頭文件
CommonCrypto/CommonDigest.h