aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2022-03-12 07:00:14 +0000
committerSaturneric <[email protected]>2022-03-12 07:00:14 +0000
commitf129055af41c44eed2a6423dfaf52c054458f0b4 (patch)
treed117164d3e9a2af58a60de4c58f225938245bc04
parentMerge branch 'develop-2.0.5' of git.codesdream.com:GpgFrontend into develop-2... (diff)
downloadGpgFrontend-f129055af41c44eed2a6423dfaf52c054458f0b4.tar.gz
GpgFrontend-f129055af41c44eed2a6423dfaf52c054458f0b4.zip
<fix>(core): Fix the existing problem of the key package
1. Fix the password generation function 2. Add some log output
-rw-r--r--src/core/function/KeyPackageOperator.cpp12
-rw-r--r--src/core/function/PassphraseGenerator.h14
2 files changed, 24 insertions, 2 deletions
diff --git a/src/core/function/KeyPackageOperator.cpp b/src/core/function/KeyPackageOperator.cpp
index 7ff65b50..2b2802f7 100644
--- a/src/core/function/KeyPackageOperator.cpp
+++ b/src/core/function/KeyPackageOperator.cpp
@@ -39,6 +39,7 @@ namespace GpgFrontend {
bool KeyPackageOperator::GeneratePassphrase(
const std::filesystem::path& phrase_path, std::string& phrase) {
phrase = PassphraseGenerator::GetInstance().Generate(256);
+ LOG(INFO) << "Generated passphrase: " << phrase.size() << " bytes";
return FileOperator::WriteFileStd(phrase_path, phrase);
}
@@ -46,9 +47,12 @@ bool KeyPackageOperator::GenerateKeyPackage(
const std::filesystem::path& key_package_path,
const std::string& key_package_name, KeyIdArgsListPtr& key_ids,
std::string& phrase, bool secret) {
+ LOG(INFO) << "Generating key package: " << key_package_name;
+
ByteArrayPtr key_export_data = nullptr;
if (!GpgKeyImportExporter::GetInstance().ExportKeys(key_ids, key_export_data,
secret)) {
+ LOG(ERROR) << "Failed to export keys";
return false;
}
@@ -60,6 +64,7 @@ bool KeyPackageOperator::GenerateKeyPackage(
QAESEncryption::Padding::ISO);
auto encoded = encryption.encode(data, hash_key);
+ LOG(INFO) << "Writing key package: " << key_package_name;
return FileOperator::WriteFileStd(key_package_path, encoded.toStdString());
}
@@ -67,16 +72,22 @@ bool KeyPackageOperator::ImportKeyPackage(
const std::filesystem::path& key_package_path,
const std::filesystem::path& phrase_path,
GpgFrontend::GpgImportInformation& import_info) {
+
+ LOG(INFO) << "Importing key package: " << key_package_path.string();
+
std::string encrypted_data;
FileOperator::ReadFileStd(key_package_path, encrypted_data);
if (encrypted_data.empty()) {
+ LOG(ERROR) << "Failed to read key package: " << key_package_path.string();
return false;
};
std::string passphrase;
FileOperator::ReadFileStd(phrase_path, passphrase);
+ LOG(INFO) << "Passphrase: " << passphrase.size() << " bytes";
if (passphrase.size() != 256) {
+ LOG(ERROR) << "Failed to read passphrase: " << phrase_path.string();
return false;
}
@@ -90,6 +101,7 @@ bool KeyPackageOperator::ImportKeyPackage(
auto decoded = encryption.removePadding(encryption.decode(encoded, hash_key));
auto key_data = QByteArray::fromBase64(decoded);
+ LOG(INFO) << "key data" << key_data.size();
if (!key_data.startsWith(GpgConstants::PGP_PUBLIC_KEY_BEGIN) &&
!key_data.startsWith(GpgConstants::PGP_PRIVATE_KEY_BEGIN)) {
return false;
diff --git a/src/core/function/PassphraseGenerator.h b/src/core/function/PassphraseGenerator.h
index 5e55b2dd..d1cc7607 100644
--- a/src/core/function/PassphraseGenerator.h
+++ b/src/core/function/PassphraseGenerator.h
@@ -42,7 +42,6 @@ namespace GpgFrontend {
class PassphraseGenerator
: public SingletonFunctionObject<PassphraseGenerator> {
public:
-
/**
* @brief PassphraseGenerator constructor
*
@@ -60,8 +59,19 @@ class PassphraseGenerator
*/
std::string Generate(int len) {
std::uniform_int_distribution<int> dist(999, 99999);
+
auto file_string = boost::format("KeyPackage_%1%") % dist(mt_);
- return file_string.str();
+ static const char alphanum[] =
+ "0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz";
+ std::string tmp_str;
+ tmp_str.reserve(len);
+
+ for (int i = 0; i < len; ++i) {
+ tmp_str += alphanum[dist(mt_) % (sizeof(alphanum) - 1)];
+ }
+ return tmp_str;
}
std::random_device rd_; ///< Random device