Original: SQL Server 2005 MD5 function
In SQL Server 2005, the function Hashbytes (), which is provided by Microsoft in SQL Server 2005, can be used to calculate the MD5 and SHA1 values of a string, using the following method:--Get 123456 MD5 encryption string Select Hashbytes (' MD5 ', ' 123456 ');--Get 123456 of the SHA1 encryption string Select Hashbytes (' SHA1 ', ' 123456 '); With this function you can encrypt a string in SQL Server, However, the return result of the hashbytes () function is the varbinary type (binary data in 16 binary form, starting with 0x). Normally, all we need is string data, and the first thing many people think of is to convert varbinary to varchar with the cast or CONVERT function, but then the converted result is garbled, Correct conversion of varbinary variable-length binary data to 16 binary strings should use the system built-in function sys.fn_varbintohexstr () or SYS.FN_SQLVARBASETOSTR (only available under SQL Server2005), As follows: Select Sys.fn_varbintohexstr (hashbytes (' MD5 ', ' 123456 ')) then you can intercept the desired part: Set Right (SYS.FN_VARBINTOHEXSTR ( Hashbytes (' MD5 ', ' 123456 ')), 32) Encrypt the string for MD5.
SQL Server 2005 MD5 functions