【问题标题】:SHA256 giving different result for same string inputSHA256 为相同的字符串输入给出不同的结果
【发布时间】:2017-12-04 12:47:40
【问题描述】:

我有使用 SHA256 生成哈希码的简单代码,但是对于相同的输入,它会给出不同的结果。但是,如果我在引号中声明相同的字符串值,例如 _input= "test",那么它会返回相同的结果。

public static System.String generateKey(System.String _input)
{
     System.Byte[] convertedArr;
     SHA256Managed sh = new System.Security.Cryptography.SHA256Managed();
        convertedArr = sh.ComputeHash(System.Text.Encoding::UTF8.GetBytes(_inputString),0, System.Text.Encoding::UTF8.GetByteCount(_input));
        hashCode = System.Convert::ToBase64String(convertedArr);
    return hashCode;
    }

【问题讨论】:

  • _input 的值是多少?
  • 请显示更多您的代码 - 输入内容、FNSGenerateHashDetails 等
  • 不使用请不要标记c#
  • FNSGenerateHashDetails 是否可以在哈希输入中添加一些“盐”?
  • 当您在函数调用中嵌入函数调用时,调试变得更加困难。使用中间变量,任何体面的编译器都会减少代码。编写代码以供人类理解和调试。最后提供一个minimal reproducible example,将其添加到问题alonf 中,带有文本值和中间值,例如FNSGenerateHashDetails::GetBytesSystem.Text.Encoding::UTF8.GetByteCount 的结果。简洁和调试代码不是 leet

标签: axapta x++ sha256


【解决方案1】:

注意:

convertedArr = sh.ComputeHash(System.Text.Encoding::UTF8.GetBytes(_inputString),0, System.Text.Encoding::UTF8.GetByteCount(_input));

哈希的输入是_inputString,但长度取自_input,它们不一样。 _inputString != _input.

函数定义:

public static System.String generateKey(System.String _input)

当前代码:

convertedArr = sh.ComputeHash(System.Text.Encoding::UTF8.GetBytes(_inputString),0, System.Text.Encoding::UTF8.GetByteCount(_input));

可调试(半伪)代码:

inputBytes  = System.Text.Encoding::UTF8.GetBytes(_input)
inputLength = System.Text.Encoding::UTF8.GetByteCount(_input)
hashBytes   = convertedArr = sh.ComputeHash(inputBytes, 0, inputLength);

有了这个输入和长度可以很容易地验证。因为_input 只使用了一次,所以出错的可能性较小。

注意:在实践中,我会从inputBytes 获得长度,但我不精通 X++,所以我没有进行更改。

【讨论】:

  • 谢谢 zaph 我在下面使用的和你的一样,_input 和 _inputString 是类型错误。它是一样的。 inputBytes = System.Text.Encoding::UTF8.GetBytes(_input) inputLength = System.Text.Encoding::UTF8.GetByteCount(_input) hashBytes = convertArr = sh.ComputeHash(inputBytes, 0, inputLength);我已经尝试了 inputLength 和 inputBytes 的长度,但不确定为什么 sh.ComputeHash 每次都不同。 inputBytes 和 inputLength 似乎总是可以的。如果我在程序中硬编码并声明相同的字符串,它可以工作,但当我把它作为参数时就不行了。现在完全一无所知
  • 输出不同,因为输入不同。发生这种情况的一种方法是,如果长度参数大于输入数据,并且为了获得指定的字节数,它使用了跟随实际输入的垃圾字节。显然System.Security.Cryptography.SHA256Managed 有效,我还没有阅读文档,如果你还没有阅读,请好好看看它们。
  • 您可以尝试ComputeHash(Byte[]) 而不是ComputeHash(Byte[], Int32, Int32)。这将消除大小变量。 .Text.Encoding::UTF8.GetBytes 也返回 Byte[] 类型吗?最后尝试使用一个短字符串(例如“TestMe”)并与HASH CALCULATOR等SHA-256在线实现进行比较,得到一个简单的案例。
  • 添加传递给 ComputeHash 的 Byte[] 有 409 个字节,即 inputLength = System.Text.Encoding::UTF8.GetByteCount(_input) = 409 , sh.ComputeHash 总是给出 32 个字节。由于我为 computeHash 传递的字节大小会出现问题吗?
  • 是的,我也试过 ComputeHash(Byte[])。 System.Text.Enconding::UTF8.GetBytes 还返回保持不变的 Byte[]。如果我硬编码 _input=myFullStringContent 它可以工作,但是如果我传递相同的字符串作为存储在 System.String 类型变量中的参数,并且我得到字节数组和 computeHash,那么它就不起作用。不确定硬编码字符串值和作为参数传递之间有什么区别。我尝试了 ComputeHash(Byte[]) 和一个传递长度的方法
猜你喜欢
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
  • 2011-04-10
  • 2020-03-12
相关资源
最近更新 更多