6

If I use the SQL SERVER HASHBYTES() function in a script on my machine, is it guaranteed to produce the same output if run on another machine/instance?

For example, will

SELECT HASHBYTES('SHA1', 'Hello World')

generate the same output regardless of SQL version, processor architecture etc?

and will

SELECT HASHBYTES('SHA1', column1) from table1

generate the same output if the column definition is slightly different - varchar(50) vs varchar(100) - for example?

Is there anything that needs to be considered here?

2 Answers 2

8

In terms of different instances, the HASHBYTES should return the same values if the same script is run, regardless of the server version, processor or architecture:

I've just run the following on both SQL Server 2005 and SQL Server 2012, on completely different hardware configuration. Both have produced the output 0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA

SELECT  HASHBYTES('SHA1', CAST('Test' AS VARCHAR(100)));

However, if the collation is different, the output will be different.

In terms of column definition, the output should be the same regardless of the length of the field.

Taking your example of VARCHAR(50) and VARCHAR(100), we can test the output like so:

SELECT  HASHBYTES('SHA1', CAST('Test' AS VARCHAR(50))); 
SELECT  HASHBYTES('SHA1', CAST('Test' AS VARCHAR(100)));

Both SELECT statements return the same value:

0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA

However, be aware that VARCHAR and NVARCHAR will not produce the same HASHBYTES value, even with the same string:

SELECT  HASHBYTES('SHA1', CAST('Test' AS VARCHAR(50))); -- Returns 0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA
SELECT  HASHBYTES('SHA1', CAST('Test' AS NVARCHAR(100))); -- Returns 0x9AB696A37604D665DC97134DBEE44CFE70451B1A 
6
  • Thanks Mark. I specifically interested in whether an identical select statement is guaranteed to produce the same output if run on a different machine. So if I was to run the 'Hello World' example locally on say, windows 7, and sent it to a colleague using windows 8, can I count on it producing the same result? Commented Jun 5, 2014 at 9:11
  • 1
    @user3701094 Updated answer. You'll need to take into account collation of the server/database. If you get your setup the same across both and tested with a value to ensure you have the same output, it should be fine. Commented Jun 5, 2014 at 9:23
  • 1
    Collations only come into play for comparisons between values, and have nothing to do with the difference in output here. The reason why n[var]char produces a different result is because it's two bytes per character whereas [var]char is a single byte per character. HASHBYTES, as the name implies, hashes a set of bytes, and so the two inputs return different results. This, of course, means that one needs to take care to use a consistent data type when using HASHBYTES!
    – Jon Seigel
    Commented Jun 5, 2014 at 13:22
  • Does anyone know if big endian / little endian architecture has an impact of the output on the hashbytes SHA1 algorithm? Also x86 vs x64 for that matter? Commented Jun 5, 2014 at 21:55
  • @A.Cunningham: I believe the input is always in SQL Server's internal format. I tested x86 vs x64 and the output is the same; I don't have an IA64 machine to test on. I think the only situation you'd need to be careful is if you're hashing in an external program (or SQLCLR) and then comparing the result to what was generated in SQL Server.
    – Jon Seigel
    Commented Jun 6, 2014 at 16:13
1

Have similar problem with HASHBYTES. Same values from one "varchar(max)" column generate multiple hash values.

The problem is caused by capitalization and Case Insensitive collation. Because of that my values were actually different.

If you do not care about case sensitivity you can change your values to upper case to mitigate that problem:

SELECT hashbytes('SHA2_512',UPPER('Hello World'))
UNION 
SELECT hashbytes('SHA2_512',UPPER('Hello world'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.