在CentOS和Win7上使用QCryptographicHash不匹配MD5散列 [英] Mismatching MD5 hash using QCryptographicHash on CentOS and Win7

查看:129
本文介绍了在CentOS和Win7上使用QCryptographicHash不匹配MD5散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要在两台机器(客户端和服务器)之间传输FTP文件的项目。我想通过将客户端上生成的MD5校验和与服务器上生成的MD5校验和进行比较来验证传输后文件的完整性。我写了一个命令行程序使用Qt的QCryptographicHash类来做到这一点。减少错误检查,这是整个代码:

I'm working on a project that requires FTP file transfer between two machines, a client and a server. I would like to verify the integrity of the file after transfer by comparing an MD5 checksum generated on the client with one generated on the server. I wrote a command line program using Qt's QCryptographicHash class to do this. Minus error checking, this is the code in its entirety:

#include <QCoreApplication>
#include <QByteArray>
#include <QFile>
#include <QCryptographicHash> 
#include <QTextStream>
#include <QStringList>

int main (int argc, char* argv[]) {
    QCoreApplication app(argc, argv);
    QTextStream cout(stdout, QIODevice::WriteOnly);
    QStringList arglist = app.arguments();

    QFile file(arglist.at(1));
    if(!file.open(QIODevice::ReadOnly)){
        /*error checking*/
    }


    QCryptographicHash cryptoHash(QCryptographicHash::Md5);
    while(!file.atEnd()){
        cryptoHash.addData(file.readLine());
    }

    QByteArray hashByteArray = cryptoHash.result();

    cout << hashByteArray.toHex() <<endl;
    file.close();
    return 0;
}



问题



在任何机器上,此代码将给出任何输入文件的可重复的MD5散列。但是,当在客户端上进行散列时,对于同一文件生成的散列不同于在我的服务器上散列的散列。我一直在战斗这一段时间,只是似乎不能确定为什么这些哈希不匹配。我最后的想法是,它可能是一个与架构相关的问题,在此SO帖中讨论。但是,在我的情况下,我使用Qt的MD5哈希实现,所以我想确保没有别的东西,我失去了之前,我放弃Qt的方法或修补与他们的源(这在这里 md5.h 和此处 md5.cpp ,BTW)。

The Problem

On any machine, this code will give a repeatable MD5 hash of any input file. However, different hashes are being generated for the same file when hashed on my client than when hashed on my server. I've been fighting with this awhile, and just can't seem to determine why these hashes aren't matching. My last thought is that it may be an issue related to architecture, as discussed in this SO post. However, in my case, I'm using Qt's implementation of MD5 hashing, so I want to make sure there isn't something else I'm missing before I either abandon Qt's approach or tinker with their source (which is here md5.h and here md5.cpp, BTW).

机器细节。服务器:CentOS 5.8 64位。客户端:Win7 64位,但在Windows上这个应用程序是使用32位版本的Qt编译的,是一个32位的应用程序。

Machine specifics. Server: CentOS 5.8 64-bit. Client: Win7 64-bit, but on Windows this application was compiled using a 32-bit build of Qt, and is a 32-bit application.

我打算分发我的应用程序的32位和64位版本。如果是与架构相关的问题,并且我在使用Qt的来源,我该如何解决这个问题?

As an aside, I intended on distributing both 32-bit and 64-bit versions of my application. How can I reconcile this issue if it is architecture related and I'm stuck using Qt's source?

EDIT 1 :我忘了提及我通过FTP传输所有文件在二进制模式下,文件大小匹配客户端和服务器。我最初相信这是一个行结束问题,但它似乎并不是这样的情况,因为我遇到的问题,即使在二进制模式FTP传输。

EDIT 1: I forgot to mention that I'm transferring all files in Binary mode through FTP and the file sizes do match on the client and the server. I was initially convinced this was a line ending issue, but it doesn't appear to be the case since I'm encountering the issue even in Binary mode FTP transfer.

推荐答案

你不应该使用readline一个更好的方法是读取一个字节数组,然后计算哈希像这样

You should not use readline a better approach is to read it in a byte array and then calculate the hash like this

QFile file("/home/opc0de/somefile.dat");

    if (file.open(QIODevice::ReadOnly)) {
        QByteArray fileData = file.readAll();
        QByteArray hashData = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);

        qDebug() << hashData.toHex();
    }

希望它有帮助。

这篇关于在CentOS和Win7上使用QCryptographicHash不匹配MD5散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆