【问题标题】:C SHA1() open file and get hash error when compilingC SHA1() 打开文件并在编译时出现哈希错误
【发布时间】:2016-05-12 22:17:54
【问题描述】:

我正在编写一个程序来获取一些文件的哈希值并对它们进行处理。我正在尝试提取哈希等,但是在编译时出现错误。

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

int main ( int argc, char *argv[] ) {

    // para digitar em vez de   //char str[50] = {0};   //scanf("Enter file name:%s", str);     //scanf     if ( argc < 1 ) /* argc should be at least 2 for correct execution */
     {
        printf( "falta o ficheiro para ter hash: %s filename", argv[0] );
        }
        else
        {
        // filename to open
        FILE *file = fopen( argv[1], "r" );     //char str[] = "teste";     //const char str[] = "Original String";     unsigned char hash[SHA_DIGEST_LENGTH]; // == 20

    SHA1(FILE, sizeof(FILE), hash);

    printf("SHA1 of %s is %s\n", argv[1], hash);

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }

    }
    return 0; }

编译时出现此错误:gcc sob.c -o sha -lcrypto

sob.c: In function ‘main’:
sob.c:24:7: error: expected expression before ‘FILE’
  SHA1(FILE, sizeof(FILE), hash);
       ^
sob.c:24:7: error: too few arguments to function ‘SHA1’
In file included from sob.c:2:0:
/usr/include/openssl/sha.h:126:16: note: declared here
 unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md);
                ^

【问题讨论】:

  • 错误消息告诉您您正在使用不正确的(数量)参数调用函数。您应该传递一个字符缓冲区作为第一个参数;您正在尝试传递类型名称。即使您将FILE 更改为file,它也无法正常工作:您无法将文件流传递给函数——您必须从文件中读取数据并将数据传递给函数。我不确定第三个参数的用途;你需要阅读函数的manual page
  • ty,我可以直接发送。

标签: c sha1


【解决方案1】:

您不能将文件(或FILE,它是一种类型,而不是变量)传递给 SHA1,而是需要将文件读入无符号字符数组并传入该数组!

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2019-05-24
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多