問題描述
我想在數據庫中存儲散列密碼(使用 BCrypt).什么是好的類型,哪個是正確的長度?使用 BCrypt 散列的密碼是否總是長度相同?
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?
編輯
示例哈希:
$2a$10$KssILxWNR6k62B7yiX0GAe2Q7wwHlrzhF3LqtVvpyvHZf0MwvNfVu
在對一些密碼進行散列后,BCrypt 似乎總是生成 60 個字符的散列.
After hashing some passwords, it seems that BCrypt always generates 60 character hashes.
編輯 2
抱歉沒有提到實現.我正在使用 jBCrypt.
Sorry for not mentioning the implementation. I am using jBCrypt.
推薦答案
bcrypt 的模塊化 crypt 格式包括
The modular crypt format for bcrypt consists of
$2$
、$2a$
或$2y$
標識 散列算法和格式- 表示成本參數的兩位數值,后跟
$
- 一個 53 個字符長的 base-64 編碼值(它們使用字母
.
、/
、0
–9
,A
–Z
,a
–z
與標準 Base 64 編碼 字母表)包括:- salt 的 22 個字符(實際上只有 132 個解碼位中的 128 個位)
- 31 個字符的加密輸出(實際上只有 186 個解碼位中的 184 個位)
$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)
因此總長度分別為 59 或 60 字節.
Thus the total length is 59 or 60 bytes respectively.
當您使用 2a 格式時,您將需要 60 個字節.因此對于 MySQL,我建議使用
CHAR(60) BINARY
或BINARY(60)
(參見 _bin 和 binary 排序規則 以了解區別).As you use the 2a format, you’ll need 60 bytes. And thus for MySQL I’ll recommend to use the
CHAR(60) BINARY
orBINARY(60)
(see The _bin and binary Collations for information about the difference).CHAR
不是二進制安全的,相等性不僅取決于字節值,還取決于實際的排序規則;在最壞的情況下,A
被視為等于a
.參見_bin
和 <代碼>二進制排序規則了解更多信息.CHAR
is not binary safe and equality does not depend solely on the byte value but on the actual collation; in the worst caseA
is treated as equal toa
. See The_bin
andbinary
Collations for more information.這篇關于我應該使用什么列類型/長度來在數據庫中存儲 Bcrypt 哈希密碼?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!