-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
179 lines (149 loc) · 8.28 KB
/
test.cpp
File metadata and controls
179 lines (149 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <string_view>
#include <cipher/alphabet.hpp>
#include <cipher/base64.hpp>
#include <cipher/cipher.hpp>
#include <cipher/entropy.hpp>
#include <cipher/vigenere.hpp>
#include <cipher/xor.hpp>
using namespace std::string_view_literals;
namespace test
{
namespace vigenere
{
constexpr static const auto vigenere_alphabet = cipher::alphabet::create("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
constexpr static const auto vigenere_ascii_to_index = cipher::alphabet::create_ascii_to_index_array(vigenere_alphabet);
constexpr static const auto vigenere_decoding_table = cipher::vigenere::create_decode_table(vigenere_alphabet, vigenere_ascii_to_index);
constexpr static const auto vigenere_encoding_table = cipher::vigenere::create_table(vigenere_alphabet);
constexpr static auto key = cipher::buffer("FORTIFICATION");
template<std::size_t len, typename charT>
constexpr static auto encrypt_vigenere_table(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::encode<false>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
vigenere_encoding_table,
vigenere_ascii_to_index);
return ciphertext;
}
template<std::size_t len, typename charT>
constexpr static auto encrypt_vigenere(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::encode<false>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
std::span{ vigenere_alphabet },
vigenere_ascii_to_index);
return ciphertext;
}
template<std::size_t len, typename charT>
constexpr static auto decrypt_vigenere_table(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::decode<false>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
vigenere_decoding_table,
vigenere_ascii_to_index);
return ciphertext;
}
template<std::size_t len, typename charT>
constexpr static auto decrypt_vigenere(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::decode<false>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
std::span{ vigenere_alphabet },
vigenere_ascii_to_index);
return ciphertext;
}
constexpr static auto test_vigenere_table_1 = encrypt_vigenere_table(cipher::buffer("DEFENDTHEEASTWALLOFTHECASTLE"));
static_assert(cipher::to_string(test_vigenere_table_1) == "ISWXVIBJEXIGGBOCEWKBJEVIGGQS"sv, cipher::to_string(test_vigenere_table_1));
constexpr static auto test_vigenere_table_2 = decrypt_vigenere_table(cipher::buffer("ISWXVIBJEXIGGBOCEWKBJEVIGGQS"));
static_assert(cipher::to_string(test_vigenere_table_2) == "DEFENDTHEEASTWALLOFTHECASTLE"sv, cipher::to_string(test_vigenere_table_2));
constexpr static auto test_vigenere_1 = encrypt_vigenere(cipher::buffer("DEFENDTHEEASTWALLOFTHECASTLE"));
static_assert(cipher::to_string(test_vigenere_1) == "ISWXVIBJEXIGGBOCEWKBJEVIGGQS"sv, cipher::to_string(test_vigenere_1));
constexpr static auto test_vigenere_2 = decrypt_vigenere(cipher::buffer("ISWXVIBJEXIGGBOCEWKBJEVIGGQS"));
static_assert(cipher::to_string(test_vigenere_2) == "DEFENDTHEEASTWALLOFTHECASTLE"sv, cipher::to_string(test_vigenere_2));
template<std::size_t len, typename charT>
constexpr static auto encrypt_autokey_table(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::encode<true>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
vigenere_encoding_table,
vigenere_ascii_to_index);
return ciphertext;
}
template<std::size_t len, typename charT>
constexpr static auto decrypt_autokey_table(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::decode<true>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
vigenere_decoding_table,
vigenere_ascii_to_index);
return ciphertext;
}
constexpr static auto test_autokey_table_1 = encrypt_autokey_table(cipher::buffer("DEFENDTHEEASTWALLOFTHECASTLE"));
static_assert(cipher::to_string(test_autokey_table_1) == "ISWXVIBJEXIGGZEQPBIMOIGAKMHE"sv, cipher::to_string(test_autokey_table_1));
constexpr static auto test_autokey_table_2 = decrypt_autokey_table(cipher::buffer("ISWXVIBJEXIGGZEQPBIMOIGAKMHE"));
static_assert(cipher::to_string(test_autokey_table_2) == "DEFENDTHEEASTWALLOFTHECASTLE"sv, cipher::to_string(test_autokey_table_2));
template<std::size_t len, typename charT>
constexpr static auto encrypt_autokey(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::encode<true>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
std::span{ vigenere_alphabet },
vigenere_ascii_to_index);
return ciphertext;
}
template<std::size_t len, typename charT>
constexpr static auto decrypt_autokey(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len, charT>();
cipher::vigenere::decode<true>(std::span{ ciphertext },
std::span{ buffer },
std::span{ key },
std::span{ vigenere_alphabet },
vigenere_ascii_to_index);
return ciphertext;
}
constexpr static auto test_autokey_1 = encrypt_autokey(cipher::buffer("DEFENDTHEEASTWALLOFTHECASTLE"));
static_assert(cipher::to_string(test_autokey_1) == "ISWXVIBJEXIGGZEQPBIMOIGAKMHE"sv, cipher::to_string(test_autokey_1));
constexpr static auto test_autokey_2 = decrypt_autokey(cipher::buffer("ISWXVIBJEXIGGZEQPBIMOIGAKMHE"));
static_assert(cipher::to_string(test_autokey_2) == "DEFENDTHEEASTWALLOFTHECASTLE"sv, cipher::to_string(test_autokey_2));
}
namespace substitution
{
}
namespace base64
{
template<std::size_t len, typename charT>
constexpr static auto decode_b64(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len*3/4, charT>();
cipher::base64::decode(std::span{ ciphertext },
std::span{ buffer });
return ciphertext;
}
template<std::size_t len, typename charT>
constexpr static auto decode_b64_cexpr(const cipher::buffer_t<len, charT>& buffer)
{
auto ciphertext = cipher::empty_buffer<len*3/4, charT>();
cipher::base64::decode<cipher::base64::DEFAULT_ALPHABET>(std::span{ ciphertext },
std::span{ buffer });
return ciphertext;
}
constexpr static auto test_base64_1 = decode_b64(cipher::buffer("SGVsbG8gV29ybGRk"));
static_assert(cipher::to_string(test_base64_1) == "Hello Worldd"sv, cipher::to_string(test_base64_1));
constexpr static auto test_base64_2 = decode_b64_cexpr(cipher::buffer("SGVsbG8gV29ybGRk"));
static_assert(cipher::to_string(test_base64_2) == "Hello Worldd"sv, cipher::to_string(test_base64_2));
}
}