简体   繁体   English

应该使用什么样的数据类型来存储哈希值?

[英]What kind of datatype should one use to store hashes?

I understand that hashes will be different based on different datatypes in SQL Server. 据我所知,基于SQL Server中的不同数据类型,哈希值会有所不同。 One support Unicode another not .... so on (also collation) 一个支持Unicode另一个不....所以(也整理)

I am using char(32) as a datatype but the output is weird. 我使用char(32)作为数据类型,但输出很奇怪。 Using this 用这个

select HASHBYTES('MD5','MD5Text')

gives this ouput: 给出这个输出:

0xA891DB2DA259280A66FD5F35201CAB6A

and when 什么时候

declare @h char(32)
select @h=HASHBYTES('MD5','MD5Text')
select @h,LEN(@h)

output: 输出:

Ё'Ы-ўY( fэ_5 «j Ё'Ы-ўY(fэ_5«j

So I am new to SQL Server. 所以我是SQL Server的新手。
Could anyone, please, tell me what datatype should I use to store hashes ?? 请问任何人,请告诉我应该用什么数据类型来存储哈希?

You should use the binary datatype. 您应该使用binary数据类型。 You can use binary instead of varbinary because the hash function will always return the same number of bytes for the same type of hash (eg MD5 , SHA1 , etc.). 您可以使用binary而不是varbinary因为哈希函数将始终为相同类型的哈希返回相同的字节数(例如, MD5SHA1等)。 This will cut down on the (slight) overhead required to manage a variable length binary ( varbinary ) column. 这将减少管理可变长度二进制( varbinary )列所需的(轻微)开销。

In terms of what size to make it, you can run this query to check the length of each hash type: 就它的大小而言,您可以运行此查询来检查每种哈希类型的长度:

SELECT  DATALENGTH(HASHBYTES('MD2', 'Testing')) AS [MD2Length],
        DATALENGTH(HASHBYTES('MD4', 'Testing')) AS [MD4Length],
        DATALENGTH(HASHBYTES('MD5', 'Testing')) AS [MD5Length],
        DATALENGTH(HASHBYTES('SHA', 'Testing')) AS [SHALength],
        DATALENGTH(HASHBYTES('SHA1', 'Testing')) AS [SHA1Length],
        /* 2012 only: */
        DATALENGTH(HASHBYTES('SHA2_256', 'Testing')) AS [SHA2_256Length],
        DATALENGTH(HASHBYTES('SHA2_512', 'Testing')) AS [SHA2_512Length];

And it should come out with this: 它应该是这样的:

MD2Length MD4Length MD5Length SHALength SHA1Length SHA2_256Length SHA2_512Length
--------- --------- --------- --------- ---------- -------------- --------------
16        16        16        20        20         32             64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM