1 //**********************************************************************************
2 //EncryptPad Copyright 2016 Evgeny Pokhilko
3 //<http://www.evpo.net/encryptpad>
4 //
5 //This file is part of EncryptPad
6 //
7 //EncryptPad is free software: you can redistribute it and/or modify
8 //it under the terms of the GNU General Public License as published by
9 //the Free Software Foundation, either version 2 of the License, or
10 //(at your option) any later version.
11 //
12 //EncryptPad is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with EncryptPad.  If not, see <http://www.gnu.org/licenses/>.
19 //**********************************************************************************
20 #include "file_properties_dialog.h"
21 #include "ui_file_properties_dialog.h"
22 #include <QDebug>
23 #include <QtGlobal>
24 #include <sstream>
25 #include "common_definitions.h"
26 #include "encryptmsg/openpgp_conversions.h"
27 
28 namespace
29 {
30     template<class T>
ToInt(T v)31     int ToInt(T v)
32     {
33         return static_cast<int>(v);
34     }
35 
36     struct Name2Id
37     {
38         const char *name;
39         int id;
40     };
41 
PopulateCombo(QComboBox * c,Name2Id * p)42     void PopulateCombo(QComboBox *c, Name2Id *p)
43     {
44         c->clear();
45         while(p->name)
46         {
47             c->addItem(qApp->translate("FilePropertiesDialog", p->name), ToInt(p->id));
48             p++;
49         }
50     }
51 
52     template<class T>
SetCurrent(QComboBox * c,T v)53     void SetCurrent(QComboBox *c, T v)
54     {
55         c->setCurrentIndex(c->findData(ToInt(v)));
56     }
57 }
58 
FilePropertiesDialog(QWidget * parent)59 FilePropertiesDialog::FilePropertiesDialog(QWidget *parent) :
60     QDialog(parent, kDefaultWindowFlags),
61     ui(new Ui::FilePropertiesDialog), isDirty(false)
62 {
63     ui->setupUi(this);
64     PopulateItems();
65     connect(ui->uiCipherAlgo, SIGNAL(currentIndexChanged(int)), this, SLOT(PropertyChanged()));
66     connect(ui->uiHashAlgo, SIGNAL(currentIndexChanged(int)), this, SLOT(PropertyChanged()));
67     connect(ui->uiCompressionAlgo, SIGNAL(currentIndexChanged(int)), this, SLOT(PropertyChanged()));
68     connect(ui->uiIterations, SIGNAL(currentIndexChanged(int)), this, SLOT(PropertyChanged()));
69 }
70 
~FilePropertiesDialog()71 FilePropertiesDialog::~FilePropertiesDialog()
72 {
73     delete ui;
74 }
75 
PopulateItems()76 void FilePropertiesDialog::PopulateItems()
77 {
78     using namespace EncryptMsg;
79     static Name2Id ciphers[] =
80     {
81         {"Triple DES", ToInt(CipherAlgo::TripleDES)},
82         {"CAST5", ToInt(CipherAlgo::CAST5)},
83         {"AES-128", ToInt(CipherAlgo::AES128)},
84         {"AES-192", ToInt(CipherAlgo::AES192)},
85         {QT_TRANSLATE_NOOP("FilePropertiesDialog", "AES-256 (recommended)"), ToInt(CipherAlgo::AES256)},
86         {"Twofish", ToInt(CipherAlgo::Twofish)},
87         {"Camellia-128", ToInt(CipherAlgo::Camellia128)},
88         {"Camellia-192", ToInt(CipherAlgo::Camellia192)},
89         {"Camellia-256", ToInt(CipherAlgo::Camellia256)},
90         {nullptr, 0}
91     };
92 
93     static Name2Id hashes[] =
94     {
95         {"SHA-1", ToInt(HashAlgo::SHA160)},
96         {"SHA-256", ToInt(HashAlgo::SHA256)},
97         {"SHA-384", ToInt(HashAlgo::SHA384)},
98         {"SHA-512", ToInt(HashAlgo::SHA512)},
99         {"SHA-224", ToInt(HashAlgo::SHA224)},
100         {nullptr, 0}
101     };
102 
103     static Name2Id compressions[] =
104     {
105         {QT_TRANSLATE_NOOP("FilePropertiesDialog", "Uncompressed"), ToInt(Compression::Uncompressed)},
106         {"ZIP", ToInt(Compression::ZIP)},
107         {"ZLIB", ToInt(Compression::ZLIB)},
108         {"BZip2", ToInt(Compression::BZip2)},
109         {nullptr, 0}
110     };
111 
112     PopulateCombo(ui->uiCipherAlgo, ciphers);
113     PopulateCombo(ui->uiHashAlgo, hashes);
114     PopulateCombo(ui->uiCompressionAlgo, compressions);
115 
116     for(unsigned int i = 0; i < 256; i++)
117     {
118         unsigned int iterations = DecodeS2KIterations(static_cast<unsigned char>(i));
119         ui->uiIterations->addItem(
120             QString::number(iterations), QVariant(iterations));
121     }
122 }
123 
124 
SetUiFromMetadata(const EncryptPad::PacketMetadata & metadata)125 void FilePropertiesDialog::SetUiFromMetadata(const EncryptPad::PacketMetadata &metadata)
126 {
127     SetCurrent(ui->uiCipherAlgo, metadata.cipher_algo);
128     SetCurrent(ui->uiHashAlgo, metadata.hash_algo);
129     SetCurrent(ui->uiCompressionAlgo, metadata.compression);
130     SetCurrent(ui->uiIterations, metadata.iterations);
131 
132     std::ostringstream stm;
133     auto it = metadata.salt.begin();
134     for(;it != metadata.salt.end(); it++)
135     {
136         if(it != metadata.salt.begin())
137             stm << " ";
138         stm << std::uppercase << std::hex << static_cast<int>(*it);
139     }
140     std::string s = stm.str();
141     QString qs = QString::fromStdString(s);
142     ui->uiSalt->setText(qs);
143     isDirty = false;
144 }
145 
GetIsDirty() const146 bool FilePropertiesDialog::GetIsDirty() const
147 {
148     return isDirty;
149 }
150 
UpdateMetadataFromUi(EncryptPad::PacketMetadata & metadata) const151 void FilePropertiesDialog::UpdateMetadataFromUi(EncryptPad::PacketMetadata &metadata) const
152 {
153     using namespace EncryptMsg;
154     metadata.cipher_algo = static_cast<CipherAlgo>(ui->uiCipherAlgo->currentData().toInt());
155     metadata.hash_algo = static_cast<HashAlgo>(ui->uiHashAlgo->currentData().toInt());
156     metadata.compression = static_cast<Compression>(ui->uiCompressionAlgo->currentData().toInt());
157     metadata.iterations = DecodeS2KIterations(EncodeS2KIterations(ui->uiIterations->currentData().toUInt()));
158 }
159 
PropertyChanged()160 void FilePropertiesDialog::PropertyChanged()
161 {
162    isDirty = true;
163 }
164