我正在做文件上传工作。我想生成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:
macout: