Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript相当于C#SHA512_Javascript_Encoding_Cryptojs - Fatal编程技术网

Javascript相当于C#SHA512

Javascript相当于C#SHA512,javascript,encoding,cryptojs,Javascript,Encoding,Cryptojs,我在转换这个C#函数时遇到了问题,该函数接收输入并将其转换为哈希,然后将字节数组返回到Javascript public static byte[] Sha512(string input) { using (SHA512 sha = new SHA512Managed()) { return sha.ComputeHash(Encoding.UTF8.GetBytes(input)); } } 本

我在转换这个C#函数时遇到了问题,该函数接收输入并将其转换为哈希,然后将字节数组返回到Javascript

    public static byte[] Sha512(string input)
    {
        using (SHA512 sha = new SHA512Managed())
        {
            return sha.ComputeHash(Encoding.UTF8.GetBytes(input));
        }
    }
本例中的输入为:“1pmy_gls.hu”。C#函数正确返回:

[57, 115, 234, 22, 47, 82, 252, 77, 133, 181, 138, 214, 32, 3, 155, 216, 181, 246, 130, 106, 160, 198, 73, 110, 50, 68, 56, 18, 120, 152, 231, 55, 3, 14, 144, 21, 84, 92, 237, 190, 6, 124, 51, 221, 95, 195, 73, 168, 100, 167, 84, 185, 167, 142, 184, 72, 243, 120, 213, 64, 176, 215, 15, 25]
我当前的Javascript函数将输入转换为:

[51, 57, 55, 51, 101, 97, 49, 54, 50, 102, 53, 50, 102, 99, 52, 100, 56, 53, 98, 53, 56, 97, 100, 54, 50, 48, 48, 51, 57, 98, 100, 56, 98, 53, 102, 54, 56, 50, 54, 97, 97, 48, 99, 54, 52, 57, 54, 101, 51, 50, 52, 52, 51, 56, 49, 50, 55, 56, 57, 56, 101, 55, 51, 55, 48, 51, 48, 101, 57, 48, 49, 53, 53, 52, 53, 99, 101, 100, 98, 101, 48, 54, 55, 99, 51, 51, 100, 100, 53, 102, 99, 51, 52, 57, 97, 56, 54, 52, 97, 55, 53, 52, 98, 57, 97, 55, 56, 101, 98, 56, 52, 56, 102, 51, 55, 56, 100, 53, 52, 48, 98, 48, 100, 55, 48, 102, 49, 57]
我对编码相当陌生,所以我不确定该如何继续。在本例中,我使用的是crypto,但使用crypto js没有问题。 我当前的Javascript函数是:

    convertPassword: function(password){
        convertPassword: function(password){
        var encoder = new TextEncoder()

        var sha512 = crypto.createHash('sha512').update(password).digest("hex")
        
        return encoder.encode(sha512)
    },
在C#代码中,您正在“latin1”字符集编码中消化散列密码。在node.js代码中,已将其设置为十六进制摘要。您需要将其更改为“latin1”,您的问题应该得到解决。所以为了有相同的输出

这一行:

var sha512 = crypto.createHash('sha512').update(password).digest("hex")
应该是这样的:

var sha512 = crypto.createHash('sha512').update(password).digest("latin1")