Calculate MD5 , SHA256, SHA512 in SQL Server

visibility 2,148 comment 0 access_time 2y languageEnglish

SQL Server has built-in function HASHBYTES that can be used to calculate hash values. The supported hash algorithms include MD2, MD4, MD5, SHA, SHA1, SHA2_256 and SHA2_512.

Function HASHBYTES 

The signature of this function is:

HASHBYTES ( '<algorithm>', { @input | 'input' } )  
  
<algorithm>::= MD2 | MD4 | MD5 | SHA | SHA1 | SHA2_256 | SHA2_512 

Examples

MD5

SELECT HASHBYTES('MD5','abc');

Output:

0x900150983CD24FB0D6963F7D28E17F72

SHA256

SELECT HASHBYTES('SHA2_256','abc');

Output:

0xBA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD

SHA512

SELECT HASHBYTES('SHA2_512','abc');

Output:

0xDDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F

Return hexadecimal string

Function HashBytes returns VARBINARY; use CONVERT function to convert the binary to string:

SELECT CONVERT(VARCHAR(100),HASHBYTES('MD5','abc'),1);
SELECT CONVERT(VARCHAR(100),HASHBYTES('MD5','abc'),2);

Results:

0x900150983CD24FB0D6963F7D28E17F72
900150983CD24FB0D6963F7D28E17F72

References

HASHBYTES (Transact-SQL)

copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Tags