2013-05-07 34 views
0

我有一個NSData的值,我通過使用以下代碼我如何使用MD5散列字節數組中的值?

Byte *byteData = (Byte*)malloc(24); //byte array 
    int len=[result length]; //find the length 
    memcpy(byteData, [result bytes], len); // copy the bytes values of "result" to bytes array 

    NSString *MD5Key=[[NSString alloc]initWithFormat:@"%@",[result md5]]; // find md5 values 
    NSLog(@"%@",MD5Key); 
    NSString *finalKey=[MD5Key substringToIndex:8]; 

發現值的字節,但我需要字節陣列(byteData)的MD5值?我怎樣才能做到這一點 ?

回答

0

我發現這種方法,並希望你可以改變它爲你的目標。

- (NSString *) md5_forString: (NSString*) sourceString 
{ 
    const char *cStr = [sourceString UTF8String]; 
    unsigned char result[16]; 
    CC_MD5(cStr, (unsigned int) 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] 
      ];  
}