Saturday, 6 December 2014

Hashing in SQL Server


Before we look into how to implement hashing in SQL Server, we need to understand the difference between hashing and encryption.


Encryption VS Hashing:

Encryption its a reversible operation i.e., you can decrypt the encrypted value with a key.
Hashing value cannot decrypted back to its original value.


HASHBYTES:

HASHBYTES function in SQL Server returns the hash value of a given input string.

this function uses following algorithms to hash the value.

  1. MD2
  2. MD4
  3. MD5
  4. SHA
  5. SHA1
  6. SHA2
  7. SHA2_256(Applies to SQL Server 2012 to SQL Server 2014)
  8. SHA2_512(Applies to SQL Server 2012 to SQL Server 2014)
this function returns type is Varbinary

syntax:


HASHBYTES ( '<algorithm>', { @input | 'input' } )

Example:

 DECLARE @HashThis nvarchar(4000);
 SET @HashThis = CONVERT(nvarchar(4000),'pranay1231232');
 SELECT HASHBYTES('SHA1', @HashThis);

 Result:


 0x52A99EAE34513AB695801F775D9F9FE2EEBBF3DB



















we can use this function while storing the User Sensitive data.This is the much simple approach than TDE.


 


No comments:

Post a Comment