iOS開發之Objective-c的MD5/SHA1加密算法的實現

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加密方式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

相關文章
相關標籤/搜索