Openssl Sha1 编译问题

Openssl Sha1 compile issue

本文关键字:问题 编译 Sha1 Openssl      更新时间:2023-10-16
#include <algorithm>
#include <stdio.h>
#include <openssl/sha.h>
using namespace std;
int main()
{
    unsigned char ibuf[] = "compute sha1";
    unsigned char obuf[20];
    SHA1(ibuf, strlen(ibuf), obuf);
    int i;
    for (i = 0; i < 20; i++) {
        printf("%02x ", obuf[i]);
    }
    printf("n");
}

g++ file.cpp -o file -l libssl

file.cpp: In function ‘int main()’:
file.cpp:29:27: error: invalid conversion from ‘unsigned char*’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:399:15: error:   initializing argument 1 of ‘size_t strlen(const char*)’ [-fpermissive]

想知道怎么了..我正在尝试计算 sha1

首先,我想知道它是否在匿名化中丢失了,但在我看来缺少

include <string.h>

命令行应该看起来更像:

g++ file.cpp -o file -lssl

您正在使用C++编译器。C++编译器通常对类型非常严格。您已经定义了要unsigned char ibuf(并且在 strlen 中使用时它被视为 unsigned char * (,并且strlen期望const char*,因此会产生错误。

您有以下选择:

  1. 您可以在strlen中投ibuf

    SHA1(ibuf, strlen((const char *)ibuf), obuf);
    
  2. 您可以使用建议的-fpermissive标志使g++更加宽容,并将错误转换为纯粹的警告,尽管我不建议这样做:

    g++ -fpermissive file.cpp -o file -lssl
    
  3. 由于代码看起来就像一个普通的C,也许你不需要C++编译器。如果是这种情况,只需使用 C 编译器而不是 C++

    gcc file.cpp -o file -lssl
    

    然后,您需要删除include <algorithm>namespace..