MD5 tool development (QT programming)

Posted by Cardale on Thu, 07 Nov 2019 17:34:43 +0100

MD5 tool development

MD5 message digest algorithm, a widely used cryptographic hash function, can generate a 128 bit (16 byte) hash value to ensure the integrity and consistency of information transmission. MD5 was designed by Ronald Linn Rivest, an American cryptographer, and was released in 1992 to replace MD4 algorithm.
MD5 application editing
Consistency verification
digital signature
Secure access authentication
Create a Qt Widget Application

Choose to add a C++ Class

UI page layout:

md5check.h

#include <QObject>
#include<QThread>
#include <QFileInfo>
#include <QFileDialog>
#include<qDebug>
#include<QFile>
#include <QCryptographicHash>
#include<QTime>
class Md5Check : public QObject
{
    Q_OBJECT
public:
    explicit Md5Check(QObject *parent = nullptr);

signals:
 void sendMd5CheckSignal(const QByteArray &);//Feedback to md5 value interface
 void sendProgressBarSignal(int,int);//Feedback to progress bar page to generate progress bar
                                    //Amount of data processed, amount of data to be processed

public slots:
 void getMd5CheckSumSlot(const QString &filePath);
};

#endif // MD5CHECK_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include"md5check.h"
#include <QClipboard>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
signals:
    void checkMd5SumSignal(const QString &filePath);
private slots:
    void on_toolButtonOpenFile_clicked();
    void recvMd5CheckSumSlot(const QByteArray &);
    void recvProgressBarSlot(int,int);
    void on_pushButtonClose_clicked();
    void on_pushButtonCopySum_clicked();
private:
    Ui::Widget *ui;
    Md5Check mMd5CheckSum;
     QThread md5Thread;
};

#endif // WIDGET_H

widget.cpp

#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    mMd5CheckSum.moveToThread(&md5Thread);//Move from object to a thread
    md5Thread.start();
    qDebug()<<"Widget::Widget"<<QThread::currentThreadId();
    connect(&mMd5CheckSum,SIGNAL(sendMd5CheckSignal(QByteArray)),this,SLOT(recvMd5CheckSumSlot(QByteArray)));
    connect(this,SIGNAL(checkMd5SumSignal(QString)),&mMd5CheckSum,SLOT(getMd5CheckSumSlot(QString)));
    connect(&mMd5CheckSum,SIGNAL(sendProgressBarSignal(int,int)),this,SLOT(recvProgressBarSlot(int,int)));

}
Widget::~Widget()
{
    md5Thread.exit();//Thread exit
    qDebug()<<QTime::currentTime();
    md5Thread.wait(10*1000);//10 seconds timeout (maximum blocking time is 10 seconds)
    qDebug()<<QTime::currentTime();//Determine wait() time
    delete ui;
}

void Widget::on_toolButtonOpenFile_clicked()
{
    QFileInfo md5FileInfo;
    QString md5FileName=QFileDialog::getOpenFileName(this,
                                QString::fromLocal8Bit("Open required MD5SUM file"));//Pop up a file dialog box
    qDebug()<<"md5FleNmae path:"<<md5FileName;//Output the address of the selected file
     md5FileInfo=QFileInfo(md5FileName);//Get text key
     ui->textBrowser->clear();
     if(md5FileInfo.exists())
     {
         ui->lineEdit->setText(md5FileInfo.fileName());//Output filename in lineEdit
         emit checkMd5SumSignal(md5FileName);//Send the address of the file as a signal
     }
}
void Widget::recvMd5CheckSumSlot(const QByteArray &baResult)
{
    ui->textBrowser->setText(baResult);
}
void Widget::recvProgressBarSlot(int current, int total)
{
    ui->progressBar->setMaximum(total);
    ui->progressBar->setValue(current);
    //Order cannot be reversed
}
void Widget::on_pushButtonClose_clicked()
{
    this->close();
}
void Widget::on_pushButtonCopySum_clicked()
{


     QString source=ui->textBrowser->toPlainText();//Get the contents of textBrowser
       qDebug()<<source;
       QClipboard *clipboard = QApplication::clipboard();//Get

//Take the pointer on the cutting board
   //Qstring originaltext = clipboard - > text(); / / get the text information on the clipboard
   clipboard->setText(source);//Set the contents of the clipboard
}

md5check.cpp

#include "md5check.h"
Md5Check::Md5Check(QObject *parent) : QObject(parent)
{

}
void Md5Check::getMd5CheckSumSlot(const QString &filePath)
{
    QFile file(filePath);
    QByteArray md5Result;
    QFileInfo md5FileInfo;
    md5FileInfo=QFileInfo(filePath);
    int totalSize=md5FileInfo.size()/(1024*1024);//Size of the selected file (convert bytes to m here)
    if(md5FileInfo.size()%(1024*1024)>0)
    {
        totalSize=totalSize+1;//Add 1 directly for the last less than 1M of the file
    }
    int currentSize=0;
    qDebug()<<"Md5Check::getMd5CheckSumSlot "<<QThread::currentThreadId();
    if(file.open(QIODevice::ReadOnly))
    {
        QCryptographicHash hash(QCryptographicHash::Md5);
        while(!file.atEnd())//If the file cannot be read, do not exit
        {
            QByteArray content=file.read(1024*1024);
            hash.addData(content);
            currentSize++;
            emit sendProgressBarSignal(currentSize,totalSize);
        }
        md5Result=hash.result().toHex();
        qDebug()<<md5Result;
        emit sendMd5CheckSignal(md5Result);
        file.close();
    }
    else
    {
        return ;
    }
}

Topics: Qt less