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

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

问题描述

我不确定密码哈希是如何工作的(稍后会实现),但现在需要创建数据库架构.

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

我正在考虑将密码限制为 4-20 个字符,但据我了解,加密后的哈希字符串长度会有所不同.

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?

推荐答案

更新:仅使用哈希函数不足以存储密码.您应该阅读Gilles 在此线程上的回答以获得更详细的解释.

Update: Simply using a hash function is not strong enough for storing passwords. You should read the answer from Gilles on this thread for a more detailed explanation.

对于密码,请使用增强密钥的哈希算法,例如 Bcrypt 或 Argon2i.例如,在 PHP 中,使用 password_hash() 函数,默认使用 Bcrypt.

For passwords, use a key-strengthening hash algorithm like Bcrypt or Argon2i. For example, in PHP, use the password_hash() function, which uses Bcrypt by default.

$hash = password_hash("rasmuslerdorf", PASSWORD_DEFAULT);

结果是一个类似于以下的 60 个字符的字符串(但数字会有所不同,因为它会生成唯一的盐).

The result is a 60-character string similar to the following (but the digits will vary, because it generates a unique salt).

$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a

使用 SQL 数据类型 CHAR(60) 来存储此 Bcrypt 哈希编码.请注意,此函数不会编码为十六进制数字字符串,因此我们无法轻松地将其解开以存储为二进制.

Use the SQL data type CHAR(60) to store this encoding of a Bcrypt hash. Note this function doesn't encode as a string of hexadecimal digits, so we can't as easily unhex it to store in binary.

其他哈希函数仍然有用,但不能用于存储密码,所以我将保留下面的原始答案,写于 2008 年.

Other hash functions still have uses, but not for storing passwords, so I'll keep the original answer below, written in 2008.

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

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 生成一个 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)

截至 2015 年,NIST 建议使用 SHA-256或更高 用于需要互操作性的散列函数的任何应用程序.但 NIST 不建议使用这些简单的哈希函数来安全地存储密码.

As of 2015, NIST recommends using SHA-256 or higher for any applications of hash functions requiring interoperability. But NIST does not recommend using these simple hash functions for storing passwords securely.

较小的散列算法有其用途(如应用程序内部,而不是用于交换),但它们是 已知可破解.

Lesser hashing algorithms have their uses (like internal to an application, not for interchange), but they are known to be crackable.

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

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