占个坑,有空写。先把代码传上来。
#include "c4hashcrypt.h"
#include <QFile>
#include <QTextCodec>
#include <QTextStream>
#include <QCryptographicHash>
#include <QDebug>
/***********************************************************
* 遍历大文件16g大概2分多,如果是零碎文件5g大概6分钟
* ********************************************************/
C4HashCrypt::C4HashCrypt(QObject *parent) :
QObject(parent)
{
}
bool C4HashCrypt::md5Files(QStringList fileList, QString hashFile)
{
QFile hM5file(hashFile);
QString lastest("Desktop/");
if(hM5file.open(QFile::Truncate|QFile::WriteOnly|QFile::Text))
{
foreach (QString file, fileList) {
QByteArray bb = md5file(file);
bb.append(" [file]= ");
file =file.right(file.size()-file.lastIndexOf(lastest));//去掉指定文件路径~
file = file.right(file.size()-QString(lastest).size());
bb.append(file);
bb.append('\n');
hM5file.write(bb);
}
hM5file.close();
return true;
}
else
{
qDebug()<<"openHM5file False";
return false;
}
}
QByteArray C4HashCrypt::md5file(QString fileName)
{
QByteArray md5C = Q_NULLPTR;
QFile file(fileName);
// QTextCodec *code = QTextCodec::codecForName("utf8");
if(file.exists())
{
if(file.open(QFile::ReadOnly))
{
#if 0
QTextStream stream(&file);//如果用这种方法则会导致加密rar文件报错。
stream.setCodec(code);
QString data;
data = stream.readAll();
md5C = QCryptographicHash::hash(data.toLatin1(),\
QCryptographicHash::Md5).toHex();//必须有.toHex();
#else
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(&file);
md5C = hash.result().toHex();
#endif
file.close();
}else
{
qDebug()<<"open file False";
}
}else
{
qDebug()<<"file is not exit";
}
return md5C;
}