2010-10-12 73 views
2

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

一般MD5,bigcrypt,sha256,sha512和河豚都支持,但我需要以编程方式检查是否支持新算法,并确定它在我的script.i checked/proc/crypto,但是比我之前提到的太少

感谢

回答

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"); 
    }