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: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.h