首页 > 解决方案 > 在 C 中验证 SHA256(python 生成)

问题描述

我正在尝试使用openssl/sha.h在C代码中使用passlib( https://passlib.readthedocs.io/en/stable/lib/passlib.hash.sha256_crypt.html )验证python生成的哈希。

对于密码“senhateste”,python passlib 生成了以下哈希:

$5$rounds=535000$e.cy1Y7iWc0OQjNC$7QbwABQMlBdHHd.UURPF.eoc0VTGOtM7yxxrOIe4OxC

其中有多个轮次、一个盐和一个校验和(以 $ 分隔)。

我试图在 C 中为原始密码生成一个哈希值,以与 Python 代码生成的密码进行比较,但我只得到一个字节数组(就像在线 sha256 哈希工具的那些)。

我正在考虑对 Pyhton 提供的相同轮数的密码进行循环哈希处理。但我不知道应该在哪里使用盐。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>

int main (void) {
     int i;
     unsigned char s[] = "senhateste";
     unsigned char *d = (unsigned char *) malloc(SHA256_DIGEST_LENGTH);

    SHA256(s,strlen((char*)s), d);

    for(i=0;i<SHA256_DIGEST_LENGTH;i++)
    {
        printf("%02x",d[i]);
    }
    printf("\n");

    free(d);
   return 0;
}

有什么建议吗?

标签: pythoncsha256

解决方案


推荐阅读