通俗易懂玩Qt:⽂件校验码⽣成器的实现(内附源码)
Qt 实现⽂件校验码⽣成器(内附源码)
该软件是基于 CertUtil 的⼀个⽂件⽂件校验码⽣成,旨在提⾼下载程序的⼀个安全系数,防⽌⿊客攻击⽹站后,将携带病毒的程序放在下载链接上,当⽤户使⽤程序时,被移植的病毒破坏系统,盗取可⽤信息,留下后门。
⼀、实验效果与开发环境
效果图如下:
connect下载开发环境:
⼆、实验源代码
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;
size(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!";//否则返回命令执⾏失败
}
size(0);//将数据还原为 0
res = process ->readAll();//接收反馈数据
return res;//返回反馈数据
}
void DealProcess::write(QString data){
QByteArray byteArray;//初始化⼀个 QByteArray 变量⽤来接收需要写⼊的数据
byteArray = Utf8().data();//使⽤变量 byteArray 接收写⼊数据
size(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 ->setText(url);
ui -> labFileUrl ->setToolTip(Local8Bit().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("<font>©</font>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);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。