无法在gcc中编译SHA1()函数。 c语言

时间:2014-09-24 14:45:00

标签: c linux string gcc

这是我的代码:

#include "openssl/sha.h"
#include "stdio.h"
#include "string.h"

unsigned char *SHA1(const unsigned char *d, unsigned long n, unsigned char *md);

int main(void)
{
  char data[] = "Hello, world!";

  size_t length = sizeof(data);

  unsigned char hash[SHA_DIGEST_LENGTH];

  SHA1(data, length, hash);

  return 0;
}

当我使用没有-Wall选项的gcc编译它时,程序运行良好并打印出一些字符串。当我使用-Wall时,我会遇到以下情况:

test.c: In function ‘main’:

test.c:11:3: warning: pointer targets in passing argument 1 of ‘SHA1’ differ in signedness [-Wpointer-sign]

   SHA1(data, length, hash);
   ^
In file included from test.c:1:0:

/usr/include/openssl/sha.h:126:16: note: expected ‘const unsigned char *’ but argument is of type ‘char *’

 unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md);

Can anyone help me, Thanks in advance.

1 个答案:

答案 0 :(得分:3)

您的程序无法编译,会产生警告。

在这种情况下,SHA1希望您传递unsigned char*,但是您传递了char *,您可以安全地将char *转换为unsigned char*,所以做

SHA1((unsigned char*)data, length, hash);