Home > SQL Server > SQL Server – Generate Hash value using MD and SHA algorithm – HASHBYTES

SQL Server – Generate Hash value using MD and SHA algorithm – HASHBYTES


Hashbytes() function returns the hash value for the given input using MD or SHA algorithm. The input data type must be a varchar, nvarchar or varbinary. The maximum size of the input is 8000 bytes.

Function syntax

Hashbytes(‘Algorithm’, input variable or string value)

Algorithm should be defined within the single quotes like ‘MD5’ and it is mandatory for this function.

Available algorithms

MD2, MD4  and MD5 :- These algorithms generates 128 bits (16 bytes) hash output.

SHA and SHA1 :- These algorithms generates 160 bits (20 bytes) hash output.

SHA2_256 :- This algorithm generates  256 bits (32 bytes) hash output.

SHA2_512 :- This algorithm generates and 512 bits (64 bytes) hash output.

Implementation

Generating hash value for a string with different algorithm

pic1Output

pic2You will get the same value in your machine. So, go execute this SQL Statement in your SQL Server.

A short story about algorithm’s security

These algorithms are the cryptographic hash function. Collisions has been found in these algorithms MD2, MD4, MD5, SHA and SHA1(Theoretically). So, It is no longer consider to be a secure algorithm. SH2 (256 and 512) has not been reported for any attack. So, it is consider to be a secure algorithm now. But, there are other more secure algorithms in development such as MD6 and SHA-3.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: