question

JagannathSahu-6824 avatar image
0 Votes"
JagannathSahu-6824 asked JackJJun-MSFT commented

Facing issue with hashing in c#

Hi,
I am facing an issue with the hashing.
I am using the below code for hashing.
i have to hash certain number and inserting in to a database table.
i am using two applications . in both the application i am using the same number which has to be hashed and the same secret key.
ideally it should save same value in database, but both the hashed values are different, its very strange.

239819-differenthashvalue.jpg

Any help is appreciated.

  public static byte[] GetHashValue(string text, string secret)
         {
             string hash = text + secret;
    
             byte[] tmpSource;
             byte[] tmpHash;
             //Create a byte array from source data
             tmpSource = Encoding.ASCII.GetBytes(hash);
    
             //Compute hash based on source data
             using (var md5 = MD5.Create())
             {
                 tmpHash = md5.ComputeHash(tmpSource);
             }
    
             return tmpHash;
         }



dotnet-csharpdotnet-standard
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JagannathSahu-6824, Welcome to Microsoft Q&A, based on my test, I could get the same byte array by using the same parameter. Then, Could you provide the related code about how do you insert the byte array to the database(like the picture you provided)?
I also get the same hash value in the database:

240364-image.png


0 Votes 0 ·
image.png (3.7 KiB)

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

most likely you have a bug where you are not sending the same string and secret. just add log to see.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.