简体   繁体   English

OpenSSL & C - 使用 SHA256 或 SHA512 的哈希密码

[英]OpenSSL & C - Hash Passwords w/ SHA256 or SHA512

I've tried my best reading over the docs but they seem very sparing in information (maybe I'm looking in the wrong place?)我已经尽最大努力阅读文档,但它们的信息似乎非常少(也许我找错了地方?)

I'm trying to create a password hasher in C using OpenSSL lib in which the program can be called and passed arguments such as the ending length of the hashed password, salt length, and the HMAC used (SHA256 or 512).我正在尝试使用 OpenSSL lib 在 C 中创建一个密码散列器,其中可以调用程序并传递参数,例如散列密码的结束长度、salt 长度和使用的 HMAC(SHA256 或 512)。 There just isn't a lot of info on how to utilize the API to do this.关于如何利用 API 来做到这一点的信息并不多。

The biggest problem I see is that there is a function called PKCS5_PBKDF2_HMAC_SHA1 , but I can't find one similar for 256 or 512.. Is only SHA1 available via OpenSSL API?我看到的最大问题是有一个名为PKCS5_PBKDF2_HMAC_SHA1的函数,但我找不到 256 或 512 的类似函数。是否只有 SHA1 可通过 OpenSSL API 使用?

Any guidance is much appreciated.非常感谢任何指导。

You can use PKCS5_PBKDF2_HMAC , which allows you to target a specific digest algorithm.您可以使用PKCS5_PBKDF2_HMAC ,它允许您针对特定的摘要算法。

int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, 
    const unsigned char *salt, int saltlen, 
    int iter, const EVP_MD *digest, // <<==== HERE
    int keylen, unsigned char *out);

A simple example appears below, which generates a random salt, then creates a PBK from "password", the generated salt, and EVP_sha256()下面是一个简单的例子,它生成一个随机盐,然后根据“密码”、生成的盐和EVP_sha256()创建一个 PBK

#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/bio.h>

int main(int argc, char *argv[])
{
    int iter = 1007;

    unsigned char salt[32] = {0};
    RAND_bytes(salt, sizeof(salt));

    unsigned char key[32] = {0};
    PKCS5_PBKDF2_HMAC("password", 8,
        salt, sizeof(salt),
        iter, EVP_sha256(),
        sizeof(key), key);

    BIO *bio = BIO_new_fp(stdout, BIO_NOCLOSE);
    BIO_dump(bio, (const char*)salt, sizeof(salt));
    BIO_dump(bio, (const char*)key, sizeof(key));
    BIO_free(bio);
}

Output (varies)输出(变化)

0000 - a7 ca ac f4 43 b0 2d 48-2b f6 d5 67 7e d2 5c b4   ....C.-H+..g~.\.
0010 - c5 82 1d 4d b1 00 cd 1e-85 91 77 4c 32 3e f3 c8   ...M......wL2>..
0000 - 48 8f be 5a e9 1c 9e 11-d8 95 cb ed 6d 6f 36 a2   H..Z........mo6.
0010 - 38 e6 db 95 e1 d7 a6 c0-8a 2f 3a f6 e1 74 e9 b9   8......../:..t..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM