简体   繁体   English

MySQL表中的64位密码哈希值

[英]64-bit Password Hashes in MySQL tables

I use this function for hashing my passwords: 我用这个函数来哈希我的密码:

// RETURNS: rAyZOnlNBxO2WA53z2rAtFlhdS+M7kec9hskSCpeL6j+WwcuUvfFbpFJUtHvv7ji   
base64_encode(hash_hmac('sha384', $str . SC_NONCE, SC_SITEKEY, true));

And I store hashes in char(64) field (MySQL -InnoDB). 我在char(64)字段(MySQL -InnoDB)中存储哈希值。

Should I use varchar(64) instead of char(64) ? 我应该使用varchar(64)而不是char(64)吗? Why? 为什么?

Edit: I changed sha256 with sha384 . 编辑:我用sha384更改了sha256 Because in this example, sha256 always returns 44 bytes for me. 因为在这个例子中,sha256总是为我返回44个字节。 Sorry for confusing. 抱歉令人困惑。 Now it's 64-bytes. 现在它是64字节。

varchars save storage by only using up to the length required. varchars仅使用最多所需的长度来节省存储空间。 If the 64 bit hash is always 64 then it makes no difference in terms of storage so probably char is just as good as varchar in this case. 如果64位散列总是64位,那么它在存储方面没有区别,所以在这种情况下char可能和varchar一样好。

If you have variable length data to store, then a varchar will save wasting unnecessary space. 如果要存储可变长度数据,则varchar将节省浪费不必要的空间。

You should use CHAR(64) since your hash is fixed in length. 您应该使用CHAR(64),因为您的哈希长度是固定的。 Using VARCHAR will add another byte, wasting space. 使用VARCHAR将添加另一个字节,浪费空间。

One of the alternative ways you can do that (if you actually care much about space) is to store the hash in the binary form. 你可以做的另一种方法(如果你真的非常关心空间)是以二进制形式存储哈希。 Some details of how to do that may be found here ; 有关如何做到这一点的一些细节可以在这里找到; you'd probably want BINARY(32) for a SHA-256 hash. 你可能想要一个SHA-256哈希的BINARY(32)。

Even though you are using a Base 64 encoded string, the result is not necessarily 64 bits in length. 即使您使用Base 64编码的字符串,结果也不一定是64位长度。 In this case, VARCHAR is better because the result can be shorter than 64 bits. 在这种情况下, VARCHAR更好,因为结果可能短于64位。

In fact as seen here , 64 bits is the maximum length rather than the set length. 在如图事实上这里 ,64位是最大长度,而不是设定长度。

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

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