Passwords 返回支持的哈希算法

Passwords 返回支持的哈希算法,passwords,cryptography,shadow,pam,Passwords,Cryptography,Shadow,Pam,我需要一个命令或脚本在系统上返回支持的哈希算法(用于哈希密码),我的意思是算法可以与pam.d配置文件或login.defs一起使用 通常支持md5、bigcrypt、sha256、sha512和blowfish,但我需要通过编程检查是否支持新算法,并在脚本中确定它。我检查了/proc/crypto,但比我前面提到的要少 谢谢/proc/crypto只是内核知道的算法列表;这与帕姆无关 没有办法直接查询PAM来找出它可以支持什么散列;当然,它在内部知道这一点,但它没有被任何公共API公开 您可以

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

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


谢谢

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

没有办法直接查询PAM来找出它可以支持什么散列;当然,它在内部知道这一点,但它没有被任何公共API公开

您可以做的一件事是使用
crypt
并尝试使用各种id类型散列传递,本质上是探测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");
   }
#包括
#包括
#包括
布尔测试加密方法(常量字符*id)
{
常量std::字符串盐=
std::string(“$”)+id+“$”+“testsalt$”;
std::string crypt_result=::crypt(“password”,salt.c_str());
/*
*如果不支持哈希ID,则glibc
*然后将其视为旧式地下室,而不是
*失败;发现这种情况。
*/
如果(crypt_result.size()==13&&
crypt_结果[0]='$'&&
crypt_result.find('$',1)=std::string::npos)
返回false;
返回true;
}
int main()
{
if(测试加密方法(“1”))
printf(“md5”);
if(测试加密方法(“2a”))
printf(“河豚”);
if(test\u crypt\u method(“4”)//测试假阳性
printf(“未定义”);
if(测试加密方法(“5”))
printf(“sha256”);
if(测试加密方法(“6”))
printf(“sha512”);
printf(“\n”);
}