banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

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

- (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

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.