-
UID:107504
-
- 注册时间2010-11-09
- 最后登录2015-07-27
- 在线时间49小时
-
- 发帖36
- 搜Ta的帖子
- 精华0
- 金钱360
- 威望46
- 贡献值0
- 好评度36
-
访问TA的空间加好友用道具
|
QHashFile qhashfile.h - #ifndef QHASHFILE_H
- #define QHASHFILE_H
- #include <QtCore/QFile>
- #include <QtCore/QByteArray>
- #include <QtCore/QCryptographicHash>
- class QHashFile
- {
- public:
- static QByteArray hash(QFile *file, QCryptographicHash::Algorithm method = QCryptographicHash::Md5);
- static QByteArray hash(const QString &fileName, QCryptographicHash::Algorithm method = QCryptographicHash::Md5);
- };
- #endif//QHASHFILE_H
qhashfile.cpp - /*
- Publish by Eric Chow at 2011/10/14.
- E-Mail: echow.always@live.com.
- */
- /*!
- \class QHashFile
- \brief The QHashFile class is tool class for fast hash file.
- */
- #include "qhashfile.h"
- /*!
- \fn QByteArray QHashFile::hash(QFile *file, QCryptographicHash::Algorithm method)
- \brief Hash file with the file object \a file and the specific method \a method, and return the result.
- */
- QByteArray QHashFile::hash(QFile *file, QCryptographicHash::Algorithm method)
- {
- Q_CHECK_PTR(file);
- if ((!file) || (!file->isReadable())) {
- return QByteArray();
- }
- if (file->pos() != 0) {
- if (!file->seek(0)) {
- return QByteArray();
- }
- }
- QCryptographicHash hash(method);
- char buffer[2048] = {0};
- qint64 numberOfReadBytes = 0;
- qint64 remainingBytes = file->size();
- while (remainingBytes) {
- numberOfReadBytes = remainingBytes > 2048 ? 2048 : remainingBytes;
- if (numberOfReadBytes == file->read(buffer, numberOfReadBytes))
- hash.addData(buffer, numberOfReadBytes);
- else
- return QByteArray();
- remainingBytes -= numberOfReadBytes;
- }
- return hash.result();
- }
- /*!
- \fn QByteArray QHashFile::hash(const QString &fileName, QCryptographicHash::Algorithm method)
- \brief Hash file with the file name \a fileName and the specific method \a method, and return the result.
- */
- QByteArray QHashFile::hash(const QString &fileName, QCryptographicHash::Algorithm method)
- {
- QByteArray result;
- QFile file(fileName);
- if (file.open(QFile::ReadOnly)) {
- result = hash(&file, method);
- file.close();
- }
- return result;
- }
|