Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 相同的内容,不同的MD5-文件和字符串_C++_C_Md5 - Fatal编程技术网

C++ 相同的内容,不同的MD5-文件和字符串

C++ 相同的内容,不同的MD5-文件和字符串,c++,c,md5,C++,C,Md5,我有一个文件testfile和一个字符串teststring 我在一个shell中写道: echo“a”>testfile 然后xxd测试文件 因此我可以看到我的filecontent的十六进制值 输出: 请参阅我的代码: int file; struct stat s; unsigned long size; char* buffer; char md5[MD5_DIGEST_LENGTH] file = open("testfile", O_RDONLY); if (file <

我有一个文件
testfile
和一个字符串
teststring

  • 我在一个shell中写道:
    echo“a”>testfile

  • 然后
    xxd测试文件

    因此我可以看到我的filecontent的十六进制值
    输出:

  • 请参阅我的代码:

    int file;
    struct stat s;
    unsigned long size;
    char* buffer;
    char md5[MD5_DIGEST_LENGTH]
    
    file = open("testfile", O_RDONLY);
    if (file < 0)
        return false;
    
    if (fstat(file, &s) < 0)
    {
        close(file);
        return false;
    }
    
    size = s.st_size;                       //GET FILE SIZE
    printf("filesize: %lu\n", size);        //PRINT FILESIZE FOR DEBUGGING
    buffer = (char*)mmap(0, size, PROT_READ, MAP_SHARED, file, 0); //MAP FILE CONTENT TO BUFFER
    MD5((unsigned char*)buffer, size, md5); //GENERATE MD5
    munmap(buffer, size);                   //UNMAP BUFFER
    close(file);
    
    for (int i = 0; i < MD5_DIGEST_LENGTH; i++)
        printf("%02x", md5[i]);
    printf("\n");
    
    
    unsigned char* teststring = "\x61\x0a"; //SAME STRING AS IN THE FILE
    
    MD5((unsigned char*)teststring, 2, md5);
    for (int i = 0; i < MD5_DIGEST_LENGTH; i++)
        printf("%02x", md5[i]);
    printf("\n");
    
    两个完全不同的md5哈希。
    我尝试将
    缓冲区
    写入文件
    并将
    teststring
    写入一个文件它们是相同的
    为什么?
    缓冲区是否与
    测试字符串
    相同


  • 正确的散列是您的第一个散列,
    60b725f10c9c85c70d97880dfe8191b3

    $ echo "a" | md5
    60b725f10c9c85c70d97880dfe8191b3
    
    第二个散列恰好是“\x64\x0a”的散列,或者是后跟换行符的字符“d”:

    $ echo "d" | md5
    e29311f6f1bf1af907f9ef9f44b8328b
    

    您确定您发布的代码是您正在编译/运行的代码吗?你忘了重新编译吗?您正在执行旧的二进制文件吗?

    以十六进制打印缓冲区/字符串(不是MD5),看看它们是否不同。如果它们相同,您可能需要以不同的方式使用md5函数(某些实现要求您完成md5操作),如果您颠倒这两种方法的顺序,结果是否相同?(即MD5接口可能会继续更新校验和?)只需执行
    printf(“%02x”,buffer[0]);printf(“%02x”,缓冲区[1])
    printf(“%02x”,teststring[0]);printf(“%02x”,teststring[1])。然后你就知道他们是不是一样了(我想不是)。查看文档,我认为您正确使用了md5功能。在调用之前和调用之间,您是否尝试过清除
    md5
    数组?@x4rf41投赞成票!没错,字符串是
    0x64 0x0a
    ,文件是
    0x61 0x0a
    ,但为什么?如您所见,我键入了``teststring=“\x61\x0a”“您确定发布的代码是您正在编译的吗?”--很可能不是
    char md5[]
    需要无符号,
    teststring
    需要无符号,甚至可以在第一时间进行编译。。。
    $ echo "a" | md5
    60b725f10c9c85c70d97880dfe8191b3
    
    $ echo "d" | md5
    e29311f6f1bf1af907f9ef9f44b8328b