在可可中的字符串上使用MD5哈希? [英] Using MD5 hash on a string in cocoa?

查看:166
本文介绍了在可可中的字符串上使用MD5哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

目标C中的MD5算法

字符串使用MD5技术在可可。任何使用的框架必须能够在iphone上访问。请尽可能提供代码。

I need to hash a string using the MD5 technique in cocoa. Any frameworks that are used must be able to be accessed on the iphone. please provide code if possible.

推荐答案

首先,MD5不是加密。

Well, first off, MD5 isn't encryption. So if you're looking for encryption, you're looking in the wrong place.

但是如果你只是想在iPhone上使用MD5哈希,那么应该给出您需要的信息:

But if you just want to hash something using MD5 on an iPhone, this should give you the information you need:

#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]
    ];
}

//…

NSString *digest = md5(@"test");
NSLog(@"MD5 TEST %@", digest);

(从在iPhone上计算MD5

这篇关于在可可中的字符串上使用MD5哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆