什么数据类型用于散列密码字段和什么长度? [英] What data type to use for hashed password field and what length?

查看:772
本文介绍了什么数据类型用于散列密码字段和什么长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道密码哈希是如何工作的(以后会实现),但现在需要创建数据库模式。



我想限制密码到4-20个字符,但是我理解加密后的哈希字符串将有不同的长度。



那么,如何将这些密码存储在数据库中?

解决方案

这取决于你使用的哈希算法。散列总是产生相同长度的结果,而不管输入。通常将二进制散列结果以文本形式表示为一系列十六进制数字。或者,您可以使用 UNHEX () 函数将十六进制数字字符串减半。




  • MD5生成128位哈希值。您可以使用CHAR(32)或BINARY(16)

  • SHA-1生成一个160位的哈希值。您可以使用CHAR(40)或BINARY(20)

  • SHA-224生成224位的哈希值。您可以使用CHAR(56)或BINARY(28)

  • SHA-256生成256位散列值。您可以使用CHAR(64)或BINARY(32)

  • SHA-384生成384位散列值。您可以使用CHAR(96)或BINARY(48)

  • SHA-512生成512位的哈希值。您可以使用CHAR(128)或BINARY(64)

  • BCrypt生成依赖于实现的448位散列值。 您可能需要CHAR(56), CHAR(60),CHAR(76),BINARY(56)或BINARY(60)



< -256或更高的密码。较小的散列算法有它们的用途,但它们是已知是可破解的



您应该您的密码在应用散列函数之前。批量密码不会影响散列结果的长度。


I'm not sure how password hashing works (will be implementing it later), but need to create database schema now.

I'm thinking of limiting passwords to 4-20 characters, but as I understand after encrypting hash string will be of different length.

So, how to store these passwords in the database?

解决方案

It depends on the hashing algorithm you use. Hashing always produces a result of the same length, regardless of the input. It is typical to represent the binary hash result in text, as a series of hexadecimal digits. Or you can use the UNHEX() function to reduce a string of hex digits by half.

  • MD5 generates a 128-bit hash value. You can use CHAR(32) or BINARY(16)
  • SHA-1 generates a 160-bit hash value. You can use CHAR(40) or BINARY(20)
  • SHA-224 generates a 224-bit hash value. You can use CHAR(56) or BINARY(28)
  • SHA-256 generates a 256-bit hash value. You can use CHAR(64) or BINARY(32)
  • SHA-384 generates a 384-bit hash value. You can use CHAR(96) or BINARY(48)
  • SHA-512 generates a 512-bit hash value. You can use CHAR(128) or BINARY(64)
  • BCrypt generates an implementation-dependent 448-bit hash value. You might need CHAR(56), CHAR(60), CHAR(76), BINARY(56) or BINARY(60)

NIST recommends using SHA-256 or higher for passwords. Lesser hashing algorithms have their uses, but they are known to be crackable.

You should salt your passwords before applying the hashing function. Salting a password does not affect the length of the hash result.

这篇关于什么数据类型用于散列密码字段和什么长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆