35分 |
md5 是数据指纹,不是加密。
@implementation NSString (MyAdditions) - (NSString *)md5 { const char *cStr = [self UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call 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] ]; } @end @implementation NSData (MyAdditions) - (NSString*)md5 { unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5( self.bytes, self.length, result ); // This is the md5 call 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] ]; } @end |
现在就是不懂如何做java代码中对应的位运算呢,oc中没有>>>这个运算符,大大求帮忙
|
|
5分 |
C语言中有>>呀,还是找个C语言版本的md5加密吧,oc中应该可以直接调用才对。 |
我写个伪代码给你: char hexDigits[] = {""0"",""1"",""2"",""3"",""4"",""5"",""6"",""7"",""8"",""9"",""a"",""b"",""c"",""d"",""e"",""f""}; NSString *md5 = @"md5"; const char *md = [md5 UTF8String]; NSUInteger j = md5.length; char str[j * 2]; NSUInteger k = 0; for (int i = 0; i < j; i++) { Byte byte0 = md[i]; str[k++] = (char)hexDigits[byte0 >> 4 & 0x7FFFFFFF & 0xf]; str[k++] = (char)hexDigits[byte0 & 0xf]; } 由于OC没有<<<运算符,我是自己把高位补零的。 |