Qt:QCryptographicHash类,生成MD5,SHA-1,SHA-2 (***)

目录

使用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
异同点
MD5SHA1SHA2-256
长度128bit160bit256bit
速度最快较快最慢
安全性不安全不安全安全
时间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取值如下:

支持的算法种类也是超多,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)

~QCryptographicHash()

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().

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:collect2.exe: error: ld returned 1 exit status mingw32-make\[2\]: *** \[modules\core\CMakeFiles\opencv_core.dir\build.make:1800: bin/libopencv_core451.dll\] Error 1 mingw32-make\[1\]: *** \[CMakeFiles\Makefile2:1794: modules/core/CMakeFiles/opencv_core.dir/all\] Error 2 mingw32-make: *** \[Makefile:165: all\] Error 2 。 引用\[2\]:mingw32-make\[2\]: *** \[modules\python2\CMakeFiles\opencv_python2.dir\build.make:76: modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.obj\] Error 1 mingw32-make\[1\]: *** \[CMakeFiles\Makefile2:3365: modules/python2/CMakeFiles/opencv_python2.dir/all\] Error 2 mingw32-make: *** \[Makefile:165: all\] Error 2 。 引用\[3\]:问题:gcc: error: long: No such file or directory** C:\PROGRA~2\Dev-Cpp\MinGW64\bin\windres.exe: preprocessing failed. modules\core\CMakeFiles\opencv_core.dir\build.make:1420: recipe for target ‘modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj’ failed mingw32-make\[2\]: *** \[modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj\] Error 1 mingw32-make\[2\]: *** Waiting for unfinished jobs… CMakeFiles\Makefile2:1603: recipe for target ‘modules/core/CMakeFiles/opencv_core.dir/all’ failed mingw32-make\[1\]: *** \[modules/core/CMakeFiles/opencv_core.dir/all\] Error 2 。 问题:qt中Opencv4 cmake时mingw32-make: *** \[Makefile:165: all\] Error 2 回答: 这些错误信息表明在使用Qt中的Opencv4进行cmake时出现了问题。具体来说,出现了ld returned 1 exit status、Error 1、Error 2等错误。这些错误通常是由于编译器无法找到所需的文件或目录导致的。可能的原因包括缺少依赖项、路径设置错误或编译器配置问题。为了解决这些错误,您可以尝试以下几个步骤: 1. 确保您已正确安装了Opencv4和Qt,并且它们的路径已正确设置。 2. 检查您的编译器配置是否正确,包括编译器版本和路径设置。 3. 检查您的项目配置文件是否正确,包括CMakeLists.txt文件中的路径和依赖项设置。 4. 确保您的系统中已安装所需的依赖项,并且它们的路径正确。 5. 尝试清理并重新构建您的项目,以确保所有文件和依赖项都正确配置。 如果您仍然遇到问题,建议您查阅Opencv4和Qt的官方文档,以获取更详细的指导和解决方案。 #### 引用[.reference_title] - *1* *2* [Qt5.14+Opencv4.1+CMake3.22配置](https://blog.csdn.net/qq_45792470/article/details/125774264)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [OpenCV使用CMake和MinGW-w64的编译安装](https://blog.csdn.net/qq_61604164/article/details/121731875)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值