objective-c 在可可中的字符串上使用 MD5 哈希?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/652300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using MD5 hash on a string in cocoa?
提问by zpesk
Possible Duplicate:
MD5 algorithm in Objective C
可能的重复:
Objective C 中的 MD5 算法
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 技术散列一个字符串。使用的任何框架都必须能够在 iphone 上访问。如果可能,请提供代码。
采纳答案by Chad Birch
Well, first off, MD5 isn't encryption. So if you're looking for encryption, you're looking in the wrong place.
嗯,首先,MD5 不是加密。因此,如果您正在寻找加密,那么您找错了地方。
But if you just want to hash something using MD5 on an iPhone, this should give you the information you need:
但是如果你只想在 iPhone 上使用 MD5 散列一些东西,这应该会给你你需要的信息:
#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);
(From Calculate MD5 on iPhone)
(来自在 iPhone 上计算 MD5)
回答by Hymanie Treehorn
Noticed this in the Facebook Connect source code. Looks pretty solid, give it a shot.
在 Facebook Connect 源代码中注意到了这一点。看起来很结实,试一试。
#import <CommonCrypto/CommonDigest.h>
...
+ (NSString*)md5HexDigest:(NSString*)input {
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
...
回答by bentford
This is what I use. Credits go to Alistair McMillan.
这就是我使用的。致谢Alistair McMillan。
#import <CommonCrypto/CommonDigest.h>
+ (NSString *) md5:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[16];
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]
];
}
NOTE #1: I didn't have to link to any libraries
注意 #1:我不必链接到任何库
NOTE #2: I couldn't find -lcrypto in the external framework list on the iphone, and this works without -lcrypto
注意 #2:我在 iphone 的外部框架列表中找不到 -lcrypto,这在没有 -lcrypto 的情况下工作
回答by Tom M
It's worth mentioning that the OpenSSL methods are deprecated on more recent versions of OS X, and the MD5 digest is conventionally lower case. Personally I'm more a fan of the unrolled style for efficiency, and I think using ObjC categories for this is a better fit.
值得一提的是,在更新的 OS X 版本中不推荐使用 OpenSSL 方法,并且 MD5 摘要通常是小写的。就个人而言,我更喜欢展开样式以提高效率,我认为使用 ObjC 类别更适合。
For MD5Digest.h: #include
对于 MD5Digest.h:#include
@interface NSString (MD5Digest)
- (NSString*) md5Digest;
@end
@interface NSData (MD5Digest)
- (NSString*) md5Digest;
@end
And MD5Digest.m:
和 MD5Digest.m:
#include <CommonCrypto/CommonDigest.h>
#include "MD5Digest.h"
static NSString* md5Digest(const void *data, CC_LONG length)
{
unsigned char digest[CC_MD5_DIGEST_LENGTH];
unsigned char* d = CC_MD5(data, length, digest);
return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15],
nil];
}
@implementation NSString (MD5Digest)
- (NSString*) md5Digest
{
return md5Digest([self UTF8String], [self lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
}
@end
@implementation NSData (MD5Digest)
- (NSString*) md5Digest
{
return md5Digest([self bytes], [self length]);
}
@end
回答by MarcWan
I added the following to my "NSString+MyGoonk" category:
我在“NSString+MyGoonk”类别中添加了以下内容:
#include <openssl/md5.h>
- (NSString *)md5
{
NSData *data = [self dataUsingEncoding: NSUTF8StringEncoding];
unsigned char *digest = MD5([data bytes], [data length], NULL);
return [NSString stringWithUTF8String: (char *)digest];
}
Two things:
两件事情:
this assumes your string is UTF8. I'm sure there's a way to make it more generic, but I almost never use anything else.
you have to link -lcrypto into your project.
这假设您的字符串是 UTF8。我确信有一种方法可以使它更通用,但我几乎从不使用其他任何东西。
您必须将 -lcrypto 链接到您的项目中。
回答by Chris Beaven
After spending too much time trying to figure this out I made a comprehensive post with correct code and how to use it. You can find the post here on my blog. http://www.saobart.com/md5-has-in-objective-c/
在花了太多时间试图弄清楚这一点之后,我用正确的代码和如何使用它做了一个全面的帖子。你可以在我的博客上找到这篇文章。http://www.saobart.com/md5-has-in-objective-c/
回答by Adam Rosenfield
MD5 is not encryption, it is a cryptographic hash function. It's a one-way function whose output is a 128-bit number. The fact that it is cryptographicmeans that it is a computationally hard problem that, given an MD5 hash output, compute a string whose MD5 is that value. So, MD5 can be used for data integrity checks, but not for encryption.
MD5 不是加密,它是一种加密散列函数。这是一个单向函数,其输出是一个 128 位数字。它是加密的这一事实意味着这是一个计算上的难题,给定 MD5 哈希输出,计算其 MD5 是该值的字符串。因此,MD5 可用于数据完整性检查,但不能用于加密。

