iOS SHA加密算法的实现
Posted 洋子哥哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS SHA加密算法的实现相关的知识,希望对你有一定的参考价值。
- (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加密算法的实现的主要内容,如果未能解决你的问题,请参考以下文章