2

我需要在系统上返回支持的哈希算法(用于哈希密码)的命令或脚本,我的意思是算法可以与 pam.d 配置文件或 login.defs 一起使用。

通常支持 md5、bigcrypt、sha256、sha512 和河豚,但我需要以编程方式检查是否支持新算法并在我的脚本中确定它。我检查了 /proc/crypto 但它比我之前提到的要少

谢谢

4

1 回答 1

2

/proc/crypto只是内核知道的算法列表;这与 PAM 无关。

没有办法直接查询 PAM 来找出它可以支持的哈希值;当然,它在内部知道这一点,但它不会被任何公共 API 公开。

您可以做的一件事是使用crypt并尝试使用各种 id 类型对 pass 进行哈希处理,本质上是探测 PAM(或更准确地说,探测 libc 的 crypt,PAM 将其用于隐藏密码)。简单的例子:

#include <unistd.h>
#include <stdio.h>
#include <string>

bool test_crypt_method(const char* id)
   {
   const std::string salt =
      std::string("$") + id + "$" + "testsalt$";

   std::string crypt_result = ::crypt("password", salt.c_str());

   /*
   * If the hash ID is not supported, glibc unfortunately
   * then treats it as a old-style DES crypt rather than
   * failing; find this situation.
   */
   if(crypt_result.size() == 13 &&
      crypt_result[0] == '$' &&
      crypt_result.find('$', 1) == std::string::npos)
      return false;

   return true;
   }

int main()
   {
   if(test_crypt_method("1"))
      printf("md5 ");
   if(test_crypt_method("2a"))
      printf("blowfish ");
   if(test_crypt_method("4")) // test for false positives
      printf("undefined ");
   if(test_crypt_method("5"))
      printf("sha256 ");
   if(test_crypt_method("6"))
      printf("sha512 ");
   printf("\n");
   }
于 2010-10-12T14:00:35.847 回答