Created
March 26, 2014 22:20
-
-
Save rd13/f1c01c42b18b9e358223 to your computer and use it in GitHub Desktop.
Enigma C++ Perf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#include <array> | |
using namespace std; | |
const std::array<char, 27> alpha = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; | |
const std::array<std::array<char, 27>, 3> rotors | |
{ | |
{{"EKMFLGDQVZNTOWYHXUSPAIBRCJ"}, | |
{"AJDKSIRUXBLHWTMCQGZNPYFVOE"}, | |
{"BDFHJLCPRTXVZNYEIWGAKMUSQO"}} | |
}; | |
const std::array<char, 27> reflector = {"YRUHQSLDPXNGOKMIEBFZCWVJAT"}; | |
const std::array<char, 4> key = {"ABC"}; | |
long mod26(long a) | |
{ | |
return (a+26)%26; | |
} | |
int li (char l) | |
{ | |
return l - 'A'; | |
} | |
template <size_t N> | |
std::size_t index_of (const std::array<char, N>& str, int find) | |
{ | |
for(std::size_t i = 0; i < N; ++i) { | |
if(str[i] == find) return i; | |
} | |
return -1; | |
} | |
template <size_t N> | |
std::array<char, N> crypt (const std::array<char, N>& ct) | |
{ | |
int L = li(key[0]); | |
int M = li(key[1]); | |
int R = li(key[2]); | |
std::array<char, N> output; | |
for ( unsigned x = 0; x < N ; ++x ) { | |
int ct_letter = li(ct[x]); | |
R = mod26(R + 1); | |
char a = rotors[2][mod26(R + ct_letter)]; | |
char b = rotors[1][mod26(M + li(a) - R)]; | |
char c = rotors[0][mod26(L + li(b) - M)]; | |
char ref = reflector[mod26(li(c) - L)]; | |
int d = mod26(index_of(rotors[0], alpha[mod26(li(ref) + L)]) - L); | |
int e = mod26(index_of(rotors[1], alpha[mod26(d + M)]) - M); | |
char f = alpha[mod26(index_of(rotors[2], alpha[mod26(e + R)]) - R)]; | |
// std::cout << f; | |
output[x] = f; | |
} | |
return output; | |
} | |
int main () | |
{ | |
for ( int i = 0; i < 1000000; i++) { | |
crypt(std::array<char, 37>{"PZUFWDSASJGQGNRMAEODZJXQQKHSYGVUSGSU"}); | |
} | |
// crypt(std::array<char, 37>{"PZUFWDSASJGQGNRMAEODZJXQQKHSYGVUSGSU"}); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment