关于 CC_SHA256 Objective-c 的一些信息 [英] Some info about CC_SHA256 objective-c

查看:97
本文介绍了关于 CC_SHA256 Objective-c 的一些信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个新项目,我需要使用 SHA256 散列一个 NSString.我使用了以下代码:

For a new project I need to hash a NSString with SHA256. I have used the following code:

unsigned char hashedChars[32];
NSString *inputString;
inputString = [NSString stringWithFormat:@"hello"];
NSData * inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
CC_SHA256(inputData.bytes, inputData.length, hashedChars);

我在stackoverflow上找到了这段代码.我并没有真正理解这段代码所做的所有事情,这里有一些关于代码的问题:

I found this piece of code on stackoverflow. I do not really get all the things this code do here are some questions about the code:

1.CC_SHA256 生成了一个哈希值,但是这个哈希值会再次存储在 inputData 中吗?我的意思是我可以做这样的事情:

1.The CC_SHA256 makes a hash but this hash will be stored in inputData again? What I mean can I do something like this:

NSString *string=CC_SHA256(..) //of course you can't put it directly in a NSString, but you get the point

2.最后hash必须是16进制字符串,但是CC_SHA256输出的类型是什么(UTF-8??)?

2.In the end the hash has to be a hexadecimal string, but what is the type that CC_SHA256 outputs (UTF-8??)?

3.CC_SHA256的第一个参数为什么一定要在最后放字节,inputData"就够了?

3.The first parameter of CC_SHA256 why do I have to put bytes at the end and is "inputData" enough?

4.字符串的长度(第二个参数)需要什么?

4.What is the need of the length of the string (second parameter)?

5.最后一个参数对我来说没有任何意义,有人可以解释一下为什么 hashedChars 必须是 32?

5.And the last parameter does not make any sense to me, can somebody please explain and why the hashedChars has to be 32?

推荐答案

CC_SHA256 的参数列表是:

The argument list for CC_SHA256 is:

 extern unsigned char *CC_SHA256(const void *data, CC_LONG len, unsigned char *md);

来自 man 页面:https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/CC_SHA256.3cc.html

参数说明:

  • *data 是输入字符串,你想要散列的内容.它是一个 C 字符串类型.获得它的一种方法是调用inputData.bytes",使用 inputData 一个 NSData 对象.
  • len 是输入字符串的长度.如果您开始使用 C 字符串,您就会意识到,使用字符串的函数要求长度是很正常的.这是因为在 C 中字符串只是一个字节序列,而文本字符串通常以空字节结尾,而二进制字符串可以有任何长度.这也是为了安全(缓冲区溢出").
  • *md 是输出.同样,这作为 C 字符串返回,SHA256 的固定长度为 32 字节(这就是为什么您没有看到 outputLength 参数).
  • 输出不相关",但可用于检查函数是否正常运行:if(CC_SHA256(...)) { all ok;}
  • *data is the input string, what you want to be hashed. It's a C string-type. A way to get this is to call 'inputData.bytes', with inputData a NSData object.
  • len is the length of the input string. As you'll realize if you'll start working with C strings, it's pretty normal for functions working with strings to ask for the length. That's because in C strings are just a sequence of bytes, and while text strings are generally terminated by a null byte, binary strings can have any length. It's also for safety ("buffer overflows").
  • *md is the output. Again, this is returned as a C string, of fixed length 32 bytes for SHA256 (that's why you don't see an outputLength parameter).
  • The output is "not relevant", but can be used to check if the function ran properly: if(CC_SHA256(...)) { all ok; }

结果字符串存入*md,是一个二进制C字符串,32字节长.它有 32 个字节长,因为这是 SHA256 摘要的长度;例如,MD5 为 16 个字节,SHA1 为 20 个字节等.这就是算法的工作原理!
输出只是一个二进制字符串.如果你想把它变成十六进制格式,你需要把它存储到一个 NSData 对象中,然后得到它的十六进制表示:

The result string is stored into *md, and it's a binary C string, 32 bytes long. It's 32 bytes long because that's the length of SHA256 digests; for example, 16 bytes for MD5, 20 bytes for SHA1, etc. It's just how the algorithm works!
The output is just a binary string. If you want to make it into hex format you need to store it into a NSData object, and then get a hex representation of it:

NSData *resultData = [NSData dataWithBytes:hashedChars length:32];

要获得十六进制表示,请查看此 SO 答案:https://stackoverflow.com/a/25378464/192024

To get the hex representation then look at this SO answer: https://stackoverflow.com/a/25378464/192024

这篇关于关于 CC_SHA256 Objective-c 的一些信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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