Code cleanup and moving functions out of classes that do not belong

This commit is contained in:
amazing-username
2017-07-09 21:44:30 -05:00
parent 6c1f92ea30
commit 33c23cfc10
11 changed files with 53 additions and 59 deletions
+25
View File
@@ -0,0 +1,25 @@
#ifndef CONVERSIONS_H_
#define CONVERSIONS_H_
#include<string>
#include<sstream>
class Conversions
{
public:
Conversions() = default;
std::string cstringToString(char[], const int);
private:
};
std::string Conversions::cstringToString(char tmp[], const int size)
{
std::string tmpString{};
std::stringstream cToS{};
for (auto index =0; index!=size; ++ index)
cToS << tmp[index];
cToS >> tmpString;
return tmpString;
}
#endif
+9 -22
View File
@@ -3,7 +3,7 @@
#include<sstream> #include<sstream>
#include<ios> #include<ios>
#include<vector> #include<vector>
#include"Conversions.h"
#include"Decryption.h" #include"Decryption.h"
#include"KeyRetrieval.h" #include"KeyRetrieval.h"
@@ -15,10 +15,8 @@ Decryption::Decryption(const std::string& message)
} }
void Decryption::setDecryptedMessage(const std::string& message) void Decryption::setDecryptedMessage(const std::string& message) { this->decryptedMessage = message; }
{ this->decryptedMessage = message; } void Decryption::setMessage(const std::string& message) { this->message = message; }
void Decryption::setMessage(const std::string& message)
{ this->message = message; }
void Decryption::decryptMessage() void Decryption::decryptMessage()
{ {
@@ -34,19 +32,17 @@ void Decryption::decryptMessage()
for (auto indexOfChar = 0u; indexOfChar<messageToBeDecrypted.size(); indexOfChar+=5) for (auto indexOfChar = 0u; indexOfChar<messageToBeDecrypted.size(); indexOfChar+=5)
{ {
char cryptedKey[5]{}; char cryptedKey[5]{};
for (auto index = 0; index!=5; ++index) for (auto index = 0; index!=keyLength; ++index)
cryptedKey[index] = (messageToBeDecrypted.at(indexOfChar + index)); cryptedKey[index] = (messageToBeDecrypted.at(indexOfChar + index));
Conversions cnvert;
decryptedMessage += decryptedCharacters[cstringToString(cryptedKey, 5)]; decryptedMessage += decryptedCharacters[cnvert.cstringToString(cryptedKey, 5)];
} }
setDecryptedMessage(decryptedMessage); setDecryptedMessage(decryptedMessage);
} }
std::string Decryption::getDecryptedMessage() const std::string Decryption::getDecryptedMessage() const { return decryptedMessage; }
{ return decryptedMessage; } std::string Decryption::getMessage() const { return message; }
std::string Decryption::getMessage() const
{ return message; }
void Decryption::setupMap() void Decryption::setupMap()
{ {
@@ -54,15 +50,6 @@ void Decryption::setupMap()
std::vector<std::string> k{kr.keyStructure()}; std::vector<std::string> k{kr.keyStructure()};
std::vector<int> c{kr.codeCharacterStructure()}; std::vector<int> c{kr.codeCharacterStructure()};
for (auto index = 0; index!=c.size(); ++index) for (auto index = 0u; index!=c.size(); ++index)
decryptedCharacters[k[index]] = c[index]; decryptedCharacters[k[index]] = c[index];
} }
std::string Decryption::cstringToString(char tmp[], const int& SIZE)
{
std::string tmpString{};
std::stringstream cToS{};
for (auto index = 0; index!=SIZE; ++index)
cToS << tmp[index];
cToS >> tmpString;
return tmpString;
}
+1 -2
View File
@@ -1,7 +1,6 @@
#ifndef DECRYPTION_H #ifndef DECRYPTION_H
#define DECRYPTION_H #define DECRYPTION_H
#include<fstream>
#include<string> #include<string>
#include<map> #include<map>
#include"Cryption.h" #include"Cryption.h"
@@ -21,9 +20,9 @@ public:
std::string getMessage() const override; std::string getMessage() const override;
private: private:
void setupMap(); void setupMap();
std::string cstringToString(char[], const int&);
std::string decryptedMessage; std::string decryptedMessage;
std::map<std::string, char> decryptedCharacters; std::map<std::string, char> decryptedCharacters;
const int keyLength{5};
}; };
#endif #endif
+1 -1
View File
@@ -9,7 +9,7 @@ QT += widgets
INCLUDEPATH += . INCLUDEPATH += .
# Input # Input
HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h KeyRetrieval.h HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h KeyRetrieval.h Conversions.h
SOURCES += Decryption.cpp \ SOURCES += Decryption.cpp \
Encryption.cpp \ Encryption.cpp \
Cryption.cpp \ Cryption.cpp \
+5 -10
View File
@@ -2,7 +2,6 @@
#include<iostream> #include<iostream>
#include<fstream> #include<fstream>
#include<string> #include<string>
#include"Encryption.h" #include"Encryption.h"
#include"GenerateKeys.h" #include"GenerateKeys.h"
#include"KeyRetrieval.h" #include"KeyRetrieval.h"
@@ -16,10 +15,8 @@ Encryption::Encryption(const std::string& message)
} }
void Encryption::setEncryptedMessage(const std::string& encryptedMessage) void Encryption::setEncryptedMessage(const std::string& encryptedMessage) { this->encryptedMessage = encryptedMessage; }
{ this->encryptedMessage = encryptedMessage; } void Encryption::setMessage(const std::string& message) { this->message = message; }
void Encryption::setMessage(const std::string& message)
{ this->message = message; }
void Encryption::encryptMessage() void Encryption::encryptMessage()
{ {
std::fstream ioEvent{}; std::fstream ioEvent{};
@@ -41,11 +38,9 @@ void Encryption::setupMap()
std::vector<std::string> k{kr.keyStructure()}; std::vector<std::string> k{kr.keyStructure()};
std::vector<int> c{kr.codeCharacterStructure()}; std::vector<int> c{kr.codeCharacterStructure()};
for (auto index = 0; index!=k.size(); ++index) for (auto index = 0u; index!=k.size(); ++index)
encryptedCharacters[c[index]] = k[index]; encryptedCharacters[c[index]] = k[index];
} }
std::string Encryption::getEncryptedMessage() const std::string Encryption::getEncryptedMessage() const { return encryptedMessage; }
{ return encryptedMessage; } std::string Encryption::getMessage() const { return message; }
std::string Encryption::getMessage() const
{ return message; }
std::map<char, std::string> Encryption::encryptedCharactersStructure() { return encryptedCharacters; } std::map<char, std::string> Encryption::encryptedCharactersStructure() { return encryptedCharacters; }
-2
View File
@@ -1,7 +1,6 @@
#ifndef ENCRYPTION_H #ifndef ENCRYPTION_H
#define ENCRYPTION_H #define ENCRYPTION_H
#include<fstream>
#include<string> #include<string>
#include<map> #include<map>
#include"Cryption.h" #include"Cryption.h"
@@ -25,5 +24,4 @@ private:
std::string encryptedMessage; std::string encryptedMessage;
std::map<char, std::string> encryptedCharacters; std::map<char, std::string> encryptedCharacters;
}; };
#endif #endif
+3 -4
View File
@@ -2,7 +2,6 @@
#include<sstream> #include<sstream>
#include<fstream> #include<fstream>
#include<ios> #include<ios>
#include"GenerateKeys.h" #include"GenerateKeys.h"
GenerateKeys::GenerateKeys() GenerateKeys::GenerateKeys()
@@ -93,7 +92,7 @@ void GenerateKeys::generateKey(std::string& key, const int& SIZE)
} }
void GenerateKeys::keyMove() void GenerateKeys::keyMove()
{ {
for (auto index = 0; index!=allCharacters.size(); ++index) for (auto index = 0u; index!=allCharacters.size(); ++index)
{ {
std::string tmpKey{}; std::string tmpKey{};
generateKey(tmpKey, keySize); generateKey(tmpKey, keySize);
@@ -115,11 +114,11 @@ void GenerateKeys::print(const std::vector<int>& ch)
} }
bool GenerateKeys::isThereRepetition() bool GenerateKeys::isThereRepetition()
{ {
for (auto index = 0; index!=allCharacters.size(); ++index) for (auto index = 0u; index!=allCharacters.size(); ++index)
{ {
std::string chosenOne{keys[index]}; std::string chosenOne{keys[index]};
auto repetition{0}; auto repetition{0};
for (auto innerIndex = 0; innerIndex!=allCharacters.size(); ++innerIndex) for (auto innerIndex = 0u; innerIndex!=allCharacters.size(); ++innerIndex)
if (chosenOne == keys[innerIndex]) if (chosenOne == keys[innerIndex])
++repetition; ++repetition;
if (repetition > 1) if (repetition > 1)
+4 -5
View File
@@ -1,8 +1,3 @@
#include"KeyManagementWindow.h"
#include"Encryption.h"
#include"GenerateKeys.h"
#include"KeyRetrieval.h"
#include<QtWidgets> #include<QtWidgets>
#include<QString> #include<QString>
#include<QWidget> #include<QWidget>
@@ -12,6 +7,10 @@
#include<cstdlib> #include<cstdlib>
#include<fstream> #include<fstream>
#include<ios> #include<ios>
#include"KeyManagementWindow.h"
#include"Encryption.h"
#include"GenerateKeys.h"
#include"KeyRetrieval.h"
KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent) KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent)
{ {
+2 -2
View File
@@ -35,8 +35,8 @@ private:
unique_ptr<QComboBox> comboBoxOfKeys; unique_ptr<QComboBox> comboBoxOfKeys;
unique_ptr<QLineEdit> valueOfKey; unique_ptr<QLineEdit> valueOfKey;
unique_ptr<QPushButton> generateNewDefaultKeys; unique_ptr<QPushButton> generateNewDefaultKeys;
int windowWidth{400}; const int windowWidth{400};
int windowHeight{400}; const int windowHeight{400};
}; };
#endif #endif
+2 -10
View File
@@ -18,11 +18,9 @@ void KeyRetrieval::retrieveKey()
{ {
readKeys.getline(keyChar, '\n'); readKeys.getline(keyChar, '\n');
if (keyChar[0]!='\0') if (keyChar[0]!='\0')
{
keys.push_back(keyChar); keys.push_back(keyChar);
} }
} }
}
void KeyRetrieval::addToList() void KeyRetrieval::addToList()
{ {
addRange(10, 10); addRange(10, 10);
@@ -42,11 +40,5 @@ void KeyRetrieval::addRange(int from, const int to)
codeCharacter.push_back(from); codeCharacter.push_back(from);
} }
std::vector<std::string> KeyRetrieval::keyStructure() std::vector<std::string> KeyRetrieval::keyStructure() { return keys; }
{ std::vector<int> KeyRetrieval::codeCharacterStructure() { return codeCharacter; }
return keys;
}
std::vector<int> KeyRetrieval::codeCharacterStructure()
{
return codeCharacter;
}
+1 -1
View File
@@ -22,6 +22,6 @@ private:
std::vector<std::string> keys; std::vector<std::string> keys;
std::vector<char> characters; std::vector<char> characters;
std::vector<int> codeCharacter; std::vector<int> codeCharacter;
int keySize{5}; const int keySize{5};
}; };
#endif #endif