在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过今天的多次尝试,我终于想出了一个获取 SHA256 的函数:
这给出了与 PHP 相同的输出。它可以轻松转换为 SHA1 - 只需将“SHA256”更改为“SHA1”即可。
希望它能帮助某人。
After much playing around today, I finally came up with a function to get the SHA256:
This gives the same output as PHP. It can easily be converted to SHA1 - just change 'SHA256' to 'SHA1'.
Hope it helps someone.
您正在将
result
传递到UIAlertView
的init
方法中。result
是一个char[]
,而UIAlertView
需要一个NSString*
。您需要将char[]
转换为NSString *
。试试这个:
另请参阅这篇有关哈希的文章在 iPhone 上。
You are passing
result
into theUIAlertView
'sinit
method.result
is achar[]
, andUIAlertView
expects anNSString*
. You need to convert yourchar[]
to anNSString *
.Try this:
Also see this article on hashing on the iPhone.
您将需要使用 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]
.