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

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

1 Comment

Or just string hex = string.Concat(hash.Select(x => x.ToString("x2")));. Not sure exactly what the VB equivalent would be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.