如何获得与Linux crypt和salt输出相同的结果?

时间:2018-08-20 17:53:18

标签: hash openssl crypt

我在Ubuntu计算机“ openssl passwd -crypt-salt pass book”上使用了以下命令来生成加盐密码。

输出由什么哈希组成?例如SHA-512,MD5等。此外,我想知道它是如何组成的。例如,是通过将“存折”一起哈希在一起制成的吗?

我需要有关使用什么哈希/算法来生成输出的更多信息。

谢谢

1 个答案:

答案 0 :(得分:0)

使用openssl passwd算法时,-crypt应用程序提供的结果似乎与Linux / Unix crypt()函数提供的结果相同。您可以使用以下(快速的)肮脏代码段对此进行验证:

#include <crypt.h>
#include <stdio.h>

int main(
  int argc,
  char **argv)
{
  char *key = argv[1];
  char *salt = argv[2];
  char *enc = crypt(key, salt);
  printf("key = \"%s\", salt = \"%s\", enc = \"%s\"\n",
    key ? key:"NULL", salt ? salt:"NULL", enc ? enc:"NULL");
}

结果:

$ ./main book pass
key = "book", salt = "pass", enc = "pahzZkfwawIXw"
$ openssl passwd -crypt -salt pass book
pahzZkfwawIXw

its OSX man page中最清楚地解释了crypt()函数的确切细节,尤其是:

Traditional crypt:
  The first 8 bytes of the key are null-padded, and the low-order 7 bits of each character is
  used to form the 56-bit DES key.

  The salt is a 2-character array of the ASCII-encoded salt.  Thus, only 12 bits of salt are
  used.  count is set to 25.

Algorithm:
  The salt introduces disorder in the DES algorithm in one of 16777216 or 4096 possible ways
  (ie. with 24 or 12 bits: if bit i of the salt is set, then bits i and i+24 are swapped in
  the DES E-box output).

  The DES key is used to encrypt a 64-bit constant, using count iterations of DES.  The value
  returned is a null-terminated string, 20 or 13 bytes (plus null) in length, consisting of
  the salt, followed by the encoded 64-bit encryption.