Axapta SHA256为相同的字符串输入提供不同的结果

Axapta SHA256为相同的字符串输入提供不同的结果,axapta,x++,sha256,Axapta,X++,Sha256,我有使用SHA256生成哈希代码的简单代码,但是,对于相同的输入,它会给出不同的结果。但是,如果我在引号中声明相同的字符串值,例如\u input=“test”,那么它将返回相同的结果 public static System.String generateKey(System.String _input) { System.Byte[] convertedArr; SHA256Managed sh = new System.Security.Cryptography.SHA

我有使用SHA256生成哈希代码的简单代码,但是,对于相同的输入,它会给出不同的结果。但是,如果我在引号中声明相同的字符串值,例如
\u 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;
    }
注:

convertedArr=sh.ComputeHash(System.Text.Encoding::UTF8.GetBytes(_-inputString),0,System.Text.Encoding::UTF8.GetByteCount(_-input))

散列的输入是
\u inputString
,但长度取自
\u input
,它们不相同<代码>\u输入字符串
!=<代码>\u输入

功能定义:

public static System.String generateKey(System.String _input)
当前代码:

convertedArr = sh.ComputeHash(System.Text.Encoding::UTF8.GetBytes(_inputString),0, System.Text.Encoding::UTF8.GetByteCount(_input));
可调试(半伪)代码:

有了这个输入和长度可以很容易地验证。由于
\u输入
仅使用一次,因此出错的可能性较小


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

输入的值是多少??请显示更多的代码-输入是什么,FNSGenerateHashDetails etc如果不使用它,请不要标记。
FNSGenerateHashDetails
是否可能添加一些“盐“到散列输入?当您在函数调用中嵌入函数调用时,调试变得更加困难。”。使用中间变量,任何合适的编译器都会减少代码。编写代码以使人理解和调试。最后,提供一个,将其添加到问题中,包括文本值和中间值,例如
FNSGenerateHashDetails::GetBytes
System.text.Encoding::UTF8.GetByteCount
的结果。简明扼要的调试代码不是leet。感谢zaph,我使用的代码与您的代码相同,输入和输入字符串的类型错误。是一样的。inputBytes=System.Text.Encoding::UTF8.GetBytes(_-input)inputLength=System.Text.Encoding::UTF8.GetByteCount(_-input)hashBytes=convertedArr=sh.ComputeHash(inputBytes,0,inputLength);我尝试了inputLength和inputBytes的长度,但不确定为什么sh.ComputeHash每次都不同。inputBytes和inputLength似乎总是正常的。如果我在程序中硬编码并声明相同的字符串,它会工作,但当我把它作为参数时,它就不工作了。完全不知道现在输出是不同的,因为输入是不同的。发生这种情况的一种方法是,如果length参数大于输入数据,并且为了获得指定的字节数,它使用实际输入后面的垃圾字节。显然,
System.Security.Cryptography.SHA256Managed
起作用了,我还没有读过这些文档,如果你还没有读过的话,请仔细看看。你可以尝试
ComputeHash(Byte[])
而不是
ComputeHash(Byte[]), Int32, Int32)
。这将消除大小变量。
.Text.Encoding::UTF8.GetBytes
是否也返回
字节[]
类型?最后尝试使用一个短字符串(例如“TestMe”),并与SHA-256联机实现进行比较,例如,使用一个简单的案例。要添加传递给ComputeHash的字节[]有409个字节,即inputLength=System.Text.Encoding::UTF8.GetByteCount(_input)=409,sh.ComputeHash总是给出32个字节。由于我的字节大小传递给computeHash,是否会出现问题?是的,我也尝试了computeHash(字节[])。System.Text.Enconding::UTF8.GetBytes还返回保持不变的字节[]。如果我硬编码_input=myFullStringContent,它可以工作,但如果我传递的字符串与存储在System.string类型变量中的参数相同,并且我得到了该变量的字节数组,并计算了该变量的哈希,那么它就不工作。不确定硬编码字符串值和作为参数传递之间的区别。我尝试了ComputeHash(字节[]),也尝试了传递长度的方法
inputBytes  = System.Text.Encoding::UTF8.GetBytes(_input)
inputLength = System.Text.Encoding::UTF8.GetByteCount(_input)
hashBytes   = convertedArr = sh.ComputeHash(inputBytes, 0, inputLength);