使用带有 unsigned char 的 SHA512 OpenSSL 获取警告

Getting warnings with SHA512 OpenSSL with unsigned char

我在 C 中使用 openssl 并收到一些我想摆脱的警告,但我不确定如何解决它,因为我发现的示例似乎在做我正在模拟的事情。

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

int main(int argc, char** argv)
{
    int i;
    unsigned char resSHA512[SHA512_DIGEST_LENGTH];
    unsigned char unhashed[1024];

    printf("Input string to hash: ");
    fgets(unhashed, sizeof(unhashed), stdin);
    unhashed[strcspn(unhashed, "\n")] = 0;

    SHA512(unhashed, strlen(unhashed), resSHA512);
    printf("\nSHA512: ");
    for ( i = 0; i < SHA512_DIGEST_LENGTH; i++ )
    {
        printf("%02x", resSHA512[i]);
    }
    printf("\n");
    return 0;
}

当我使用 openssl 在我的 Mac 上编译时,我还收到一个警告,即 SHA512 已被 open 弃用,我通过它的 git 存储库。我在 Linux 中没有收到警告,除非我将 -Wall 标志与 gcc 一起使用。

非常感谢任何帮助,因为我正在努力学习新事物!

谢谢

您读入 unhashed 的内容是一个字符串,因此您应该将其声明为 char 的数组。这将解决 string-related 函数的警告。

这会在调用 SHA512 时发出警告,它的第一个参数需要 unsigned char。在这种情况下,您可以安全地将参数转换为 unsigned char *.

您在 Mac 上看到的警告是因为整个 OpenSSL 库在 OS 上被标记为已弃用,以支持其自己的加密库。