我应该使用哪种列类型/长度来存储数据库中的Bcrypt哈希密码? [英] What column type/length should I use for storing a Bcrypt hashed password in a Database?
问题描述
我想在数据库中存储散列密码(使用BCrypt)。对于这一点来说,什么是一个好的类型,哪一个会是正确的长度? BCrypt的密码长度是否一样?
编辑
示例hash :
$ 2a $ 10 $ KssILxWNR6k62B7yiX0GAe2Q7wwHlrzhF3LqtVvpyvHZf0MwvNfVu
After散列一些密码,似乎BCrypt总是产生60个字符哈希。
编辑2
抱歉没有提及实施。我正在使用 jBCrypt 。
bcrypt的模块化crypt格式包含
$ 2 $
$ 2a $
或 $ 2y $
标识散列算法和格式 $
。
, /
, 0
- 9
, A
- Z
, a
- z
与标准Base 64 Encoding 字母不同)包含:- 盐的22个字符(实际上只有132个解码位的128位)
- 31个字符的加密输出(有效地仅186个解码比特中的184个比特)
$ b 长度分别为59或60字节。
当您使用2a格式时,您需要60个字节。因此对于MySQL我建议使用 CHAR(60)BINARY
或 BINARY(60)
(请参阅 _bin 和二进制 Collations 关于差异)。
$ b $ CHAR
不是二进制安全的,并且相等不仅仅取决于字节值,而是取决于实际整理;在最坏的情况下 A
被视为等于 a
。请参阅 _bin
和二进制
排序规则以获取更多信息。
I want to store a hashed password (using BCrypt) in a database. What would be a good type for this, and which would be the correct length? Are passwords hashed with BCrypt always of same length?
EDIT
Example hash:
$2a$10$KssILxWNR6k62B7yiX0GAe2Q7wwHlrzhF3LqtVvpyvHZf0MwvNfVu
After hashing some passwords, it seems that BCrypt always generates 60 character hashes.
EDIT 2
Sorry for not mentioning the implementation. I am using jBCrypt.
The modular crypt format for bcrypt consists of
$2$
,$2a$
or$2y$
identifying the hashing algorithm and format- a two digit value denoting the cost parameter, followed by
$
- a 53 characters long base-64-encoded value (they use the alphabet
.
,/
,0
–9
,A
–Z
,a
–z
that is different to the standard Base 64 Encoding alphabet) consisting of:- 22 characters of salt (effectively only 128 bits of the 132 decoded bits)
- 31 characters of encrypted output (effectively only 184 bits of the 186 decoded bits)
Thus the total length is 59 or 60 bytes respectively.
As you use the 2a format, you’ll need 60 bytes. And thus for MySQL I’ll recommend to use the CHAR(60) BINARY
or BINARY(60)
(see The _bin and binary Collations for information about the difference).
CHAR
is not binary safe and equality does not depend solely on the byte value but on the actual collation; in the worst case A
is treated as equal to a
. See The _bin
and binary
Collations for more information.
这篇关于我应该使用哪种列类型/长度来存储数据库中的Bcrypt哈希密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!