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
Convert HashBytes to VarChar
提问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 0x68E109F0F40CA72A15E05CC22786F8E6
into a VarChar I get há e?§*à\?'???
instead of 68E109F0F40CA72A15E05CC22786F8E6
.
但是,这将返回 VarBinary 而不是 VarChar 值。如果我尝试转换0x68E109F0F40CA72A15E05CC22786F8E6
为 VarChar,我会得到há e?§*à\?'???
而不是68E109F0F40CA72A15E05CC22786F8E6
.
Is there any SQL-based solution?
有没有基于 SQL 的解决方案?
回答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_varbintohexstr
and then substringing
the result.
使用master.dbo.fn_varbintohexsubstring(0, HashBytes('SHA1', @input), 1, 0)
而不是master.dbo.fn_varbintohexstr
然后substringing
结果。
In fact fn_varbintohexstr
calls fn_varbintohexsubstring
internally. The first argument of fn_varbintohexsubstring
tells it to add 0xF
as the prefix or not. fn_varbintohexstr
calls fn_varbintohexsubstring
with 1
as the first argument internaly.
实际上是内部fn_varbintohexstr
调用fn_varbintohexsubstring
。的第一个参数fn_varbintohexsubstring
告诉它是否添加0xF
前缀。fn_varbintohexstr
调用fn_varbintohexsubstring
with1
作为内部的第一个参数。
Because you don't need 0xF
, call fn_varbintohexsubstring
directly.
因为不需要0xF
,fn_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 似乎最适合我。