目录
使用QCryptographicHash对文件进行MD5计算。用来检测文件是否改变
Qt:QCryptographicHash类,生成MD5,SHA-1,SHA-2 (***)
QCryptographicHash实现哈希值计算,支持多种算法
qt 大文件生成md5校验码
文件校验中md5 sha-1 sha-256 sha-512 哪种校验方式比较快呢?
QT 获取文件MD5值 (适合大内存,或小文件)
官方手册:QCryptographicHash Class
---------------------------------------------
官方手册:注意数据类型。
[static] QByteArray QCryptographicHash::hash(const QByteArray &data, QCryptographicHash::Algorithm method)
=============================
qt 大文件生成md5校验码
共有类型
枚举QCryptographicHash::Algorithm:
QCryptographicHash::Md4 0 生成一个MD4散列
QCryptographicHash::Md5 1 生成一个MD5散列
QCryptographicHash::Sha1 2 生成一个SHA-1散列
QCryptographicHash::Sha224 3 生成一个SHA-224散列(SHA-2)
QCryptographicHash::Sha256 4 生成一个SHA-256散列(SHA-2)
QCryptographicHash::Sha384 5 生成一个SHA-384散列(SHA-2)
QCryptographicHash::Sha512 6 生成一个SHA-512散列(SHA-2)
QCryptographicHash::Sha3_224 7 生成一个SHA3-224散列
QCryptographicHash::Sha3_256 8 生成一个SHA3-256散列
QCryptographicHash::Sha3_384 9 生成一个SHA3-384散列
QCryptographicHash::Sha3_512 10 生成一个SHA3-512散列
QString MakeMd5(const QString &sourceFilePath)
{
QFile sourceFile(sourceFilePath);
qint64 fileSize = sourceFile.size();
const qint64 bufferSize = 1024*10;
if (sourceFile.open(QIODevice::ReadOnly)) {
char buffer[bufferSize];
int bytesRead;
int readSize = qMin(fileSize, bufferSize);
QCryptographicHash hash(QCryptographicHash::Md5);
while (readSize > 0 && (bytesRead = sourceFile.read(buffer, readSize)) > 0) {
fileSize -= bytesRead;
hash.addData(buffer, bytesRead);
readSize = qMin(fileSize, bufferSize);
}
sourceFile.close();
return QString(hash.result().toHex());
}
return QString();
}
————————————————
版权声明:本文为CSDN博主「东方忘忧」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:qt 大文件生成md5校验码_qt md5校验_东方忘忧的博客-CSDN博客
QT 获取文件MD5值 (适合大内存,或小文件)
QT 获取文件的MD5值
概述
1. MD5值是一个文件唯一的标识,当文件被修改(更新)后,其MD5值也会改变
2. MD5值与文件名没什么关系
代码
QFile file(filePathName);
file.open(QFile::ReadOnly);
QByteArray fileMsg = file.readAll(); //只合适于: 大内存,或小文件
QByteArray md5 = QCryptographicHash::hash(fileMsg , QCryptographicHash::Md5).toHex();
file.close();
————————————————
版权声明:本文为CSDN博主「WeYoung.Tian」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:QT 获取文件MD5值_qt获取文件唯一性_WeYoung.Tian的博客-CSDN博客
QCryptographicHash实现哈希值计算,支持多种算法
文章目录
•1. 介绍
• 1.1. 支持的算法
• 1.2. 提供的接口
•2. 范例
1. 介绍
多看看Qt core模块会发现不少惊喜呀,里面包含的类不少涉及到不少方面的功能实现html
先附上全部core类:Qt Core,再直接给出QCryptographicHash的帮助:QCryptographicHash算法
此类用于提供密码散列,哈希值。能够生成二进制或文本形式的hash值,并支持多种算法,算法能够由QCryptographicHash::Algorithm选择app
1.1. 支持的算法
Constant Value Description
QCryptographicHash::Md4 0 Generate an MD4 hash sum
QCryptographicHash::Md5 1 Generate an MD5 hash sum
QCryptographicHash::Sha1 2 Generate an SHA-1 hash sum
QCryptographicHash::Sha224 3 Generate an SHA-224 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha256 4 Generate an SHA-256 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha384 5 Generate an SHA-384 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha512 6 Generate an SHA-512 hash sum (SHA-2). Introduced in Qt 5.0
QCryptographicHash::Sha3_224 RealSha3_224 Generate an SHA3-224 hash sum. Introduced in Qt 5.1
QCryptographicHash::Sha3_256 RealSha3_256 Generate an SHA3-256 hash sum. Introduced in Qt 5.1
QCryptographicHash::Sha3_384 RealSha3_384 Generate an SHA3-384 hash sum. Introduced in Qt 5.1
QCryptographicHash::Sha3_512 RealSha3_512 Generate an SHA3-512 hash sum. Introduced in Qt 5.1
QCryptographicHash::Keccak_224 7 Generate a Keccak-224 hash sum. Introduced in Qt 5.9.2
QCryptographicHash::Keccak_256 8 Generate a Keccak-256 hash sum. Introduced in Qt 5.9.2
QCryptographicHash::Keccak_384 9 Generate a Keccak-384 hash sum. Introduced in Qt 5.9.2
QCryptographicHash::Keccak_512 10 Generate a Keccak-512 hash sum. Introduced in Qt 5.9.2
1.2. 提供的接口
QCryptographicHash(Algorithm method)
~QCryptographicHash()
void addData(const char *data, int length)
void addData(const QByteArray &data)
bool addData(QIODevice *device)
void reset()
QByteArray result() const
static QByteArray hash(const QByteArray &data, Algorithm method)
能够实例化此类,构造时须要提供算法类型,而后经过addData须要计算hash的数据,最后经过result获取结果,能够利用reset清空数据但不能修改算法。post
还给了一个方便易用的静态方法,直接提供算法类型及数据内容便可。网站
2. 范例
#include <QCoreApplication>
#include <QDebug>
#include <QCryptographicHash>
int main(int argc, char *argv[])
{
QCoreApplication a( argc,argv);
QString text("test");
qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash:: Md5); //16進制結果
qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash:: Md5).toHex(); //轉換爲字符串
qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash:: Keccak_512); //16進制結果
qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash:: Keccak_512).toHex(); //轉換爲字符串
return 0;
}
结果spa
"\t\x8Fk\xCD""F!\xD3s\xCA\xDEN\x83&'\xB4\xF6"
"098f6bcd4621d373cade4e832627b4f6"
"\x1E.\x9F\xC2\x00+\x00-u\x19\x8Bu\x03!\f\x05\xA1\xBA\xAC""E`\x91j<m\x93\xBC\xCE:P\xD7\xF0\x0F\xD3\x95\xBF\x16G\xB9\xAB\xB8\xD1\xAF\xCC\x9Cv\xC2\x89\xB0\xC9""8;\xA3\x86\xA9V\xDAK8\x93""D\x17x\x9E"
"1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e"
其中test计算md5的结果是098f6bcd4621d373cade4e832627b4f6 能够在相关网站反查结果:
http://www.cmd5.com/
能够证实计算正确。code
转载请以连接形式标明本文标题和地址: Techie亮博客 » QCryptographicHash实现哈希值计算,支持多种算法
QCryptographicHash實現哈希值計算,支持多種算法 - JavaShuo
===============================
使用QCryptographicHash对文件进行MD5计算。用来检测文件是否改变
#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;
}
原文: 使用QCryptographicHash对文件进行MD5计算。用来检测文件是否改变_porridgeCooker的博客-CSDN博客
文件校验中md5 sha-1 sha-256 sha-512 哪种校验方式比较快呢?
结论
- 如果是防篡改场景,建议使用 MD5,因为它计算快速,摘要长度短。
- 如果是防偷窥场景,必须使用 SHA-256 或更大长度的摘要算法。
- 如果是防碰撞场景,为避免彩虹表攻击,必须加盐。
VS:摘要长度
摘要越长,意味着 hash 范围越大,产生碰撞的概率越低,但是更长的摘要通常需要更多的计算时间和储存成本。
MD5 , SHA1 , SHA256 比较
image.png
MD5 SHA1 SHA2 都是散列算法 ,什么是散列算法?
是一种从任何一种数据中创建小的数字"指纹"的方法.
基本特性:
如何两个散列值是不同的(同一函数),那么这两个散列值的原始输入也是不相同的.
如果两个散列值相同, 两个输入值很有可能是相同的, 但也可能是不同的.这种情况称为"散列碰撞"
什么是SHA?
SHA (Secure Hash Algorithm): 代表安全哈希算法, 是一种加密散列算法.
SHA0 SHA1 SHA2 SHA3 版本越高越安全.
SHA256 指SHA2 长度256bit, 规范命名 SHA2-256
异同点
MD5 | SHA1 | SHA2-256 | |
---|---|---|---|
长度 | 128bit | 160bit | 256bit |
速度 | 最快 | 较快 | 最慢 |
安全性 | 不安全 | 不安全 | 安全 |
时间 | 1992年 | 1995年 | 2005 |
用例
- 数据完整性校验
- 伪随机数
- 密码保存
- 工作量证明
===============================
Qt:QCryptographicHash类,生成MD5,SHA-1,SHA-2 (***)
知道吗,Qt是跨平台C++图形用户界面应用程序开发框架。它既可以开发GUI程序,也可用于开发非GUI程序。它还提供了很多便捷功能,有很多类的功能是非常强大的。
比如这个类QCryptographicHash,是Qt提供的生成hash散列表的类,目前能够实现的加密方式有:
MD4, MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512。
安全相关的开发,肯定少不了哦。
而且它的算法还超级简单,一看就懂,不解释了:
void addData(const char *data, int length)
void addData(const QByteArray &data)
bool addData(QIODevice *device)
void reset()
QByteArray result() const
static QByteArray hash(const QByteArray &data, Algorithm method)
支持的算法种类也是超多,Algorithm取值如下:
![](https://img-blog.csdnimg.cn/img_convert/d8211457895bb074dce573171476e35b.png)
支持的算法种类也是超多,Algorithm取值如下:
QCryptographicHash::Md4
QCryptographicHash::Md5
QCryptographicHash::Sha1
QCryptographicHash::Sha224
QCryptographicHash::Sha256
QCryptographicHash::Sha384
QCryptographicHash::Sha512
QCryptographicHash::Sha3_224
QCryptographicHash::Sha3_256
QCryptographicHash::Sha3_384
QCryptographicHash::Sha3_512
————————————————
版权声明:本文为CSDN博主「iamdbl」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:Qt那些类---用QCryptographicHash类生成MD5,SHA-1,SHA-2_qt文件sha128_iamdbl的博客-CSDN博客
官方手册:QCryptographicHash Class
Contents
QCryptographicHash Class
The QCryptographicHash class provides a way to generate cryptographic hashes. More...
Header: | #include <QCryptographicHash> |
qmake: | QT += core |
Since: | Qt 4.3 |
This class was introduced in Qt 4.3.
Note: All functions in this class are reentrant.
Public Types
enum | Algorithm { Md4, Md5, Sha1, Sha224, Sha256, …, Keccak_512 } |
Public Functions
QCryptographicHash(QCryptographicHash::Algorithm method) | |
void | addData(const char *data, int length) |
void | addData(const QByteArray &data) |
bool | addData(QIODevice *device) |
void | reset() |
QByteArray | result() const |
Static Public Members
QByteArray | hash(const QByteArray &data, QCryptographicHash::Algorithm method) |
int | hashLength(QCryptographicHash::Algorithm method) |
Detailed Description
QCryptographicHash can be used to generate cryptographic hashes of binary or text data.
Refer to the documentation of the QCryptographicHash::Algorithm enum for a list of the supported algorithms.
Member Type Documentation
enum QCryptographicHash::Algorithm
Note: In Qt versions before 5.9, when asked to generate a SHA3 hash sum, QCryptographicHash actually calculated Keccak. If you need compatibility with SHA-3 hashes produced by those versions of Qt, use the Keccak_ enumerators. Alternatively, if source compatibility is required, define the macro QT_SHA3_KECCAK_COMPAT.
Constant | Value | Description |
QCryptographicHash::Md4 | 0 | Generate an MD4 hash sum |
QCryptographicHash::Md5 | 1 | Generate an MD5 hash sum |
QCryptographicHash::Sha1 | 2 | Generate an SHA-1 hash sum |
QCryptographicHash::Sha224 | 3 | Generate an SHA-224 hash sum (SHA-2). Introduced in Qt 5.0 |
QCryptographicHash::Sha256 | 4 | Generate an SHA-256 hash sum (SHA-2). Introduced in Qt 5.0 |
QCryptographicHash::Sha384 | 5 | Generate an SHA-384 hash sum (SHA-2). Introduced in Qt 5.0 |
QCryptographicHash::Sha512 | 6 | Generate an SHA-512 hash sum (SHA-2). Introduced in Qt 5.0 |
QCryptographicHash::Sha3_224 | RealSha3_224 | Generate an SHA3-224 hash sum. Introduced in Qt 5.1 |
QCryptographicHash::Sha3_256 | RealSha3_256 | Generate an SHA3-256 hash sum. Introduced in Qt 5.1 |
QCryptographicHash::Sha3_384 | RealSha3_384 | Generate an SHA3-384 hash sum. Introduced in Qt 5.1 |
QCryptographicHash::Sha3_512 | RealSha3_512 | Generate an SHA3-512 hash sum. Introduced in Qt 5.1 |
QCryptographicHash::Keccak_224 | 7 | Generate a Keccak-224 hash sum. Introduced in Qt 5.9.2 |
QCryptographicHash::Keccak_256 | 8 | Generate a Keccak-256 hash sum. Introduced in Qt 5.9.2 |
QCryptographicHash::Keccak_384 | 9 | Generate a Keccak-384 hash sum. Introduced in Qt 5.9.2 |
QCryptographicHash::Keccak_512 | 10 | Generate a Keccak-512 hash sum. Introduced in Qt 5.9.2 |
Member Function Documentation
QCryptographicHash::QCryptographicHash(QCryptographicHash::Algorithm method)
Constructs an object that can be used to create a cryptographic hash from data using method.
QCryptographicHash::~QCryptographicHash()
Destroys the object.
void QCryptographicHash::addData(const char *data, int length)
Adds the first length chars of data to the cryptographic hash.
void QCryptographicHash::addData(const QByteArray &data)
This function overloads addData().
bool QCryptographicHash::addData(QIODevice *device)
Reads the data from the open QIODevicedevice until it ends and hashes it. Returns true if reading was successful.
This function was introduced in Qt 5.0.
[static] QByteArray QCryptographicHash::hash(const QByteArray &data, QCryptographicHash::Algorithm method)
Returns the hash of data using method.
[static] int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)
Returns the size of the output of the selected hash method in bytes.
This function was introduced in Qt 5.12.
void QCryptographicHash::reset()
Resets the object.
QByteArray QCryptographicHash::result() const
Returns the final hash value.
See also QByteArray::toHex().