Maximiliano Cesán
Maximiliano Cesán

Reputation: 170

Difference between C# cryptoSha256 and HashLib Sha256

Good morning, I need to Encryption a file in a Hash mode.

I look this library to use

HashLibrary

My Test with local C# 256 Method and HashLib Method get Differents Results,

                FileStream fileStream;
                SHA256 sha256 = SHA256Managed.Create();

                fileStream = new FileStream(localPath, FileMode.Open);
                fileStream.Position = 0;

                ///using System.Security.Cryptography;
                byte[] hashValue = sha256.ComputeHash(fileStream);
                string hash = ByteArrayToString(hashValue);

                #region using HashLib;
                //Run Hash
                IHash hash256 = HashFactory.Crypto.CreateSHA256();
                HashResult result256 = hash256.ComputeStream(fileStream);
                byte[] bytearray = result256.GetBytes();
                string stringtest = result256.ToString();
                stringtest = result256.ToString().Replace("-", "");
                #endregion

Result of First Method

byte[] hashValue = 94,171,27,169,32,82,120,2,177,84,58,6,216,77,110,239,85,282,75,159,183,85,70,208,22,146,201,22,47,122,153,74

string hash = 5EAB1BA920527802B1543A06D84D6EEF55FC4B9FB75546D01692C9162F7A994A

Result of Second Method with HashLib

var bytearray = 227,176,196,66,152,252,28,20,154,251,244,200,153,11,185,36,39,174,65,228,100,155,147,76,164,149,153,27,120,82,184,85

var stringtest = E3B0C442-98FC1C14-9AFBF4C8-996FB924-27AE41E4-649B934C-A495991B-7852B855

Someone can help me? i dont understand what is the problem, why result are differents?.

I start to use this library becouse i cant sha224, sha1 with System.Security.Cryptography

Upvotes: 0

Views: 185

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93998

You already have read everything from the stream, so you're at the end of the stream. Recreate it to perform the testing. The second string is the well known hash over an empty array (aka nuthin').

Sometimes it is easy to check your output against well known tools such as sha256sum. For instance, the empty array can be tested like this, given a normal *nix shell (e.g. Cygwin or the Windows Subsystem for Linux I suppose):

$ dd count=0 status=none | sha256sum -b | awk '{print $1}'
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Upvotes: 3

Related Questions