我找到了两个函数来计算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
错误在哪里?请帮帮我!