-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.cpp
62 lines (53 loc) · 2.24 KB
/
data.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<data.h>
#include<QDebug>
#define t(str) QString::fromLocal8Bit(str)
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CBC, QAESEncryption::ISO);
QByteArray AESEncrypt(QString content, QString key) {
QByteArray hashKey=QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha256);
QByteArray hashIV = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);
return encryption.encode(content.toUtf8(), hashKey,hashIV);
}
QString AESDecrypt(QByteArray content, QString key) {
QByteArray hashKey = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha256);
QByteArray hashIV = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);
return QString::fromUtf8(encryption.removePadding(encryption.decode(content, hashKey, hashIV)));
}
QString QModel2QString(QStandardItemModel* model) {
QString result;
for (int y = 0; y < model->rowCount(); y++) {
result.append(model->item(y, 0)->text());
result.append("\t");
result.append(model->item(y, 1)->text());
result.append("\t");
result.append(model->item(y, 2)->data(Qt::UserRole).toString());
result.append("\n");
}
result.chop(1);
return result;
}
void QString2QModel(QString content,QStandardItemModel *model) {
model->clear();
model->setColumnCount(3);
model->setHorizontalHeaderLabels({ t("ƽ̨"),t("Óû§Ãû"),t("ÃÜÂë") });
if (content.isEmpty())return;
foreach(QString column, content.split("\n")) {
QStringList items = column.split("\t");
QStandardItem* password = new QStandardItem(QString(items.at(2)).fill('*'));
password->setData(items.at(2), Qt::UserRole);
model->appendRow({new QStandardItem(items.at(0)),new QStandardItem(items.at(1)), password});
}
}
bool saveData(QString path,QStandardItemModel* model,QString key) {
QFile file(path);
if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate))return false;
file.write(AESEncrypt(QModel2QString(model).append("EOF"), key));
return true;
}
bool readData(QString path, QStandardItemModel* model,QString key) {
QFile file(path);
if(!file.open(QIODevice::ReadOnly))return false;
QString table = AESDecrypt(file.readAll(), key);
if (!table.endsWith("EOF"))return false;
QString2QModel(table.chopped(3), model);
return true;
}