在 Objective C 中生成 SHA256 字符串

发布于 2024-10-17 05:21:28 字数 960 浏览 4 评论 0原文

可能的重复:
iPhone 版 Objective-C 中的 Sha256

你好,

我在生成时遇到了严重的问题Objective C 中的 SHA256 字符串(可能是因为我对这门语言很陌生)。

在 jQuery 中,我所要做的就是:

var sha256String=$.sha256("Hello");

它会按预期生成哈希值。

但在 Objective-C 中,我尝试了以下方法但没有成功:

NSString *pword=[[NSString alloc]
initWithString:login_pword.text];
unsigned char result[64];
CC_SHA256([pword UTF8String], [pword lengthOfBytesUsingEncoding:NSASCIIStringEncoding],result);
UIAlertView *msg=[[UIAlertView alloc] initWithTitle:@"Hi" message:result delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[msg show];
[msg release];

是否有一些我可以调用的函数,例如:

NSString *sha256String=[self getSHA256:pword];

这就是我正在尝试创建的,但我发现它非常困难!

我希望有人能提供帮助。

预先非常感谢,

Possible Duplicate:
Sha256 in Objective-C for iPhone

Greetings,

I'm having terrible trouble generating a SHA256 string in Objective C (probably because I'm so new to the language).

In jQuery, all I have to do is this:

var sha256String=$.sha256("Hello");

which produces the hash as expected.

But in Objective-C, I've tried the following to no avail:

NSString *pword=[[NSString alloc]
initWithString:login_pword.text];
unsigned char result[64];
CC_SHA256([pword UTF8String], [pword lengthOfBytesUsingEncoding:NSASCIIStringEncoding],result);
UIAlertView *msg=[[UIAlertView alloc] initWithTitle:@"Hi" message:result delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[msg show];
[msg release];

Is there some function that I can call such as:

NSString *sha256String=[self getSHA256:pword];

This is what I'm trying to create and I'm finding it very difficult!

I hope someone can help.

Many thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-10-24 05:21:28

经过今天的多次尝试,我终于想出了一个获取 SHA256 的函数:

-(NSString*) sha256:(NSString *)clear{
    const char *s=[clear cStringUsingEncoding:NSASCIIStringEncoding];
    NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
    CC_SHA256(keyData.bytes, keyData.length, digest);
    NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    NSString *hash=[out description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
    return hash;
}

这​​给出了与 PHP 相同的输出。它可以轻松转换为 SHA1 - 只需将“SHA256”更改为“SHA1”即可。

希望它能帮助某人。

After much playing around today, I finally came up with a function to get the SHA256:

-(NSString*) sha256:(NSString *)clear{
    const char *s=[clear cStringUsingEncoding:NSASCIIStringEncoding];
    NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
    CC_SHA256(keyData.bytes, keyData.length, digest);
    NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    NSString *hash=[out description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
    return hash;
}

This gives the same output as PHP. It can easily be converted to SHA1 - just change 'SHA256' to 'SHA1'.

Hope it helps someone.

固执像三岁 2024-10-24 05:21:28

您正在将 result 传递到 UIAlertViewinit 方法中。 result 是一个 char[],而 UIAlertView 需要一个 NSString*。您需要将 char[] 转换为 NSString *

试试这个:

NSString *resultString = [NSString stringWithCString:result encoding:NSASCIIStringEncoding];
UIAlertView *msg=[[UIAlertView alloc] initWithTitle:@"Hi" message:resultString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

另请参阅这篇有关哈希的文章在 iPhone 上。

You are passing result into the UIAlertView's init method. result is a char[], and UIAlertView expects an NSString*. You need to convert your char[] to an NSString *.

Try this:

NSString *resultString = [NSString stringWithCString:result encoding:NSASCIIStringEncoding];
UIAlertView *msg=[[UIAlertView alloc] initWithTitle:@"Hi" message:resultString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

Also see this article on hashing on the iPhone.

雨巷深深 2024-10-24 05:21:28

您将需要使用 OpenSSL C 函数。有关如何执行此操作的示例,请参阅此问题。作为输入字符串,您可以使用长度为 [myString lengthOfBytesUsingEncoding:NSUTF8StringEncoding][myString UTFString]

You will need to use the OpenSSL C functions. See for example this question on how to do that. As input string, you'd use [myString UTFString] with length [myString lengthOfBytesUsingEncoding:NSUTF8StringEncoding].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文