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 MD_DIGEST_LENGTH ibr>
        [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 SHA_DIGEST_LENGTH ibr>
        [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 SHA_DIGEST_LENGTH ibr>
        [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 MD_DIGEST_LENGTH ibr>
        [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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值