shadow 可接受的哈希值

shadow 可接受的哈希值

/etc/shadow 接受哪些哈希算法?

我曾尝试使用从 PHP 生成密码哈希password_hash("password1234", PASSWORD_DEFAULT);,但是当我将用户密码替换为使用 PHP 生成的哈希所提供的哈希时,我尝试进行身份验证时,这似乎不起作用。

为什么 blowfish 不起作用?(我认为 PHP 默认使用 blowfish)

答案1

使用库函数对密码进行哈希处理crypt(3);请参阅手册页crypt(3). 该函数根据格式选择加密函数

  • $1$salt_chars$:使用 MD5
  • $2a$salt_chars$:使用 Blowfish(非标准,很可能不可用)
  • $5$salt_chars$:使用 SHA-256
  • $6$salt_chars$:使用 SHA-512
  • 不在表格中$类型$salt_chars$:像以前一样使用 DES。

我不知道您是否可以使用 PHP 来模拟确切的操作crypt(3)

答案2

我曾尝试使用 PHP 生成密码哈希password_hash("password1234", PASSWORD_DEFAULT)......

然后,您需要以期望的格式保存输出/etc/shadow,正如 AlexP 所解释的那样:

$<method>$<salt>$<hash>

password_hash的输出有所不同。它返回:

$<method>$<cost>$<hash>

例如,对于我来说,密码a( password_hash("a", PASSWORD_DEFAULT)) 的输出为:

$2y$10$w4hgkTWvE37igKd9TMn8xOcCNT/L/lojOEkqaPyIW4qdyAp92GmHC

奇怪的是,盐也是哈希的一部分:

password_hash("a", PASSWORD_DEFAULT, ['salt'=>'abcdefghijklmnopqrstuv'])
// $2y$10$abcdefghijklmnopqrstuuj/LkVFwQAC6H0GkC0f1Bcmj82rKvDn.

shadow版本是:

$6$OaeCC6PI$qGI4OTUD/seGOvJE.ckkrWMoqiVQBf8EXtQFto7MsKP8TyTCxPXPF66csX3c4ljdRnjM1U8W65EpwUgMF.4qf0

看到第二栏里的盐了吗?

无论如何,我不知道 PHP 在这里做什么。你应该使用crypt,但我不是 PHP 专家,这是 SO 的一个问题。

此外,对我来说,man crypt报告2y不理解它:

If salt is a character string starting with the characters "$id$"  fol-
lowed by a string terminated by "$":

      $id$salt$encrypted

then  instead  of  using  the DES machine, id identifies the encryption
method used and this then determines  how  the  rest  of  the  password
string is interpreted.  The following values of id are supported:

      ID  | Method
      ---------------------------------------------------------
      1   | MD5
      2a  | Blowfish (not in mainline glibc; added in some
          | Linux distributions)
      5   | SHA-256 (since glibc 2.7)
      6   | SHA-512 (since glibc 2.7)

相关内容