Skary
Skary

Reputation: 1362

HASHBYTES 'SHA1' return value that differ to standard SHA1 implementation

I am computing HASH value of each row in a table (for diffing purpose), after implementing the algorithm I am testing the results. Results are consistent and algorithm somewhat seems to work, but testing it step by step I found a strange result.

The script:

SELECT HASHBYTES('SHA1', (SELECT INNERTBL.VALUT FOR XML RAW)) as KHASH
FROM ACLING AS INNERTBL

Should perform the SHA1 calculation on the table key, but when I perform the same calculation with external tool I get different results:

enter image description here

In fact when I perform SHA1('<row VALUT="A"/>') with external tool (tool here: https://emn178.github.io/online-tools/sha1.html) I get a different result:

enter image description here

So my question is, there is something wrong with my logic or simply SQL Server use some non standard SHA1 "parametrization"? (I have suspect about the use of a, may be standard but particular, padding scheme)

Example in fiddler: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=efa4e0ba11c112f54e36afb5d54d2cce

Upvotes: 0

Views: 696

Answers (2)

Peter &#197;
Peter &#197;

Reputation: 1319

It is important to remember that you get the same result only if the string are binary the same. For example if the two strings uses different characterset they will have different HASH value. For more details please check thsi out https://security.stackexchange.com/questions/18290/is-sha-1-hash-always-the-same

Upvotes: 1

lptr
lptr

Reputation: 6798

SELECT HASHBYTES('SHA1','<row VALUT="A"/>'), --- you are testing this
HASHBYTES('SHA1',N'<row VALUT="A"/>') -- ..but for xml returns Nvarchar

Upvotes: 3

Related Questions