博客> MD5/SHA1加密算法的实现
MD5/SHA1加密算法的实现
2019-07-15 17:55 评论:0 阅读:1121 1498893132
ios
Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用  
MD5即Message Digest Algorithm 5(信息-摘要算法 5),用于确保信息传输完整一致。是计算机广泛使用的杂凑算法之一  
SHA即Secure Hash Algorithm(安全散列算法) 是美国国家安全局 (NSA) 设计,美国国家标准与技术研究院 (NIST) 发布的一系列密码散列函数。  

使用方式如下:  
MD5加密方式  

SString *) md5  
{  
    const charchar *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:@"x", digest[i]];  

    return output;  
}  
SHA1加密方式  

- (NSString*) sha1  
{  
    const charchar *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:@"x", digest[i]];  

    return output;  
}  
当然也可以结合BASE64来使用,这里的BASE64编码使用 GTMBase64实现,需要导入  

- (NSString *) sha1_base64  
{  
    const charchar *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 encode[removed] strlen(cStr), digest );  

    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];  
    base64 = [GTMBase64 encode[removed] data.length, digest);  

    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];  

    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)  
        [output appendFormat:@"x", digest[i]];  

    return output;  
}  

-(NSString *) md5  
{  
    const charchar *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:@"x", digest[i]];  

    return output;  
}  

- (NSString *) sha1_base64  
{  
    const charchar *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 encode[removed] strlen(cStr), digest );  

    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];  
    base64 = [GTMBase64 encode[removed] data.length, digest);

NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
    [output appendFormat:@"x", digest[i]];
}

return output;

}

收藏
1
sina weixin mail 回到顶部