|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <string> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +//49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d |
| 8 | + |
| 9 | +//SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t |
| 10 | + |
| 11 | + |
| 12 | +typedef unsigned char byte; |
| 13 | +typedef char character; |
| 14 | + |
| 15 | +template<typename T> |
| 16 | +ostream& operator<< (ostream& out, const vector<T>& v) { |
| 17 | + size_t last = v.size() - 1; |
| 18 | + for (size_t i = 0; i < v.size(); ++i) { |
| 19 | + out << v[i]; |
| 20 | + } |
| 21 | + return out; |
| 22 | +} |
| 23 | + |
| 24 | +byte toNumber(character a) { |
| 25 | + string hexChars("0123456789abcdef"); |
| 26 | + |
| 27 | + byte ret = (byte)hexChars.find(a); |
| 28 | + |
| 29 | + return ret; |
| 30 | +} |
| 31 | + |
| 32 | +vector<byte> hexTobin(vector<character> hexString) { |
| 33 | + vector<byte> binString; |
| 34 | + |
| 35 | + if (hexString.size() % 2 != 0) { |
| 36 | + throw invalid_argument("Ivalid hexadecimal string."); |
| 37 | + } |
| 38 | + |
| 39 | + // Ask what an iterator is and how it works |
| 40 | + for (vector<character>::iterator it = hexString.begin(); it != hexString.end(); it++) { |
| 41 | + character first = toNumber(*it); |
| 42 | + |
| 43 | + it++; |
| 44 | + character second = toNumber(*it); |
| 45 | + |
| 46 | + binString.push_back(second ^ (first << 4)); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + return binString; |
| 51 | +} |
| 52 | + |
| 53 | +character base64Value(int a) { |
| 54 | + string base64Chars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); |
| 55 | + |
| 56 | + return base64Chars[a]; |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +vector<character> toBase64(vector<byte> raw) { |
| 61 | + vector<character> base64string; |
| 62 | + |
| 63 | + for (int i = 0; i < raw.size(); ++i) { |
| 64 | + character B1, B2, B3, B4; |
| 65 | + const char bitmask = 0b00111111; |
| 66 | + byte first, second, third; |
| 67 | + |
| 68 | + first = raw[i]; |
| 69 | + B1 = base64Value(bitmask & (first >> 2)); |
| 70 | + base64string.push_back(B1); |
| 71 | + |
| 72 | + ++i; |
| 73 | + if (i < raw.size()) { |
| 74 | + |
| 75 | + second = raw[i]; |
| 76 | + B2 = base64Value(bitmask & ((first << 4) ^ (second >> 4))); |
| 77 | + base64string.push_back(B2); |
| 78 | + |
| 79 | + ++i; |
| 80 | + if (i < raw.size()) { |
| 81 | + |
| 82 | + third = raw[i]; |
| 83 | + |
| 84 | + B3 = base64Value(bitmask & ((second << 2) ^ (third >> 6))); |
| 85 | + B4 = base64Value(bitmask & third); |
| 86 | + base64string.push_back(B3); |
| 87 | + base64string.push_back(B4); |
| 88 | + } |
| 89 | + else { |
| 90 | + third = 0; |
| 91 | + B3 = base64Value(bitmask & ((second << 2) ^ (third >> 6))); |
| 92 | + base64string.push_back(B3); |
| 93 | + base64string.push_back('='); |
| 94 | + } |
| 95 | + } |
| 96 | + else { |
| 97 | + second = 0; |
| 98 | + B2 = base64Value(bitmask & ((first << 4) ^ (second >> 4))); |
| 99 | + base64string.push_back(B2); |
| 100 | + base64string.push_back('='); |
| 101 | + base64string.push_back('='); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + return base64string; |
| 107 | +} |
| 108 | + |
| 109 | +string characterVectorToString(vector<character> v) { |
| 110 | + string s(v.begin(), v.end()); |
| 111 | + return s; |
| 112 | +} |
| 113 | + |
| 114 | +string byteVectorToString(vector<byte> v) { |
| 115 | + string s(v.begin(), v.end()); |
| 116 | + return s; |
| 117 | +} |
| 118 | + |
| 119 | +vector<byte> stringToByteVector(string s) { |
| 120 | + vector<byte> v(s.begin(), s.end()); |
| 121 | + return v; |
| 122 | +} |
| 123 | + |
| 124 | +vector<character> stringToCharacterVector(string s) { |
| 125 | + vector<character> v(s.begin(), s.end()); |
| 126 | + return v; |
| 127 | +} |
| 128 | + |
| 129 | +bool testBase64(string input, string expected) { |
| 130 | + vector<byte> in = stringToByteVector(input); |
| 131 | + vector<character> cs = toBase64(in); |
| 132 | + string actual = characterVectorToString(cs); |
| 133 | + |
| 134 | + bool isCorrect = expected.compare(actual) == 0; |
| 135 | + |
| 136 | + cout << ((isCorrect) ? "OK:" : "FAIL") << endl; |
| 137 | + cout << " input: " << input << endl; |
| 138 | + cout << " expected: " << expected << endl; |
| 139 | + cout << " actual: " << actual << endl << endl; |
| 140 | + return isCorrect; |
| 141 | +} |
| 142 | + |
| 143 | +int main() { |
| 144 | + // testing base64 |
| 145 | + testBase64("any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZS4="); |
| 146 | + testBase64("any carnal pleasure", "YW55IGNhcm5hbCBwbGVhc3VyZQ=="); |
| 147 | + testBase64("any carnal pleasur", "YW55IGNhcm5hbCBwbGVhc3Vy"); |
| 148 | + testBase64("any carnal pleasu", "YW55IGNhcm5hbCBwbGVhc3U="); |
| 149 | + |
| 150 | + // Challenge 1 |
| 151 | + string hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; |
| 152 | + vector<character> hexInput(hex.begin(), hex.end()); |
| 153 | + vector<byte> raw = hexTobin(hexInput); |
| 154 | + vector<character> base64 = toBase64(raw); |
| 155 | + |
| 156 | + testBase64(byteVectorToString(raw), "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"); |
| 157 | + |
| 158 | + system("Pause"); |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
0 commit comments