I'd like to use gravatar in my iPhone application. Is there anyway to generate a hexadecimal MD5 hash in Objective-C for iPhone? Using openssl on iPhone is a no-go.
我想在我的iPhone应用程序中使用gravatar。无论如何,在Objective-C中为iPhone生成十六进制MD5哈希?在iPhone上使用openssl是不行的。
2 个解决方案
#1
This is how I did it before I removed it from my app:
这是我在从应用程序中删除它之前的方法:
#import <CommonCrypto/CommonDigest.h>
NSString* md5( NSString *str ) {
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
return [[NSString
stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1],
result[2], result[3],
result[4], result[5],
result[6], result[7],
result[8], result[9],
result[10], result[11],
result[12], result[13],
result[14], result[15]
] lowercaseString];
}
It's only fair to add that I didn't write this myself. I found it somewhere on the internet but I didn't record where.
添加我自己没有写这个是公平的。我发现它在互联网上的某个地方,但我没有记录在哪里。
#2
The code I used for generating the necessary MD5 hash is up on my github repository, in the CommonCrypto subfolder. There are a bunch of similar routines in there which will either show you how to use CommonCrypto or how to format strings of hex byte values, base-64, etc.
我用于生成必要的MD5哈希的代码在我的github存储库中,在CommonCrypto子文件夹中。那里有许多类似的例程,它们将向您展示如何使用CommonCrypto或如何格式化十六进制字节值的字符串,base-64等。
A potentially better way of generating the string would be:
生成字符串的一种更好的方法是:
NSMutableString * str = [[NSMutableString alloc] initWithCapacity: 33];
int i;
for ( i = 0; i < 16; i++ )
{
[str appendFormat: @"%02x", result[i]];
}
NSString * output = [str copy];
[str release];
return ( [output autorelease] );
If you're going to use the code in the answer above, however, I'd personally suggest changing the %02X's to %02x and forgoing the -lowercaseString call completely-- might as well generate the hex values lowercase to start with.
但是,如果您要使用上面答案中的代码,我个人建议将%02X更改为%02x并完全放弃-lowercaseString调用 - 也可以生成小写的十六进制值。
#1
This is how I did it before I removed it from my app:
这是我在从应用程序中删除它之前的方法:
#import <CommonCrypto/CommonDigest.h>
NSString* md5( NSString *str ) {
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
return [[NSString
stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1],
result[2], result[3],
result[4], result[5],
result[6], result[7],
result[8], result[9],
result[10], result[11],
result[12], result[13],
result[14], result[15]
] lowercaseString];
}
It's only fair to add that I didn't write this myself. I found it somewhere on the internet but I didn't record where.
添加我自己没有写这个是公平的。我发现它在互联网上的某个地方,但我没有记录在哪里。
#2
The code I used for generating the necessary MD5 hash is up on my github repository, in the CommonCrypto subfolder. There are a bunch of similar routines in there which will either show you how to use CommonCrypto or how to format strings of hex byte values, base-64, etc.
我用于生成必要的MD5哈希的代码在我的github存储库中,在CommonCrypto子文件夹中。那里有许多类似的例程,它们将向您展示如何使用CommonCrypto或如何格式化十六进制字节值的字符串,base-64等。
A potentially better way of generating the string would be:
生成字符串的一种更好的方法是:
NSMutableString * str = [[NSMutableString alloc] initWithCapacity: 33];
int i;
for ( i = 0; i < 16; i++ )
{
[str appendFormat: @"%02x", result[i]];
}
NSString * output = [str copy];
[str release];
return ( [output autorelease] );
If you're going to use the code in the answer above, however, I'd personally suggest changing the %02X's to %02x and forgoing the -lowercaseString call completely-- might as well generate the hex values lowercase to start with.
但是,如果您要使用上面答案中的代码,我个人建议将%02X更改为%02x并完全放弃-lowercaseString调用 - 也可以生成小写的十六进制值。