我正在做文件上传工作。我想生成 SHA256 和 CRC32 哈希值。任何人都可以帮助我如何生成这些哈希值?我想让它适用于 iOS。
最佳答案
SHA256 在 CommonCrypto 中可用。 CRC32 不是哈希,它是循环冗余校验。
示例代码:
#import <CommonCrypto/CommonDigest.h>
NSData *dataIn = [@"Now is the time for all good computers to come to the aid of their masters." dataUsingEncoding:NSASCIIStringEncoding];
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
CC_SHA256(dataIn.bytes, dataIn.length, macOut.mutableBytes);
NSLog(@"dataIn: %@", dataIn);
NSLog(@"macOut: %@", macOut);
NSLog输出:
dataIn: <4e6f7720 69732074 68652074 696d6520 666f7220 616c6c20 676f6f64 20636f6d 70757465 72732074 6f20636f 6d652074 6f207468 65206169 64206f66 20746865 6972206d 61737465 72732e>
macOut:<53f89cf6 7ebfbe56 89f1f76a 3843dfd1 09d68c5b a938dcd2 9a12004e 108260cb>
关于iphone - 如何在 ios 中生成 SHA256 和 CRC32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9444215/