HASHBYTES (Transact-SQL)
Returns the MD2, MD4, MD5, SHA, or SHA1 hash of its input.
- 4/4/2009
- Mike C_1
- 5/16/2012
- Chandan Motwani
Function based on:
http://support.microsoft.com/kb/104829
CREATE function [dbo].[hexchar](
@binvalue varbinary(255)
) returns varchar(255)
as begin
declare @charvalue varchar(255)
declare @i int
declare @length int
declare @hexstring char(16)
select @charvalue = ''
select @i = 1
select @length = datalength(@binvalue)
select @hexstring = '0123456789ABCDEF'
while (@i <= @length)
begin
declare @tempint int
declare @firstint int
declare @secondint int
select @tempint = convert(int, substring(@binvalue,@i,1))
select @firstint = floor(@tempint/16)
select @secondint = @tempint - (@firstint*16)
select @charvalue = @charvalue + substring(@hexstring, @firstint+1, 1) + substring(@hexstring, @secondint+1, 1)
select @i = @i + 1
end
return @charvalue
end
- 2/23/2010
- Mark OSVATH
- 4/7/2010
- Stanley Roark
I'm siting the following MSDN article on REPLICATE(): http://msdn.microsoft.com/en-us/library/ms174383.aspx
REPLICATE() truncates its VARCHAR returns to 8000 characters. Consider the following code.
DECLARE @v varchar(max);
SET @v = REPLICATE ('A', 9000); -- Input longer than 8,000 bytes
SELECT LEN(@v) AS VarcharLength, HashBytes('SHA1', @v) AS HashValue;
SET @v = REPLICATE ('A', 8000); -- Input exactly 8,000 bytes
SELECT LEN(@v) AS VarcharLength, HashBytes('SHA1', @v) AS HashValue;
The output is as follows:
VarcharLength HashValue
8000 0xD2967D6425E56C18BA979EEFB4E0DBD1269D9BC9
VarcharLength HashValue
8000 0xD2967D6425E56C18BA979EEFB4E0DBD1269D9BC9
You will see that the hash values are the same because the input is truncated to 8000 characters by REPLICATE() and that it is not HashBytes() that is truncating the value.
To see the error for yourself try the following code sample:
DECLARE @v varchar(max);
SET @v = REPLICATE ('A', 8000);
SET @v = @v + 'A';
SELECT HashBytes('SHA1', @v)
To avoid the 'String or binary data would be truncated.' error, make sure you trap for any VARCHAR whose LEN is greater than 8000.
- 3/25/2010
- faamasani_smalltime_dba
- 3/25/2010
- faamasani_smalltime_dba
- What *IS* a HASHBYTE?
- Why would I use it?
- What the heck are MD2, MD4, MD5, SHA and SHA1?
I ran across a comment about hashbytes and didn't know what it was (yes, I'm a SQL programmer) and so I looked it up here, and I still don't know what it is!
- 1/1/2010
- DanThMan
Truncation effect on input longer than 8,000 bytes.
DECLARE @v varchar(max);
SET @v = REPLICATE ('A', 9000); -- Input longer than 8,000 bytes
SELECT HashBytes('SHA1', @v);
SET @v = REPLICATE ('A', 8000); -- Input exactly 8,000 bytesSELECT HASHBYTES('SHA1', @v);
The output, shown below, is the same hash value twice. This indicates that the two strings that were actually hashed were exactly the same.
0xD2967D6425E56C18BA979EEFB4E0DBD1269D9BC9
0xD2967D6425E56C18BA979EEFB4E0DBD1269D9BC9
- 6/8/2009
- Mike C_1
- 4/4/2009
- Mike C_1
- 2/9/2009
- Omer van Kloeten
- 3/6/2009
- Thomas Lee
