|
| 1 | +#include <stdlib> |
| 2 | +#include <bitset> |
| 3 | +#include <to_ulong> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +string compute_altsum(string salt, string passwd); // needed to compute intermediate_0 sum |
| 8 | +string compute_intermsum(string psswd, string magic, string salt, string altsum); // intermediate_0 sum |
| 9 | +string interm_1000(string psswd, string salt, string intermsum); // extends to intermsum to intermediate_1000 sum |
| 10 | +bitset<128> str_to_bin(string tmp); // for the final printing |
| 11 | +string md5_crypthash(string passwd, string salt, string magic, string finalsum); |
| 12 | + |
| 13 | +int main() { |
| 14 | + // compute alternate sum |
| 15 | + // compute intermediate sum |
| 16 | + // remaining calculations to extend intermsum to interm_1000 |
| 17 | + return 0; |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +// compute alternate sum |
| 22 | +string compute_altsum(string salt, string passwd) { |
| 23 | + return altsum; // replace altsum with md5(psswd + salt + psswd); TODO: look into the library Alex sent and get rid of the overhead of this function |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +// compute intermediate sum (Intermediate_0) |
| 28 | +string compute_intermsum(string psswd, string magic, string salt, string altsum) { |
| 29 | + //concatenate the inputs; altsum is repeated as necessaryi |
| 30 | + // For each bit in length(password), from low to high and stopping after the most significant set bit such that |
| 31 | + // if (!bit.set()) { |
| 32 | + // intermsum.append(a NUL byte) |
| 33 | + |
| 34 | + //} |
| 35 | + // else { |
| 36 | + // intermsum.append(first byte of the password) |
| 37 | + |
| 38 | + // } |
| 39 | + |
| 40 | + return intermsum; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +// remaining calculations (Intermediate_0 extended to Intermediate_1000) |
| 45 | +string interm_1000(string psswd, string salt, string intermsum) { |
| 46 | + string working_final; |
| 47 | + string tmp_intermsum = intermsum; |
| 48 | + for (unsigned i = 0; i < 1000; ++i) { |
| 49 | + working_final = ""; |
| 50 | + if (i % 2 == 0) { // if i is even intermsum_i |
| 51 | + working_final.append(tmp_intermsum); |
| 52 | + } |
| 53 | + else { // else, concatenate password |
| 54 | + working_final.append(psswd); |
| 55 | + } |
| 56 | + if (i % 3 != 0) { // if not divisible by 3, salt |
| 57 | + working_final.append(salt); |
| 58 | + } |
| 59 | + if (i % 7 != 0) { // if not divisible by 7, password |
| 60 | + working_final.append(psswd); |
| 61 | + } |
| 62 | + if (i % 2 == 0) { // if i is even, psswd |
| 63 | + working_final.append(psswd); |
| 64 | + } |
| 65 | + else { // if i is odd, intermsum_i |
| 66 | + working_final.append(tmp_intermsum); |
| 67 | + } |
| 68 | + tmp_intermsum = md5(working_final); |
| 69 | + } |
| 70 | + return tmp_intermsum; // actually the final thing |
| 71 | +} |
| 72 | + |
| 73 | +// HEREEEEEEEEEE |
| 74 | +string md5_crypthash(string psswd, string salt, string magic, string finalsum) { |
| 75 | + // output magic, then salt, then '$' to separate salt from encrypted section |
| 76 | + string tmp = magic.append(salt).append('$'); |
| 77 | + // then convert string to binary |
| 78 | + bitset<128> much_binary (finalsum.c_str()); |
| 79 | + // reordering shenanigans (11 4 10 5 3 9 15 2 8 14 1 7 13 0 6 12) |
| 80 | + int byte_seq[16] = {11, 4, 10, 5, 3, 9, 15, 2, 8, 14, 1, 7, 13, 0, 6, 12}; // totally not arbitrary |
| 81 | + bitset<128> new_WORLD_order; |
| 82 | + for (unsigned i = 0; i < 16; ++i) { // find the bytes of totally not arbitrary order in the not arbitrary order |
| 83 | + // reorder stoof |
| 84 | + ith_start = byte_seq[i] * 8; |
| 85 | + ith_end = ith_start + 7; |
| 86 | + jth_start = i * 8; |
| 87 | + jth_end = jth_start + 7; // index of bits |
| 88 | + // identifying the bits in the byte in question |
| 89 | + for (unsigned j = jth_start; j <= jth_end; ++j) { |
| 90 | + new_WORLD_order[j] = much_binary[j]; |
| 91 | + } |
| 92 | + } |
| 93 | + // partition into groups of 6 bits (22 groups total) |
| 94 | + //beginning with the least significant |
| 95 | + //NOTE: the link says no additional padding, but we're electing to ignore that |
| 96 | + string partitioned_stuff; |
| 97 | + for (unsigned i = 127; i >= 0; i = i - 6) { |
| 98 | + if (i > 128) { // to catch the BIG number :^) |
| 99 | + break; |
| 100 | + } |
| 101 | + bitset<6> tmp_bits; |
| 102 | + for (unsigned j = 6; j >= 6; ++j) { |
| 103 | + if (j > 7) { // another BIG one :^') |
| 104 | + break; |
| 105 | + } |
| 106 | + tmp[6 - j] = new_WORLD_order[i - j]; |
| 107 | + } |
| 108 | + // convert to char base64 |
| 109 | + partitioned_stuff = partitioned_stuff.append(gimme_char(tmp_bits)); |
| 110 | + } |
| 111 | + // output corresponding base64 character with said grop of 6 bits |
| 112 | + |
| 113 | + return tmp.append(partitioned_stuff); |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +string gimme_char(bitset<6> bit_grp) { // 22 groups total |
| 118 | + int to_int = static_cast<int>(bit_grp.to_ulong()); |
| 119 | + string crypt_str = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 120 | + char crypt_arr[crypt_str.size()+1]; |
| 121 | + strcpy(crypt_arr, crypt_str.c_str()); |
| 122 | + return crypt_arr[to_int]; // NOTE / TODO: check type later; currently a str of length 1 |
| 123 | +} |
| 124 | + |
0 commit comments