简体   繁体   中英

How to generate similar hash password using sql server?

Cryptography algorithm is md5.

In C#, I am using following code to generate password:-

private MD5 _md5;
var plain = ASCIIEncoding.Default.GetBytes('admin');
var hashed = _md5.ComputeHash(plain);
var shashed = ASCIIEncoding.Default.GetString(hashed);
Console.Write(shashed);

Result is: !#/)zW¥§C‰JJ€Ã

When I use SQL server code to generate password:-

DECLARE @HashThis varchar(100);
SELECT @HashThis = CONVERT(varchar(100),'admin');
SELECT HASHBYTES('MD5', @HashThis);
GO

Result is: 0x21232F297A57A5A743894A0E4A801FC3

I am using the same algorithm. How can I generate same hash value in SQL Server?

Your C# code and the SQL code are both computing the same hash value, but SQL is formatting the result as a hex string while your C# code is attempting to cast the bytes of the hash value directly to ASCII characters (which they are not). To get the same result in C# you could use the BitConverter class:

var shashed = "0x" + System.BitConverter.ToString(hashed).Replace("-", "");

Output:

0x21232F297A57A5A743894A0E4A801FC3

Have you checked the result against a characterset table;

In most charactersets, 0x21 is !, 0x23 is #, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM