0

Using this code on the JavaScript side and

    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".

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243

3 Answers3

1

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. You should actually be converting the byte array to a hex string. See here for example.

spender
  • 117,338
  • 33
  • 229
  • 351
1

You're treating the hash array as a sequence of ASCII characters. You need the hexadecimal representation instead, which you can get using BitConverter.ToString, something like this:

Dim res As String = BitConverter.ToString(hash).Replace("-", "").ToLower();
LukeH
  • 263,068
  • 57
  • 365
  • 409
0

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();
                }
            }
        }
    }
}
Community
  • 1
  • 1
Jimmy Chandra
  • 6,472
  • 4
  • 26
  • 38
  • Or just `string hex = string.Concat(hash.Select(x => x.ToString("x2")));`. Not sure exactly what the VB equivalent would be. – LukeH Jan 31 '11 at 10:22