获取文件的 md5/sha1 校验和
我在 Objective C 中找到了两个计算 md5 和 sha 1 的函数。代码如下:
-(void)md5HexDigest:(NSString*)input {
NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
uint8_t digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(data.bytes, data.length, digest);
NSMutableString* ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",digest[i]];
}
NSLog (@"%@",ret);
}
-(void) SHA1digest:(NSString*)input{
NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH *2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
NSLog (@"%@", output);
}
我得到了这些校验和:
2010-11-04 20:38:01.962 MD5 Counter[88118:a0f] c8142be71e8ed4625c4f27eb573835f5
2010-11-04 20:38:01.964 MD5 Counter[88118:a0f] ba7ff5f68edef52dd89a92c075b88f247f3ef9aa
但是,真正的总和是: SHA1: 1c0d5ea45464e336fcb38c644dc125c3a16b5493
MD5: e8f4d590c8fe62386844d6a2248ae609
错误在哪里?请帮助我!
I have found two functions to count md5 and sha 1 in Objective C. Here's the code:
-(void)md5HexDigest:(NSString*)input {
NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
uint8_t digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(data.bytes, data.length, digest);
NSMutableString* ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",digest[i]];
}
NSLog (@"%@",ret);
}
-(void) SHA1digest:(NSString*)input{
NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH *2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
NSLog (@"%@", output);
}
I get these checksums:
2010-11-04 20:38:01.962 MD5 Counter[88118:a0f] c8142be71e8ed4625c4f27eb573835f5
2010-11-04 20:38:01.964 MD5 Counter[88118:a0f] ba7ff5f68edef52dd89a92c075b88f247f3ef9aa
However, the real sums are:
SHA1: 1c0d5ea45464e336fcb38c644dc125c3a16b5493
MD5: e8f4d590c8fe62386844d6a2248ae609
Where is the mistake? Help me, please!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 CommonCrypto C API。这些函数记录在 手册页。特别是 CC_md5和 CC_sha1 函数系列您将会感兴趣。
You should use the CommonCrypto C API. The functions are documented in section 3CC of the man pages. In particular the CC_md5 and CC_sha1 function families will be of interest to you.
我不确定 Mac AppStore 的限制是什么,但您也许可以调用
md5
命令。它默认安装在 OSX 上,并计算作为参数给出的文件的 MD5 校验和。I'm not sure what the Mac AppStore restrictions are, but you might be able to call out the
md5
command. It's is installed by default on OSX and computes the MD5 checksum of the file given as an argument.