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

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

问题描述

可能的重复:
Objective 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天全站免登陆