Code Bye

关于md5加密,使用一个数组作为密钥,有java的代码,但是没oc版的,求oc版的代码

java的代码


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

md5 后你再仿照他的java 代码做位运算就行了。

现在就是不懂如何做java代码中对应的位运算呢,oc中没有>>>这个运算符,大大求帮忙

5分
引用 2 楼 f6523312 的回复:

现在就是不懂如何做java代码中对应的位运算呢,oc中没有>>>这个运算符,大大求帮忙

C语言中有>>呀,还是找个C语言版本的md5加密吧,oc中应该可以直接调用才对。

本帖最后由 zhangao0086 于 2015-03-12 10:45:18 编辑

我写个伪代码给你:

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没有<<<运算符,我是自己把高位补零的。
还是多说一句:md5不是加密


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于md5加密,使用一个数组作为密钥,有java的代码,但是没oc版的,求oc版的代码