对字符串进行SHA224/SHA256/SHA384/SHA512编码

iOS 对NSString进行扩展(Extention),使其S

2018-09-28  本文已影响50人  努力奔跑的小男孩

首先要引入头文件
需要 .m文件 引入头文件

#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>

SHA224 代码

- (NSString *)SHA224{
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];// 注意这里的编码格式
    NSData *data=[NSData dataWithBytes:cstr length:self.length];
    uint8_t digest[CC_SHA224_DIGEST_LENGTH];
    CC_SHA224(data.bytes, (CC_LONG)data.length, digest);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA224_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA224_DIGEST_LENGTH; i++){
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

调用示例:

NSString *str = @"jljdfajfjdsjfajfjjlsdaj";
NSString *result = [self SHA224:str0];
NSLog(@"\nresult0--%@",result0);

SHA256代码

- (NSString *)SHA256{
    const char *cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];// 注意这里的编码格式
    NSData *data=[NSData dataWithBytes:cstr length:self.length];
    uint8_t digest[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(data.bytes, (CC_LONG)data.length, digest);
    NSMutableString *output=[NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
    for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++){
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

调用示例:

NSString *str = @"jljdfajfjdsjfajfjjlsdaj";
NSString *result = [self SHA256:str0];
NSLog(@"\nresult0--%@",result0);

SHA384代码

- (NSString*) SHA384{
    const char *cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];// 注意这里的编码格式
    NSData *data=[NSData dataWithBytes:cstr length:self.length];
    uint8_t digest[CC_SHA384_DIGEST_LENGTH];
    CC_SHA384(data.bytes, (CC_LONG)data.length, digest);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA384_DIGEST_LENGTH*2];
    for(int i = 0; i < CC_SHA384_DIGEST_LENGTH; i++){
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

调用示例:

NSString *str = @"jljdfajfjdsjfajfjjlsdaj";
NSString *result = [self SHA384:str0];
NSLog(@"\nresult0--%@",result0);

SHA512代码

- (NSString*)SHA512{
    const char *cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];// 注意这里的编码格式
    NSData *data=[NSData dataWithBytes:cstr length:self.length];
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512(data.bytes, (CC_LONG)data.length, digest);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH*2];
    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++){
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

调用示例:

NSString *str = @"jljdfajfjdsjfajfjjlsdaj";
NSString *result = [self SHA512:str0];
NSLog(@"\nresult0--%@",result0);

附:

SHA256的另外一种实现方式,但是当NSString *str = @"我是字符串"; 调用时,就会报错,所以推荐使用上面↑的实现方式.

- (NSString *)SHA256{
    const char *s = [self cStringUsingEncoding:NSASCIIStringEncoding]; // 注意这里的编码格式
    NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
    
    uint8_t digest[CC_SHA256_DIGEST_LENGTH] = {0};
    CC_SHA256(keyData.bytes, (CC_LONG)keyData.length, digest);
    NSData *out = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    NSString *hash = [out description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
    return hash;
}

调用示例:

NSString *str = @"jljdfajfjdsjfajfjjlsdaj";
//NSString *str = @"我是字符串";// 调用会报错
NSString *result = [self SHA256:str0];
NSLog(@"\nresult0--%@",result0);
上一篇下一篇

猜你喜欢

热点阅读