Skip to content

Commit

Permalink
增加SHA 系列计算
Browse files Browse the repository at this point in the history
URL encode [a-zA-Z0-9]数据全编码
  • Loading branch information
sleepinging committed Jun 7, 2020
1 parent db531a6 commit 848c778
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 33 deletions.
49 changes: 38 additions & 11 deletions widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@

#include <QDebug>
#include <QTextCodec>
#include <QByteArray>
#include <QMessageBox>
#include <QCryptographicHash>
#include <QMap>
#include <QUrl>

static auto g_sha_fn=QMap<QString,QCryptographicHash::Algorithm>{
{"Sha1",QCryptographicHash::Sha1},
{"Sha224",QCryptographicHash::Sha224},
{"Sha256",QCryptographicHash::Sha256},
{"Sha384",QCryptographicHash::Sha384},
{"Sha512",QCryptographicHash::Sha512}
};

Widget::Widget(QWidget *parent)
: QWidget(parent)
Expand All @@ -25,12 +34,7 @@ Widget::~Widget()

void Widget::on_btn_encode_clicked()
{
auto str1=ui->te_org->toPlainText();
// QString str1="ABC中国";
auto utf8 = QTextCodec::codecForName("UTF-8");
auto strUnicode= utf8->toUnicode(str1.toUtf8().data());
auto qtc = get_select_charset();
auto bs=qtc->fromUnicode(strUnicode);
auto bs=get_input_data();

//HEX
{
Expand All @@ -44,14 +48,13 @@ void Widget::on_btn_encode_clicked()
}

//url
ui->te_url->setText(bs.toPercentEncoding());
ui->te_url->setText(QUrl::toPercentEncoding(bs,QByteArray(),"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890"));

//base64
ui->te_base64->setText(bs.toBase64());

//md5
{
QString md5;
auto data=QCryptographicHash::hash(bs, QCryptographicHash::Md5);
auto hex=data.toHex();
//32位大写
Expand All @@ -64,6 +67,12 @@ void Widget::on_btn_encode_clicked()
ui->te_md5->append(hex.mid(8,16));
}

//SHA
{
auto sha=QCryptographicHash::hash(bs, g_sha_fn[ui->cb_sha->currentText()]);
ui->te_sha->setText(sha.toHex().toUpper());
}

//转unicode显示
{
QString us;
Expand Down Expand Up @@ -104,7 +113,7 @@ void Widget::on_btn_hex_clicked()
QMessageBox::critical(this,"错误","字符 "+hexc);
return;
}
bs.append(num);
bs.append(static_cast<char>(num));
}
auto tc=get_select_charset();
ui->te_org->setText(tc->toUnicode(bs));
Expand Down Expand Up @@ -143,7 +152,7 @@ void Widget::on_btn_uni_clicked()

void Widget::on_btn_md5_clicked()
{

QMessageBox::information(this,"手动滑稽🙂","恭喜您发明了世界上最伟大的压缩算法");
}

void Widget::on_cb_cl_stateChanged(int arg1)
Expand All @@ -168,3 +177,21 @@ QTextCodec* Widget::get_select_charset()
// qDebug()<<pcs;
return QTextCodec::codecForName(pcs);
}

QByteArray Widget::get_input_data()
{
auto str1=ui->te_org->toPlainText();
// QString str1="ABC中国";
auto utf8 = QTextCodec::codecForName("UTF-8");
auto strUnicode= utf8->toUnicode(str1.toUtf8().data());
auto qtc = get_select_charset();
auto bs=qtc->fromUnicode(strUnicode);
return bs;
}

void Widget::on_cb_sha_currentTextChanged(const QString &arg1)
{
auto bs=get_input_data();
auto sha=QCryptographicHash::hash(bs, g_sha_fn[arg1]);
ui->te_sha->setText(sha.toHex().toUpper());
}
4 changes: 4 additions & 0 deletions widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ private slots:
void on_btn_md5_clicked();

void on_cb_cl_stateChanged(int arg1);
void on_cb_sha_currentTextChanged(const QString &arg1);

private:
QTextCodec* get_select_charset();
//获取输入的数据
QByteArray get_input_data();
private:
Ui::Widget *ui;
};
Expand Down
83 changes: 61 additions & 22 deletions widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<height>670</height>
</rect>
</property>
<property name="windowTitle">
<string>滑稽专用字符转换工具</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>要转换的字符串</string>
</property>
</widget>
</item>
<item row="1" column="0" rowspan="2" colspan="2">
<item row="1" column="0" rowspan="2">
<widget class="QTextEdit" name="te_org">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
Expand All @@ -32,10 +32,10 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="1" column="2">
<item row="1" column="1">
<widget class="QComboBox" name="cb_cs"/>
</item>
<item row="2" column="2">
<item row="2" column="1" rowspan="2">
<widget class="QPushButton" name="btn_encode">
<property name="text">
<string>转换↓</string>
Expand All @@ -49,91 +49,130 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="0" rowspan="2">
<widget class="QTextEdit" name="te_hex"/>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="cb_cl">
<property name="text">
<string>C语言数组</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QTextEdit" name="te_hex"/>
</item>
<item row="4" column="2">
<item row="5" column="1">
<widget class="QPushButton" name="btn_hex">
<property name="text">
<string>转换↑</string>
</property>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>URL</string>
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<item row="7" column="0">
<widget class="QTextEdit" name="te_url"/>
</item>
<item row="6" column="2">
<item row="7" column="1">
<widget class="QPushButton" name="btn_url">
<property name="text">
<string>转换↑</string>
</property>
</widget>
</item>
<item row="7" column="0">
<item row="8" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>base64</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="9" column="0">
<widget class="QTextEdit" name="te_base64"/>
</item>
<item row="8" column="2">
<item row="9" column="1">
<widget class="QPushButton" name="btn_base64">
<property name="text">
<string>转换↑</string>
</property>
</widget>
</item>
<item row="9" column="0" colspan="2">
<item row="10" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Unicode(UTF-16 小端模式)</string>
</property>
</widget>
</item>
<item row="10" column="0" colspan="2">
<item row="11" column="0">
<widget class="QTextEdit" name="te_uni"/>
</item>
<item row="10" column="2">
<item row="11" column="1">
<widget class="QPushButton" name="btn_uni">
<property name="text">
<string>转换↑</string>
</property>
</widget>
</item>
<item row="11" column="0">
<item row="12" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>MD5</string>
</property>
</widget>
</item>
<item row="12" column="0" colspan="2">
<item row="13" column="0">
<widget class="QTextEdit" name="te_md5"/>
</item>
<item row="12" column="2">
<item row="13" column="1">
<widget class="QPushButton" name="btn_md5">
<property name="text">
<string>转换↑</string>
</property>
</widget>
</item>
<item row="14" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>SHA</string>
</property>
</widget>
</item>
<item row="15" column="0">
<widget class="QTextEdit" name="te_sha"/>
</item>
<item row="15" column="1">
<widget class="QComboBox" name="cb_sha">
<item>
<property name="text">
<string>Sha1</string>
</property>
</item>
<item>
<property name="text">
<string>Sha224</string>
</property>
</item>
<item>
<property name="text">
<string>Sha256</string>
</property>
</item>
<item>
<property name="text">
<string>Sha384</string>
</property>
</item>
<item>
<property name="text">
<string>Sha512</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down

0 comments on commit 848c778

Please sign in to comment.