截断字节数组与对来自 SHA-256 的编码字符串进行子串化
我不熟悉哈希算法以及使用它们时相关的风险,因此对我在上一个问题中收到的以下答案有疑问。 。 。
基于哈希值在编码为 ASCII 时必须在 16 个 ASCI 字符之内的评论,解决方案是首先选择一些加密哈希函数(SHA-2 系列包括 SHA-256、SHA-384、和 SHA-512) 然后,将所选哈希函数的输出截断为 96 位(12 字节) - 即保留哈希函数输出的前 12 个字节并丢弃剩余字节 然后,将截断的输出进行 Base-64 编码为 16 个 ASCII 字符(128 位) 有效地产生 96 位强的加密哈希。
如果我将 Base-64 编码的字符串子串为 16 个字符,那么保留哈希函数的前 12 个字节然后对它们进行 Base-64 编码是完全不同的吗?如果是这样,有人可以解释一下(提供示例代码)截断字节数组吗?
我针对 36,000 多个不同值测试了完整哈希值的子字符串,没有发生冲突。下面的代码是我当前的实现。
感谢您提供的任何帮助(和清晰度)。
public static byte[] CreateSha256Hash(string data)
{
byte[] dataToHash = (new UnicodeEncoding()).GetBytes(data);
SHA256 shaM = new SHA256Managed();
byte[] hashedData = shaM.ComputeHash(dataToHash);
return hashedData;
}
public override void InputBuffer_ProcessInputRow(InputBufferBuffer Row)
{
byte[] hashedData = CreateSha256Hash(Row.HashString);
string s = Convert.ToBase64String(hashedData, Base64FormattingOptions.None);
Row.HashValue = s.Substring(0, 16);
}
[原帖] (http://stackoverflow.com/questions/4340471/is-there-a-hash-algorithm-that- Produces-a-hash-size-of-64-bits-in-c)
I am not familiar with Hashing algorithms and the risks associated when using them and therefore have a question on the answer below that I received on a previous question . . .
Based on the comment that the hash value must, when encoded to ASCII, fit within 16 ASCI characters, the solution is first, to choose some cryptographic hash function (the SHA-2 family includes SHA-256, SHA-384, and SHA-512)
then, to truncate the output of the chosen hash function to 96 bits (12 bytes) - that is, keep the first 12 bytes of the hash function output and discard the remaining bytes
then, to base-64-encode the truncated output to 16 ASCII characters (128 bits)
yielding effectively a 96-bit-strong cryptographic hash.
If I substring the base-64-encoded string to 16 characters is that fundamentally different then keeping the first 12 bytes of the hash function and then base-64-encoding them? If so, could someone please explain (provide example code) for truncating the byte array?
I tested the substring of the full hash value against 36,000+ distinct values and had no collisions. The code below is my current implementation.
Thanks for any help (and clarity) you can provide.
public static byte[] CreateSha256Hash(string data)
{
byte[] dataToHash = (new UnicodeEncoding()).GetBytes(data);
SHA256 shaM = new SHA256Managed();
byte[] hashedData = shaM.ComputeHash(dataToHash);
return hashedData;
}
public override void InputBuffer_ProcessInputRow(InputBufferBuffer Row)
{
byte[] hashedData = CreateSha256Hash(Row.HashString);
string s = Convert.ToBase64String(hashedData, Base64FormattingOptions.None);
Row.HashValue = s.Substring(0, 16);
}
[Original post]
(http://stackoverflow.com/questions/4340471/is-there-a-hash-algorithm-that-produces-a-hash-size-of-64-bits-in-c)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有区别。然而,更容易获取数组前 12 个字节的 Base64 字符串,而不是截断数组:
Base 64 编码只是在每个字符中放入 6 位,因此 3 个字节(24 位)变成 4 个字符。只要您在偶数 3 字节边界处拆分数据,就与在偶数 4 字符边界处拆分字符串相同。
如果您尝试在这些边界之间分割数据,base64 字符串将使用填充数据填充到下一个边界,因此结果将不同。
No, there is no difference. However, it's easier to just get the base64 string of the first 12 bytes of the array, instead of truncating the array:
The base 64 encoding simply puts 6 bits in each character, so 3 bytes (24 bits) goes into 4 characters. As long as you are splitting the data at an even 3 byte boundary, it's the same as splitting the string at the even 4 character boundary.
If you try to split the data between these boundaries, the base64 string will be padded with filler data up to the next boundary, so the result would not be the same.
截断就像在此处添加
Take(12)
一样简单:更改
为:
Truncating is as easy as adding
Take(12)
here:Change
To: