Generate MD5 hash string with T-SQL

2020-01-29 03:21发布

Is there a way to generate MD5 Hash string of type varchar(32) without using fn_varbintohexstr

SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', 'email@dot.com')), 3, 32)

So it could be used inside a view with SCHEMABINDING

8条回答
对你真心纯属浪费
2楼-- · 2020-01-29 03:57
declare @hash nvarchar(50)
--declare @hash varchar(50)

set @hash = '1111111-2;20190110143334;001'  -- result a5cd84bfc56e245bbf81210f05b7f65f
declare @value varbinary(max);
set @value = convert(varbinary(max),@hash);


select  
 SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5', '1111111-2;20190110143334;001')),3,32) as 'OK'
,SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5', @hash)),3,32) as 'ERROR_01'
,SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5',convert(varbinary(max),@hash))),3,32) as 'ERROR_02'
,SUBSTRING(sys.fn_sqlvarbasetostr(sys.fn_repl_hash_binary(convert(varbinary(max),@hash))),3,32)
,SUBSTRING(sys.fn_sqlvarbasetostr(master.sys.fn_repl_hash_binary(@value)),3,32)
查看更多
趁早两清
3楼-- · 2020-01-29 04:00

try this:

select SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5',  'email@dot.com' )),3,32) 
查看更多
smile是对你的礼貌
4楼-- · 2020-01-29 04:02

For data up to 8000 characters use:

CONVERT(VARCHAR(32), HashBytes('MD5', 'email@dot.com'), 2)

Demo

For binary data (without the limit of 8000 bytes) use:

CONVERT(VARCHAR(32), master.sys.fn_repl_hash_binary(@binary_data), 2)

Demo

查看更多
爷、活的狠高调
5楼-- · 2020-01-29 04:06
CONVERT(VARCHAR(32), HashBytes('MD5', 'email@dot.com'), 2)
查看更多
神经病院院长
6楼-- · 2020-01-29 04:08

Solution:

SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5','your text')),3,32)
查看更多
Deceive 欺骗
7楼-- · 2020-01-29 04:16

Use HashBytes

SELECT HashBytes('MD5', 'email@dot.com')

That will give you 0xF53BD08920E5D25809DF2563EF9C52B6

-

SELECT CONVERT(NVARCHAR(32),HashBytes('MD5', 'email@dot.com'),2)

That will give you F53BD08920E5D25809DF2563EF9C52B6

查看更多
登录 后发表回答