SHA256 哈希计算 [英] SHA256 hash calculation

查看:162
本文介绍了SHA256 哈希计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的雇主,我必须向网络应用程序的客户提供他们下载的某些文件的校验和.

for my employer I have to present customers of a web-app with checksums for certain files they download.

我想向用户展示他们的客户端工具也可能生成的散列,因此我一直在比较在线散列工具.我的问题是关于它们的散列形式,因为它们不同,很奇怪.

I'd like to present the user with the hash their client tools are also likely to generate, hence I have been comparing online hashing tools. My question is regarding their form of hashing, since they differ, strangely enough.

快速搜索后,我用 5 进行了测试:

After a quick search I tested with 5:

  1. http://www.convertstring.com/Hash/SHA256
  2. http://www.freeformatter.com/sha256-generator.html#广告输出
  3. http://online-encoder.com/sha256-encoder-decoder.html
  4. http://www.xorbin.com/tools/sha256-hash-calculator
  5. http://www.everpassword.com/sha-256-generator

输入值test"(后面没有enter")所有 5 项都会给我相同的 SHA256 结果.然而,这里开始了奇怪的事情,当我输入值test[enter]test"(所以两行)时,在线工具 1、2 和 3 给了我相同的 SHA256 哈希值,而站点 4 和 5 给了我不同的哈希值(所以 1、2 和 3 相等,而 4 和 5 相等).这很可能与工具或底层代码处理 \r\n 的方式有关,或者至少我是这么认为的.

Entering the value 'test' (without 'enter' after it) all 5 give me the same SHA256 result. However, and here begins the peculiar thing, when I enter the value 'test[enter]test' (so two lines) online tool 1, 2 and 3 give me the same SHA256 hash, and site 4 and 5 give me a different one (so 1, 2 and 3 are equal, and 4 and 5 are equal). This most likely has to do with the way the tool, or underlying code handles \r\n, or at least I think so.

巧合的是,站点 1、2 和 3 向我展示了与我的 C# 代码相同的哈希:

Coincidentally, site 1, 2 and 3 present me with the same hash as my C# code does:

    var sha256Now = ComputeHash(Encoding.UTF8.GetBytes("test\r\ntest"), new SHA256CryptoServiceProvider());

    private static string ComputeHash(byte[] inputBytes, HashAlgorithm algorithm)
    {
        var hashedBytes = algorithm.ComputeHash(inputBytes);
        return BitConverter.ToString(hashedBytes);
    }

问题是:哪些网站是正确的"?

The question is: which sites are 'right'?

有什么方法可以知道哈希值是否符合标准?

Is there any way to know if a hash is compliant with the standard?

UPDATE1:将编码更改为 UTF8.不过,这对正在创建的输出哈希没有影响.谢谢@Hans.(因为我的 Encoding.Default 可能是 Encoding.UTF8)

UPDATE1: Changed the encoding to UTF8. This has no influence on the output hash being created though. Thx @Hans. (because my Encoding.Default is probably Encoding.UTF8)

UPDATE2:也许我应该稍微扩展一下问题,因为它可能没有得到充分解释,抱歉.我想我要问的更多是可用性问题而不是技术问题;我应该提供具有不同行尾的所有哈希吗?还是我应该坚持一个?客户可能会打电话给我的公司,因为如果他们有不同的哈希计算方式,他们的文件会以某种方式被更改.这通常是如何解决的?

UPDATE2: Maybe I should expand the question a bit, since it may have been under-explained, sorry. I guess what I am asking is more of a usability question than a technical one; Should I offer all the hashes with different line endings? Or should I stick to one? The client will probably call my company afraid that their file was changed somehow if they have a different way of calculating the hash. How is this usually solved?

推荐答案

所有这些站点都返回有效值.

All those sites return valid values.

站点 4 和 5 使用 \n 作为换行符.

Sites 4 and 5 use \n as line break.

编辑

我看到您编辑了您的问题以在代码示例中添加 Encoding.Default.GetBytes.

I see you edited your question to add Encoding.Default.GetBytes in the code example.

这很有趣,因为您看到有一些字符串到字节数组转换要在计算散列之前运行.换行(\n\r\n)以及文本编码都是解释字符串以获得不同字节值的方法.

This is interesting, because you see there is some string to byte array conversion to run before computing the hash. Line breaking (\n or \r\n) as well as text encoding are both ways to interpret your string to get different bytes values.

一旦输入的字节数相同,所有哈希结果都将相同.

Once you have the same bytes as input, all hash results will be identical.

编辑 2:

如果您直接处理字节,则只需使用这些字节计算散列.不要试图提供不同的哈希值;一个哈希值只能返回一个值.如果您的客户的哈希值与您的不同,那么他们做错了.

If you're dealing with bytes directly, then just compute the hash with those bytes. Don't try to provide different hash values; a hash must only return one value. If your clients have a different hash value than yours, then they are doing it wrong.

话虽如此,我很确定它永远不会发生,因为没有任何方法可以误解字节数组.

That being said, I'm pretty sure it won't ever happen because there isn't any way to misinterpret a byte array.

这篇关于SHA256 哈希计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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