SQL 将哈希字节转换为 VarChar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:03:58  来源:igfitidea点击:

Convert HashBytes to VarChar

sqlsql-server

提问by GateKiller

I want to get the MD5 Hash of a string value in SQL Server 2005. I do this with the following command:

我想在 SQL Server 2005 中获取字符串值的 MD5 哈希值。我使用以下命令执行此操作:

SELECT HashBytes('MD5', 'HelloWorld')

However, this returns a VarBinary instead of a VarChar value. If I attempt to convert 0x68E109F0F40CA72A15E05CC22786F8E6into a VarChar I get há e?§*à\?'???instead of 68E109F0F40CA72A15E05CC22786F8E6.

但是,这将返回 VarBinary 而不是 VarChar 值。如果我尝试转换0x68E109F0F40CA72A15E05CC22786F8E6为 VarChar,我会得到há e?§*à\?'???而不是68E109F0F40CA72A15E05CC22786F8E6.

Is there any SQL-based solution?

有没有基于 SQL 的解决方案?

Yes

是的

回答by GateKiller

I have found the solution else where:

我在其他地方找到了解决方案:

SELECT SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', 'HelloWorld')), 3, 32)

回答by Rapscallion

SELECT CONVERT(NVARCHAR(32),HashBytes('MD5', 'Hello World'),2)

回答by Xaqron

Use master.dbo.fn_varbintohexsubstring(0, HashBytes('SHA1', @input), 1, 0)instead of master.dbo.fn_varbintohexstrand then substringingthe result.

使用master.dbo.fn_varbintohexsubstring(0, HashBytes('SHA1', @input), 1, 0)而不是master.dbo.fn_varbintohexstr然后substringing结果。

In fact fn_varbintohexstrcalls fn_varbintohexsubstringinternally. The first argument of fn_varbintohexsubstringtells it to add 0xFas the prefix or not. fn_varbintohexstrcalls fn_varbintohexsubstringwith 1as the first argument internaly.

实际上是内部fn_varbintohexstr调用fn_varbintohexsubstring。的第一个参数fn_varbintohexsubstring告诉它是否添加0xF前缀。fn_varbintohexstr调用fn_varbintohexsubstringwith1作为内部的第一个参数。

Because you don't need 0xF, call fn_varbintohexsubstringdirectly.

因为不需要0xFfn_varbintohexsubstring直接调用。

回答by Timo Riikonen

Contrary to what David Knightsays, these two alternatives return the same response in MS SQL 2008:

David Knight所说的相反,这两种选择在 MS SQL 2008 中返回相同的响应:

SELECT CONVERT(VARCHAR(32),HashBytes('MD5', 'Hello World'),2)
SELECT UPPER(master.dbo.fn_varbintohexsubstring(0, HashBytes('MD5', 'Hello World'), 1, 0))

So it looks like the first one is a better choice, starting from version 2008.

所以看起来第一个是更好的选择,从 2008 版开始。

回答by Ramans

convert(varchar(34), HASHBYTES('MD5','Hello World'),1)

(1 for converting hexadecimal to string)

(1 用于将十六进制转换为字符串)

convert this to lower and remove 0x from the start of the string by substring:

将其转换为较低并通过子字符串从字符串的开头删除 0x:

substring(lower(convert(varchar(34), HASHBYTES('MD5','Hello World'),1)),3,32)

exactly the same as what we get in C# after converting bytes to string

与我们在 C# 中将字节转换为字符串后得到的完全相同

回答by Simon Jones

With personal experience of using the following code within a Stored Procedure which Hashed a SP Variable I can confirm, although undocumented, this combination works 100% as per my example:

根据在对 SP 变量进行哈希处理的存储过程中使用以下代码的个人经验,我可以确认,尽管没有记录,但根据我的示例,这种组合 100% 有效:

@var=SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('SHA2_512', @SPvar)), 3, 128)

回答by anopres

Changing the datatype to varbinary seems to work the best for me.

将数据类型更改为 varbinary 似乎最适合我。