通俗易懂玩Qt:文件校验码生成器的实现(内附源码)

Qt 实现文件校验码生成器(内附源码)

该软件是基于 CertUtil 的一个文件文件校验码生成,旨在提高下载程序的一个安全系数,防止黑客攻击网站后,将携带病毒的程序放在下载链接上,当用户使用程序时,被移植的病毒破坏系统,盗取可用信息,留下后门。

一、实验效果与开发环境

效果图如下:

开发环境:
通俗易懂玩Qt:文件校验码生成器的实现(内附源码)_第1张图片

二、实验源代码

dealProcess.h

class DealProcess : public QObject {
    Q_OBJECT

public:
    explicit DealProcess(QObject *parent = nullptr);
    ~DealProcess();

    void write(QString data);
    QByteArray exec(QString command, QStringList args);

private:
    QProcess *process;
    QByteArray data_in;

    void init();
};

dealProcess.c

DealProcess::DealProcess(QObject *parent) :
    QObject(parent) {

    init();
}

DealProcess::~DealProcess() {}

void DealProcess::init() {
    process = new QProcess;
    data_in.resize(0);  //将数据初始化为 0
}

QByteArray DealProcess::exec(QString command, QStringList args) {
    QByteArray res = "Process had been executed!"; //定义一个

    process -> start(command, args);    //开启一条命令执行通道

    if(!process -> waitForStarted()) {  //等待命令通道的开启
        return "Process start failed!"; //否则返回命令通道失败
    }

    if(data_in.size() != 0) {           //判断写入数据的长度是否为 0,是则跳过写入步骤
        process -> write(data_in);      //写入数据
        process -> closeWriteChannel(); //关闭数据写入通道
    }

    if(!process -> waitForFinished()) { //等待命令自行完成
        return "Process finished failed!";  //否则返回命令执行失败
    }

    data_in.resize(0);  //将数据还原为 0

    res = process -> readAll();    //接收反馈数据

    return res; //返回反馈数据
}

void DealProcess::write(QString data) {
    QByteArray byteArray;   //初始化一个 QByteArray 变量用来接收需要写入的数据

    byteArray = data.toUtf8().data();   //使用变量 byteArray 接收写入数据
    data_in.resize(byteArray.size());   //根据接收数据大小重新定义类成员变量 data_in 的大小
    data_in = byteArray;    //将变量 byteArray 接收到的数据写入到类成员变量
}

mainWindow.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    DealProcess *deal_process;

    QFileDialog *file_dialog;

    QMessageBox *message_box;

    QClipboard *clipboard;

    QString command;
    QStringList args;

    QPalette pale;
    QColor color,
           drop_color;

    void configInit();
    void uiInit();
    void connectInit();
    void setBackgroung(QColor color);
    void setUrl(QString url);

protected:
    void dragEnterEvent(QDragEnterEvent *e);
    void dragLeaveEvent(QDragLeaveEvent *e);
    void dropEvent(QDropEvent *e);
};

mainWindow.c

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui -> setupUi(this);

    configInit();
    uiInit();
    connectInit();
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::setBackgroung(QColor color) {
    pale = palette();
    pale.setColor(QPalette::Window, color);
    setPalette(pale);
}

void MainWindow::setUrl(QString url) {
    ui -> labFileUrl -> setText(url);
    ui -> labFileUrl -> setToolTip(tr(url.toLocal8Bit().data()));
    args[1] = url;
}

void MainWindow::configInit() {
    deal_process = new DealProcess; //初始化命令处理类
    command = "CertUtil";
    args << "-hashfile" << "" << "md5";
    file_dialog = new QFileDialog;
    color.setRgb(243, 243, 243, 255);       //窗口背景色
    drop_color.setRgb(230, 230, 230, 250);  //拖拽进入后的窗口背景色
    clipboard = QApplication::clipboard();

    setWindowTitle("文件校验码生成器");
    setAcceptDrops(true);   //允许放下操作
    setBackgroung(color);
}

void MainWindow::uiInit() {
    //QGroupBox init.
    ui -> grbChooseFile -> setTitle("选择校验文件");
    ui -> grbResult -> setTitle("运行结果");
    ui -> grbValue -> setTitle("校验值");

    //QPushButton init.
    ui -> btnChooseFile -> setText("选择文件");
    ui -> btnCreateFileChecker -> setText("获取校验");
    ui -> btnCopyCode -> setText("复制代码");

    //QComboBox init
    QStringList str_list_check_type;
    str_list_check_type << "MD2" << "MD4" << "MD5" << "SHA1" << "SHA256" << "SHA384" << "SHA512";
    for(int i = 0; i < str_list_check_type.length(); i ++) {
        ui -> cbbCheckType -> addItem(str_list_check_type[i], str_list_check_type[i]);
    }
    ui -> cbbCheckType -> setCurrentText("MD5");

    //QPlainTextEdit
    ui -> ptedResult -> setDisabled(true);

    //QMessageBox
    message_box = new QMessageBox;
    message_box -> setButtonText(QMessageBox::Close, QString("关闭"));

    //QLabel
    ui -> labFileUrl -> setText("");

    //QStatusBar
    ui -> statusBar ->addPermanentWidget(new QLabel("©2021-2099 我是混子我怕谁"));

    //QMenu
    ui -> actionQuit -> setShortcut(Qt::CTRL | Qt::Key_Q);

    ui -> actionCompare -> setShortcut(Qt::ALT | Qt::Key_C);
}

void MainWindow::connectInit() {
    //QPushButton
    connect(ui -> btnChooseFile, &QPushButton::clicked, [=] {
        QString url;
        url = file_dialog -> getOpenFileName(this,
                                             tr("选择文件"),
                                             QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
                                             tr("所有文件(*.*);;压缩文件(*.zip *.rar *.7z);;"));
        setUrl(url);
    });

    connect(ui -> btnCreateFileChecker, &QPushButton::clicked, [=] {
        QString res = "";

        if(args[1] != "") {
            res = QString::fromLocal8Bit(deal_process -> exec(command, args));
            ui -> ptedResult -> clear();
            ui -> ptedResult -> appendPlainText(res);
            ui -> ledCheckerCode -> setText(res.split("\r\n")[1].split(' ').join(""));
        }
        else {
            message_box -> critical(this, "温馨提示", "请选择即将校验的文件!", QMessageBox::Close);
        }
    });

    connect(ui -> btnCopyCode, &QPushButton::clicked, [=] {
        QString checker_code = ui -> ledCheckerCode -> text();  //获取校验码

        if(checker_code != "") {    // 判断校验码是否为空
            clipboard -> setText(checker_code);
        }
    });

    //QComboBox
    connect(ui -> cbbCheckType, &QComboBox::currentTextChanged, [=](QString check_type) {
        args[2] = check_type;
    });

    //QMenu
    connect(ui -> actionQuit, &QAction::triggered, [=] {
        QMainWindow::close();
    });

    connect(ui ->actionCompare, &QAction::triggered, [=] {
        qDebug() << 1;
    });
}

void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
    e -> acceptProposedAction();    //接收放下操作

    setBackgroung(drop_color);
}

void MainWindow::dragLeaveEvent(QDragLeaveEvent *e) {
    Q_UNUSED(e);

    setBackgroung(color);
}

void MainWindow::dropEvent(QDropEvent *e) {
    setBackgroung(color);

    QList<QUrl> urls = e -> mimeData() -> urls();

    if(urls.length() != 1) {
        message_box -> critical(this, "友情提示", "不接受多个文件,请拖拽单个\r\n文件!", QMessageBox::Close);
        return;
    }

    QString url = urls[0].path().mid(1);

    if(QFileInfo(url).isDir()) {
        message_box -> critical(this, "友情提示", "不接受文件夹,请拖拽单个\r\n文件!", QMessageBox::Close);
        return;
    }

    setUrl(url);
}

源码分享:https://gitee.com/mjzhutianxiu/filechecker

后续还会在原有的基础上做微调,详细源码。

学习分享,一起成长!以上为小编的经验分享,若存在不当之处,请批评指正!

你可能感兴趣的:(QT学习)