2011-11-28 119 views
1

我的iPhone代碼donot生成與Ruby on Rails生成的相同的十六進制字符串。iphone md5生成由Ruby on Rails生成的不同字符串

我的Ruby代碼:

hash = Digest::MD5.digest('aaa') 
hexMd5FromRuby = Digest::MD5.hexdigest(hash) 

iPhone代碼:

NSString *inStr = @"aaa"; 
const char *cStr = [inStr UTF8String]; 
unsigned char result[CC_MD5_DIGEST_LENGTH]; 
CC_MD5(cStr, strlen(cStr), result); 

NSString *hexMd5FromIphone = [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] ]; 

兩個hexMd5FromRuby和hexMd5FromIphone產生不同的結果。

回答

2

正確的MD5是47bce5c74f589f4867dbd57e9ca9f808。哪一個是錯的,Ruby一個?我不知道Ruby,但是看起來好像在你的例子中計算哈希兩次。此代碼的工作對我來說:

require 'digest/md5' 
digest = Digest::MD5.hexdigest("aaa") 
puts digest # 47bce5c74f589f4867dbd57e9ca9f808 

你的代碼似乎得到字符串的二進制摘要,然後從第一步計算六角MD5摘要的二進制數據。