首页 > 其他分享 >Qt 计算字符串和文件的md5 值

Qt 计算字符串和文件的md5 值

时间:2023-03-20 09:12:28浏览次数:36  
标签:hash Qt QByteArray file 字符串 QCryptographicHash include string md5

chatgpt结果

string

#include <QCryptographicHash>
#include <QDebug>

QString string = "Hello, World!";
QByteArray data = string.toUtf8();
QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);

qDebug() << "MD5 hash of the string:" << hash.toHex();

file


#include <QCryptographicHash>
#include <QFile>
#include <QDebug>

QString filePath = "path/to/file";
QFile file(filePath);

if (file.open(QFile::ReadOnly)) {
    QCryptographicHash hash(QCryptographicHash::Md5);
    if (hash.addData(&file)) {
        QByteArray result = hash.result();
        qDebug() << "MD5 hash of the file:" << result.toHex();
    }
    else {
        qWarning() << "Failed to calculate MD5 hash of the file.";
    }
    file.close();
}
else {
    qWarning() << "Failed to open the file.";
}

标签:hash,Qt,QByteArray,file,字符串,QCryptographicHash,include,string,md5
From: https://www.cnblogs.com/simp/p/17235142.html

相关文章

  • Qt Tcp Server
    chatgpt结果head//TcpServerBase.h#ifndefTCPSERVER_H#defineTCPSERVER_H#include<QObject>#include<QTcpServer>#include<QTcpSocket>classTcpServerBa......
  • 字符与字符数组与字符串
    字符是一种系统自带的数据类型,用char定义,一次只能储存一个字符1#include<stdio.h>23intmain()4{5//定义一个字符变量,存储一个字符6charch......
  • MySQL如何正确查询字符串长度
    前言笔者最近有一个需求,需要将一段文字插入到备注字段remark前面。由于担心插入后超过字段长度的限制,所以需要统计线上数据,根据长度倒序查询remark最长的一批数据看看长......
  • Java基础字符串练习
    ​定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在控制台输出结果。要求:1、如果传递的参数为空,返回null2、如果传递的数组元素个数为0......
  • Qt音视频开发26-监控画面各种图形绘制设计
    一、前言视频监控系统做到后面,逐渐需要搭配人工智能算法,将算法计算后的信息以OSD标签以及方框各种图形的信息显示到视频中,这种当然和OSD一样也是有两种方式,一种是源头就贴......
  • C++/Qt网络通讯模块设计与实现(三)
    上一节给大家从源码级别分析了SNetClient网络客户端的实现过程,详见​C++/Qt网络通讯模块设计与实现(二),并给大家留了一个疑问,即引入SNetClientRunning类是为了解决什么问题......
  • QT5.15.2静态编译包下载
    QT5.15.2静态编译包下载      经过反复的折腾,终于编译成了QT5.15.2的静态编译。网上指导静态编译的资料很多,但是只有自己趟过坑,才知道有多深。最终明白“纸上......
  • Qt中的QDate
    //获取当前的日期QDated=QDate::currentDate();//第一种方式:qDebug()<<"year:"<<d.year()<<",moth:"<<d.month()<<",day:"<<d.d......
  • 08、【opencsacade+qt 篇】之二:绘制球
    绘制球的基本过程gp_Ax2类:实例化一个坐标;gp_Ax2::SetLocation(gp_Pnt);设置原点位置BRepPrimAPI_MakeSphere(gp_Ax2,int)::shape();传入坐标和半径并创建球形AIS_Sha......
  • 力扣---1616. 分割两个字符串得到回文串
    给你两个字符串a和b,它们长度相同。请你选择一个下标,将两个字符串都在相同的下标分割开。由a可以得到两个字符串:aprefix和asuffix,满足a=aprefix+asuffix,......