Hashing a string with Sha256

Nattfrosten

I try to hash a string using SHA256, I'm using the following code:

using System;
using System.Security.Cryptography;
using System.Text;
 public class Hash
    {
    public static string getHashSha256(string text)
    {
        byte[] bytes = Encoding.Unicode.GetBytes(text);
        SHA256Managed hashstring = new SHA256Managed();
        byte[] hash = hashstring.ComputeHash(bytes);
        string hashString = string.Empty;
        foreach (byte x in hash)
        {
            hashString += String.Format("{0:x2}", x);
        }
        return hashString;
    }
}

However, this code gives significantly different results compared to my friends php, as well as online generators (such as This generator)

Does anyone know what the error is? Different bases?

Quuxplusone

Encoding.Unicode is Microsoft's misleading name for UTF-16 (a double-wide encoding, used in the Windows world for historical reasons but not used by anyone else). http://msdn.microsoft.com/en-us/library/system.text.encoding.unicode.aspx

If you inspect your bytes array, you'll see that every second byte is 0x00 (because of the double-wide encoding).

You should be using Encoding.UTF8.GetBytes instead.

But also, you will see different results depending on whether or not you consider the terminating '\0' byte to be part of the data you're hashing. Hashing the two bytes "Hi" will give a different result from hashing the three bytes "Hi". You'll have to decide which you want to do. (Presumably you want to do whichever one your friend's PHP code is doing.)

For ASCII text, Encoding.UTF8 will definitely be suitable. If you're aiming for perfect compatibility with your friend's code, even on non-ASCII inputs, you'd better try a few test cases with non-ASCII characters such as é and and see whether your results still match up. If not, you'll have to figure out what encoding your friend is really using; it might be one of the 8-bit "code pages" that used to be popular before the invention of Unicode. (Again, I think Windows is the main reason that anyone still needs to worry about "code pages".)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Asynchronous SHA256 Hashing

Why does the bit count of a string output from a hashing function (SHA256) fluctuate?

Hashing password by SHA256 then write to file

hashing a dictionary using sha256

Hashing function Sha256 in Circom

Hashing String (SHA-256) in an ActionListener class

Java mac sha256 hashing does not match with php hmac sha256 using pack?

SHA256 Hashing with Salt in PHP not same as in C#

Decode string using python || SHA256

hex string to SHA256 digest in python

SHA256 different values for same String

Sign a byte string with SHA256 in Python

Excel formula-based function for SHA256 / SHA512 hashing without VBA or macros

Hmac with SHa256 Or SHa256 with key appended in the input string? Which is more secure?

sha-256 hashing in python

SHA256 Hashing with two blocks: Where should the hash of the first block be inputed?

Encryption / decryption for j2me app with AES 128 encryption using sha256 hashing

SHA256 hashing in Java - meaning of 0xff bitwise operation

SHA256 Hashing Large files in angular 6 using Filereader Issue

Converting some browser JavaScript to HTML and JavaScript files - Hashing using SHA256

Base64 encoding of a SHA256 string

java - is there a way to confirm that a string is a sha256 hash?

How to hash some string with sha256 in Java?

SHA256 digest differs between array initializer and string

how to generate SHA256 with 32 bytes for a given string?

Get SHA256 hash of UTF-16le String

Sql Server Storing SHA256 String as Question Marks

how to get sha256 hashed string in erlang?

VB.Net signing string SHA256 - invalid algorithm