【问题标题】:A JavaScript hash and the equivalent .NET algorithmJavaScript 哈希和等效的 .NET 算法
【发布时间】:2011-01-31 09:57:23
【问题描述】:

在 JavaScript 端使用 this code

    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

我无法使用这两位代码为相同的值重新创建相同的哈希值。

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

我错过了什么?我认为这与字符编码有关?

解决方案:这是一个简单的更改:

    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

【问题讨论】:

    标签: c# javascript asp.net vb.net hash


    【解决方案1】:

    您将hash 数组视为ASCII 字符序列。您需要十六进制表示,您可以使用 BitConverter.ToString 获得,如下所示:

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

    【讨论】:

      【解决方案2】:

      我没有足够的 VB 知识来提供代码,但问题是您将字节数组视为编码字符串并尝试对其进行解码。您实际上应该将字节数组转换为十六进制字符串。例如,请参阅here

      【讨论】:

        【解决方案3】:

        它们基本上是一样的……你可能想看看:How do you convert Byte Array to Hexadecimal String, and vice versa?

        您可以使用它将字符串转换回它的十六进制表示形式。

        证明它的工作原理相同的示例,请参阅:

        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();
                        }
                    }
                }
            }
        }
        

        【讨论】:

        • 或者只是string hex = string.Concat(hash.Select(x => x.ToString("x2")));。不确定 VB 等价物究竟是什么。
        猜你喜欢
        • 2013-09-19
        • 1970-01-01
        • 1970-01-01
        • 2017-12-25
        • 2014-05-27
        • 2013-03-15
        • 2021-08-22
        • 2016-06-23
        • 1970-01-01
        相关资源
        最近更新 更多