iphone - 如何在 ios 中生成 SHA256 和 CRC32

标签 iphone ios sha

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

相关文章:

ios - 恢复功能在 NSTime 中无法正常工作

algorithm - 单个 "1"位的 SHA-256 哈希值是多少?

iphone - iPad界面方向问题

ios - UILabel 按下时会使应用程序崩溃

ios - 应用内购买框架的问题(体系结构 x86_64 的 undefined symbol )

iOS - 自己的 URL Scheme 并测试它

c# - 6字符短哈希算法

security - 更改预先存在的数据库上的散列函数

iphone - 可以像iPhone中的 "unlock slider"一样自定义UISlider吗?

iphone - splitview 委托(delegate)方法没有被调用