简体   繁体   English

JavaScript哈希和等效的.NET算法

[英]A JavaScript hash and the equivalent .NET algorithm

Using this code on the JavaScript side and 在JavaScript端使用此代码

    Using sha As New SHA256Managed
        Using memStream As New MemoryStream(Encoding.ASCII.GetBytes("Hello World!"))
            Dim hash() As Byte = sha.ComputeHash(memStream)
            Dim res As String = Encoding.Default.GetString(hash)
        End Using
    End Using

I have been unable to recreate the same hash for the same values with these two bits of code. 我无法使用这两位代码为相同的值重新创建相同的哈希值。

The JavaScript implementation returns 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069 , and the VB.NET example returns ƒ±eñüS¹-ÁH¡Ö]ü-K£Öw(JÝÒ mi" . JavaScript实现返回7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069 ,VB.NET示例返回ƒ±eñüS¹-ÁH¡Ö]ü-K£Öw(JÝÒ mi"

What am I missing? 我错过了什么? I assume it's something to do with the character encoding? 我认为它与字符编码有关?

Solution: it was one simple change: 解决方案:这是一个简单的更改:

    Using sha As New SHA256Managed
        Using memStream As New MemoryStream(Encoding.ASCII.GetBytes("Hello World!"))
            Dim hash() As Byte = sha.ComputeHash(memStream)
            Dim res As String = BitConverter.ToString(hash)
        End Using
    End Using

I don't know enough VB to provide code, but the problem is that you are treating the byte array as an encoded string and attempting to decode it. 我不知道足够的VB提供代码,但是问题是您将字节数组视为已编码的字符串并尝试对其进行解码。 You should actually be converting the byte array to a hex string. 您实际上应该将字节数组转换为十六进制字符串。 See here for example. 例如参见这里

You're treating the hash array as a sequence of ASCII characters. 您将hash数组视为ASCII字符序列。 You need the hexadecimal representation instead, which you can get using BitConverter.ToString , something like this: 您需要使用十六进制表示形式,可以使用BitConverter.ToString来获取,如下所示:

Dim res As String = BitConverter.ToString(hash).Replace("-", "").ToLower();

They are basically the same thing... you might want to see: How do you convert Byte Array to Hexadecimal String, and vice versa? 它们基本上是相同的...你可能想看到: 如何将字节数组转换为十六进制字符串,反之亦然?

You can use it to convert the string back to hex representation of it. 您可以使用它将字符串转换回十六进制表示形式。

An example to prove it work the same, see: 一个证明它工作原理的例子,见:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var sha = new SHA256Managed())
            {
                using (var stream = new MemoryStream(
                    Encoding.ASCII.GetBytes("Hello World!")))
                {
                    var hash = sha.ComputeHash(stream);
                    var result = Encoding.Default.GetString(hash);

                    //print out each byte as hexa in consecutive manner
                    foreach (var h in hash)
                    {
                        Console.Write("{0:x2}", h);
                    }
                    Console.WriteLine();

                    //Show the resulting string from GetString
                    Console.WriteLine(result);
                    Console.ReadLine();
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM