HashBytes (Transact-SQL)
Returns the MD2, MD4, MD5, SHA, or SHA1 hash of its input.
Transact-SQL Syntax Conventions
- '<algorithm>'
-
Identifies the hashing algorithm to be used to hash the input. This is a required argument with no default. The single quotation marks are required.
- @input
-
Specifies a variable containing the data to be hashed. @input is varchar, nvarchar, or varbinary.
- ' input '
-
Specifies a string to be hashed.
Other Resources
Choosing an Encryption AlgorithmHelp and Information
Getting SQL Server 2005 AssistanceAlso, for the return type, it does not tell me how to determine the size of the varbinary. If the input is a 15-character password, how many bytes will the output be?
Review this link before using HashBytes or CHECKSUM
They are not 100% guaranteed functions for data compare.
[mdc] The odds of SHA-1 generating a collision are miniscule. The odds of generating a meaningful collision are significantly smaller than that. CHECKSUM, on the other hand, has a very high probability of generating collisions.
Msg 8152, Level 16, State 10, Line 1
String or binary data would be truncated.
https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=273429
[Tai Yee -- MSFT] Thanks, I have submitted this feedback to the SQL Server documentation team.
See following - :
[mdc] Effect on input longer than 8,000 bytes.
DECLARE @v varchar(max);
SET @v = REPLICATE ('A', 9000); -- Input longer than 8,000 bytes
SELECT HashBytes('SHA1', @v);
SET @v = REPLICATE ('A', 8000); -- Input exactly 8,000 bytesSELECT HASHBYTES('SHA1', @v);
The output, shown below, is the same hash value twice. This indicates that the two strings that were actually hashed were exactly the same. No error was generated by this code.
0xD2967D6425E56C18BA979EEFB4E0DBD1269D9BC9
0xD2967D6425E56C18BA979EEFB4E0DBD1269D9BC9