关于密码哈希算法BCrypt的编码结果各部分意义分析及其他注意事项

发布时间 2023-12-26 10:05:29作者: dirgo

找到一个英文的解析:

The bcrypt standard makes storing salts easy - everything it needs to check a password is stored in the output string.

The prefix "$2a$" or "2y" in a hash string in a shadow password file indicates that hash string is a bcrypt hash in modular crypt format. The rest of the hash string includes the cost parameter, a 128-bit salt (base-64 encoded as 22 characters), and the 192-bit[dubious – discuss] hash value (base-64 encoded as 31 characters).

结合例子大体意思翻译一下:

编码结果示例:$2a$10$AxafsyVqK51p.s9WAEYWYeIY9TKEoG83LTEOSB3KUkoLtGsBKhCwe

BCrypt编码后结果一共60字符,2a和2y(php漏洞修复版)表示采用的哈希算法,再后面$10$中间的10表示迭代次数,越多则耗资源越多,再后面是base64位编码的22个字符,是128bit大小的盐;再后面是base64编码的31个字符,是192bit的哈希值

 

其他注意事项:

1.目前BCrypt是密码编码存储的主流算法,也是spring security框架默认采用的,个人认为是平衡了兼容性和安全性的结果,当然不是最安全的,如果非常在意安全性,则应采用Argon2,但这个算法似乎暂时没有js实现;

2.开始自己对128bit的盐是22个字符这个疑惑了很久,后来仔细研究了一下base64编码才明白,base64一个字符只需要6bit,所以128bit是完全够的

其他参考文章:

python - python bcrypt中'$ 2a 的意义是什么?_Stack Overflow中文网的意义是什么?_Stack Overflow中文网">python - python bcrypt中'$ 2a 的意义是什么?_Stack Overflow中文网

使用 bcryptjs 对密码做加密-腾讯云开发者社区-腾讯云

密码的加密加盐处理-腾讯云开发者社区-腾讯云