In SQL Server, for simple hash code
encryption like Password Encryption,
we can use the HASHBYTES function to encrypt the string.
This is a built-in cryptographic function
with hashing algorithms like MD2,
MD4, MD5, SHA1, SHA2 (256 and 512).
In these algorithm, SHA2 (256 and 512) are
introduced in SQL Server 2008.
The other MD and SHA1 algorithms are
available in SQL Server 2005 onwards.
The output conforms to the algorithm
standard:
128 bits (16 bytes) for MD2, MD4, and MD5
160 bits (20 bytes) for SHA and SHA1
256 bits (32 bytes) for SHA2_256
512 bits (64 bytes) for SHA2_512
I strongly recommended only SHA2 (256 and 512). All the earlier
versions of MD and SHA1 are depreciated.
Try this SQL Queries:
DECLARE @input NVARCHAR(50);
SET @input = 'arkaguptablog'
SELECT HASHBYTES('MD2', @input) as 'MD2 Output', LEN(HASHBYTES('MD2', @input)) AS Length;
SELECT HASHBYTES('MD4', @input) as 'MD4 Output', LEN(HASHBYTES('MD4', @input)) AS Length;
SELECT HASHBYTES('MD5', @input) as 'MD5 Output', LEN(HASHBYTES('MD5', @input)) AS Length;
SELECT HASHBYTES('SHA', @input) as 'SHA Output', LEN(HASHBYTES('SHA', @input)) AS Length;
SELECT HASHBYTES('SHA1', @input) as 'SHA1 Output', LEN(HASHBYTES('SHA1', @input)) AS Length;
SELECT HASHBYTES('SHA2_256', @input) as 'SHA-256 Output', LEN(HASHBYTES('SHA2_256', @input)) AS Length;
SELECT HASHBYTES('SHA2_512', @input) as 'SHA-512 Output', LEN(HASHBYTES('SHA2_512', @input)) AS Length;
Output Result :
If there is
any other better option,
Please share
it here or mail to me arkaa4@gmail.com
Thank You,
Arka Gupta.
No comments:
Post a Comment