码迷,mamicode.com
首页 > 移动开发 > 详细

iOS SHA加密算法的实现

时间:2018-06-26 10:55:33      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:pre   div   tab   nss   form   aci   int   ++   tput   

- (NSString *)SHAStringWithSourceData:(NSData *)data type:(SHAType)type{
    
    int shaDigestLength;
    switch (type) {
        case SHAType_sha1: shaDigestLength = CC_SHA1_DIGEST_LENGTH; break;
        case SHAType_sha224: shaDigestLength = CC_SHA224_DIGEST_LENGTH; break;
        case SHAType_sha256: shaDigestLength = CC_SHA256_DIGEST_LENGTH; break;
        case SHAType_sha384: shaDigestLength = CC_SHA384_DIGEST_LENGTH; break;
        case SHAType_sha512: shaDigestLength = CC_SHA512_DIGEST_LENGTH; break;
        default: break;
    }
    
    uint8_t digest[shaDigestLength];
    switch (type) {
        case SHAType_sha1: CC_SHA1(data.bytes, (CC_LONG)data.length, digest); break;
        case SHAType_sha224: CC_SHA224(data.bytes, (CC_LONG)data.length, digest); break;
        case SHAType_sha256: CC_SHA256(data.bytes, (CC_LONG)data.length, digest); break;
        case SHAType_sha384: CC_SHA384(data.bytes, (CC_LONG)data.length, digest); break;
        case SHAType_sha512: CC_SHA512(data.bytes, (CC_LONG)data.length, digest); break;
        default: break;
    }
    
    NSMutableString* output = [NSMutableString stringWithCapacity:shaDigestLength * 2];
    for(int i = 0; i < shaDigestLength; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
}

  

iOS SHA加密算法的实现

标签:pre   div   tab   nss   form   aci   int   ++   tput   

原文地址:https://www.cnblogs.com/yangzigege/p/9227184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!