aboutsummaryrefslogtreecommitdiffstats
path: root/src/m_paper_key/PaperKeyModule.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-07-28 15:01:41 +0000
committersaturneric <[email protected]>2024-07-28 15:01:41 +0000
commitb38441115629e765ec5c4d82b0ca5c9ad7738c88 (patch)
treed11a3ba0d81776db6017d596eaacbc6a1cf697ed /src/m_paper_key/PaperKeyModule.cpp
parentfix: problems in infrastructure in some cases (diff)
downloadModules-b38441115629e765ec5c4d82b0ca5c9ad7738c88.tar.gz
Modules-b38441115629e765ec5c4d82b0ca5c9ad7738c88.zip
feat: paper key module is now useable
Diffstat (limited to 'src/m_paper_key/PaperKeyModule.cpp')
-rw-r--r--src/m_paper_key/PaperKeyModule.cpp97
1 files changed, 64 insertions, 33 deletions
diff --git a/src/m_paper_key/PaperKeyModule.cpp b/src/m_paper_key/PaperKeyModule.cpp
index c118bd9..695e149 100644
--- a/src/m_paper_key/PaperKeyModule.cpp
+++ b/src/m_paper_key/PaperKeyModule.cpp
@@ -28,10 +28,13 @@
#include "PaperKeyModule.h"
+#include <unistd.h>
+
#include <QtCore>
#include "GFModuleDefine.h"
#include "extract.h"
+#include "restore.h"
GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.paper_key", "PaperKey",
"1.0.0", "Integrated PaperKey Functions.", "Saturneric")
@@ -52,70 +55,98 @@ EXECUTE_MODULE() {
FLOG_DEBUG("paper key module executing, event id: %1", event["event_id"]);
if (event["event_id"] == "REQUEST_TRANS_KEY_2_PAPER_KEY") {
- if (event["secret_key"].isEmpty() || event["output_path"].isEmpty()) {
- CB_ERR(event, -1, "secret key or output path is empty");
- }
+ if (event["secret_key"].isEmpty()) CB_ERR(event, -1, "secret key is empty");
- QByteArray secret_key_data =
- QByteArray::fromBase64(event["secret_key"].toUtf8());
+ QByteArray secret_key_rdata =
+ QByteArray::fromBase64(event["secret_key"].toLatin1());
QTemporaryFile secret_key_t_file;
- if (!secret_key_t_file.open()) {
+ if (!secret_key_t_file.open())
CB_ERR(event, -1, "unable to open temporary file");
- }
- secret_key_t_file.write(secret_key_data);
+ secret_key_t_file.write(secret_key_rdata);
secret_key_t_file.flush();
- secret_key_t_file.seek(0);
+ secret_key_t_file.reset();
- FILE *file = fdopen(secret_key_t_file.handle(), "rb");
- if (file == nullptr) {
+ FILE *secret_key_file = fdopen(secret_key_t_file.handle(), "rb");
+ if (secret_key_file == nullptr)
CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
- }
- extract(file, event["output_path"].toUtf8(), AUTO);
+ QTemporaryFile paper_key_t_file;
+ if (!paper_key_t_file.open())
+ CB_ERR(event, -1, "unable to open temporary file");
+
+ FILE *paper_key_fp = fdopen(dup(paper_key_t_file.handle()), "w");
+ if (paper_key_fp == nullptr)
+ CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
+
+ auto ret = extract(secret_key_file, paper_key_fp, BASE16);
+ paper_key_t_file.flush();
+ paper_key_t_file.reset();
+
+ CB(event, GFGetModuleID(),
+ {
+ {"ret", QString::number(ret)},
+ {"paper_key", QString::fromLatin1(paper_key_t_file.readAll())},
+ });
+ return ret;
+ }
- fclose(file);
- } else if (event["event_id"] == "REQUEST_TRANS_PAPER_KEY_2_KEY") {
- if (event["public_key"].isEmpty() || event["paper_key_secrets"].isEmpty()) {
+ if (event["event_id"] == "REQUEST_TRANS_PAPER_KEY_2_KEY") {
+ if (event["public_key"].isEmpty() || event["paper_key_secrets"].isEmpty())
CB_ERR(event, -1, "public key or paper key secrets is empty");
- }
QByteArray public_key_data =
- QByteArray::fromBase64(event["public_key"].toUtf8());
+ QByteArray::fromBase64(event["public_key"].toLatin1());
QTemporaryFile public_key_t_file;
- if (!public_key_t_file.open()) {
+ if (!public_key_t_file.open())
CB_ERR(event, -1, "unable to open temporary file");
- }
public_key_t_file.write(public_key_data);
public_key_t_file.flush();
public_key_t_file.seek(0);
FILE *pubring = fdopen(public_key_t_file.handle(), "rb");
- if (pubring == nullptr) {
+ if (pubring == nullptr)
CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
- }
QByteArray secrets_data =
- QByteArray::fromBase64(event["paper_key_secrets"].toUtf8());
+ QByteArray::fromBase64(event["paper_key_secrets"].toLatin1());
- QTemporaryFile secrets_data_file;
- if (!secrets_data_file.open()) {
+ QTemporaryFile secrets_data_t_file;
+ if (!secrets_data_t_file.open())
CB_ERR(event, -1, "unable to open temporary file");
- }
- secrets_data_file.write(public_key_data);
- secrets_data_file.flush();
- secrets_data_file.seek(0);
+ secrets_data_t_file.write(secrets_data);
+ secrets_data_t_file.flush();
+ secrets_data_t_file.reset();
- FILE *secrets = fdopen(secrets_data_file.handle(), "rb");
- if (secrets == nullptr) {
+ FILE *secrets_fp = fdopen(secrets_data_t_file.handle(), "r");
+ if (secrets_fp == nullptr)
CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
- }
- restore(pubring, secrets, AUTO, )
+ QTemporaryFile secret_key_t_file;
+ if (!secret_key_t_file.open())
+ CB_ERR(event, -1, "unable to open temporary file");
+
+ FILE *secret_key_fp = fdopen(dup(secret_key_t_file.handle()), "wb");
+ if (secret_key_fp == nullptr)
+ CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
+
+ auto ret = restore(pubring, secrets_fp, AUTO, secret_key_fp);
+ secret_key_t_file.reset();
+ FLOG_DEBUG("secret key temp file size: %1, ret: %2",
+ secret_key_t_file.size(), ret);
+
+ CB(event, GFGetModuleID(),
+ {
+ {"ret", QString::number(ret)},
+ {"secret_key",
+ QString::fromLocal8Bit(secret_key_t_file.readAll().toBase64())},
+ });
+
+ return ret;
}
CB_SUCC(event);