Skip to content

Instantly share code, notes, and snippets.

@einaros
Created December 30, 2011 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einaros/4a8d873c040b5659edbc to your computer and use it in GitHub Desktop.
Save einaros/4a8d873c040b5659edbc to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <bitset>
#include <iostream>
#include <map>
#include <cstring>
#include <string>
using namespace std;
int bitCount(unsigned int num) {
bitset<32> set(num);
int count = 32;
for (int i = 1; i <= 32; ++i) {
if (set[32 - i]) break;
--count;
}
return count;
}
unsigned int reverseXor(unsigned int num) {
bitset<32> orig(num), mod(num>>6), res(num);
int blank = 32 - bitCount(num>>6);
int i;
for (i = 31; i > 32 - (blank + 6) && i > 0; --i)
orig.set(i, res[i] ^ mod[i]);
for (; i >= 0; --i) {
mod.set(i, orig[i + 6]);
orig.set(i, res[i] ^ mod[i]);
}
return (unsigned int)orig.to_ulong();
}
int hash(const char* str, int l, int h = 0) {
for (int i = 0; i < l; ++i) {
h += (int)*(str + i);
h += (h << 10);
h ^= (h >> 6);
}
return h;
}
int hashback(const char* str, int l, int target) {
unsigned int h = target;
for (; l > 0; --l) {
h = reverseXor(h);
h = h * [REDACTED];
h -= (int)*(str + l - 1);
}
return h;
}
int permuteChar(char* c) {
*c += 1;
if (*c == 91) *c = 97;
else if (*c == 123) {
*c = 65;
return 1;
}
return 0;
}
bool permute(char* str, int len) {
for (int i = len - 1; i >= 0; --i) {
if (!permuteChar(str + i)) return true;
}
return false;
}
int main(int argc, char** argv) {
map<int, string> precomp;
int target = 55555;
char* prefix = new char[4];
strcpy(prefix, "AAA");
do {
precomp[hashback(prefix, 3, target)] = string(prefix);
} while (permute(prefix, 3));
char* str = new char[8];
str[7] = 0;
if (argc > 1) strncpy(str, argv[1], 6);
else strcpy(str, "AAAAAAA");
do {
int h = hash(str, 7);
map<int, string>::iterator hit = precomp.find(h);
if (hit != precomp.end() && hash((string(str) + hit->second).c_str(), 10) == target) {
cout << str << hit->second << endl;
}
} while (permute(str, 7));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment