| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
** EPITECH PROJECT, 2024 |
| 3 |
|
|
** zappy/ai |
| 4 |
|
|
** File description: |
| 5 |
|
|
** message.hpp |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#include "Message.hpp" |
| 9 |
|
|
|
| 10 |
|
✗ |
Message::Message(const std::string &contentMsg) |
| 11 |
|
|
{ |
| 12 |
|
✗ |
content = contentMsg; |
| 13 |
|
✗ |
} |
| 14 |
|
|
|
| 15 |
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 |
Message::Message() |
| 16 |
|
|
{ |
| 17 |
|
4 |
} |
| 18 |
|
|
|
| 19 |
|
4 |
Message::~Message() |
| 20 |
|
|
{ |
| 21 |
|
4 |
} |
| 22 |
|
|
|
| 23 |
|
1 |
std::string Message::convertDigitsToLetters(const std::string &digits) |
| 24 |
|
|
{ |
| 25 |
|
1 |
const std::string conversionTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 26 |
|
|
|
| 27 |
|
|
std::string letters; |
| 28 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 |
for (char digit : digits) |
| 29 |
|
|
{ |
| 30 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
if (isdigit(digit)) |
| 31 |
|
|
{ |
| 32 |
|
3 |
unsigned int index = digit - '0'; |
| 33 |
|
|
|
| 34 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
if (index < conversionTable.size()) |
| 35 |
|
|
{ |
| 36 |
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
letters += conversionTable[index]; |
| 37 |
|
|
} |
| 38 |
|
|
} |
| 39 |
|
|
} |
| 40 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 |
for (unsigned int i = 0; i < letters.length() / 2; i++) |
| 41 |
|
1 |
std::swap(letters[i], letters[letters.length() - i - 1]); |
| 42 |
|
1 |
return letters; |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
✗ |
std::string Message::getCurrentTimeAsLetters() |
| 46 |
|
|
{ |
| 47 |
|
✗ |
std::time_t now = std::time(nullptr); |
| 48 |
|
✗ |
char buf[20]; |
| 49 |
|
✗ |
std::strftime(buf, sizeof(buf), "%Y%m%d%H%M", std::localtime(&now)); |
| 50 |
|
✗ |
std::string digits(buf); |
| 51 |
|
✗ |
return convertDigitsToLetters(digits); |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
|
1 |
void Message::generateMessage() |
| 55 |
|
|
{ |
| 56 |
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 |
content = content + ":" + _signature; |
| 57 |
|
1 |
} |
| 58 |
|
|
|
| 59 |
|
✗ |
void Message::format(const std::string &contentToFormat) |
| 60 |
|
|
{ |
| 61 |
|
✗ |
content = contentToFormat; |
| 62 |
|
✗ |
vigenereEncrypt(); |
| 63 |
|
✗ |
generateMessage(); |
| 64 |
|
✗ |
} |
| 65 |
|
|
|
| 66 |
|
1 |
void Message::vigenereEncrypt() |
| 67 |
|
|
{ |
| 68 |
|
1 |
std::string result = content; |
| 69 |
|
|
int keyIndex = 0; |
| 70 |
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 |
const std::string &key = "KIMUCHI"; |
| 71 |
|
|
|
| 72 |
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 |
for (unsigned int i = 0; i < content.length(); ++i) |
| 73 |
|
|
{ |
| 74 |
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 |
if (isalpha(content[i])) |
| 75 |
|
|
{ |
| 76 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 |
char base = islower(content[i]) ? 'a' : 'A'; |
| 77 |
|
5 |
char shiftedChar = (content[i] - base + tolower(key[keyIndex]) - 'a') % 26 + base; |
| 78 |
|
5 |
result[i] = shiftedChar; |
| 79 |
|
5 |
keyIndex = (keyIndex + 1) % key.length(); |
| 80 |
|
|
} |
| 81 |
|
|
} |
| 82 |
|
|
content = result; |
| 83 |
|
1 |
} |
| 84 |
|
|
|
| 85 |
|
1 |
void Message::vigenereDecrypt() |
| 86 |
|
|
{ |
| 87 |
|
1 |
std::string result = content; |
| 88 |
|
|
int keyIndex = 0; |
| 89 |
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 |
const std::string &key = "KIMUCHI"; |
| 90 |
|
|
|
| 91 |
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 |
for (unsigned int i = 0; i < content.length(); ++i) |
| 92 |
|
|
{ |
| 93 |
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 |
if (isalpha(content[i])) |
| 94 |
|
|
{ |
| 95 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 |
char base = islower(content[i]) ? 'a' : 'A'; |
| 96 |
|
5 |
char shiftedChar = content[i] - base; |
| 97 |
|
5 |
char keyOffset = tolower(key[keyIndex]) - 'a'; |
| 98 |
|
|
|
| 99 |
|
5 |
char originalChar = (shiftedChar - keyOffset + 26) % 26 + base; |
| 100 |
|
5 |
result[i] = originalChar; |
| 101 |
|
|
|
| 102 |
|
5 |
keyIndex = (keyIndex + 1) % key.length(); |
| 103 |
|
|
} |
| 104 |
|
|
} |
| 105 |
|
|
content = result; |
| 106 |
|
1 |
} |
| 107 |
|
|
|