Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <inttypes.h>
- #include <assert.h>
- #include <vector>
- // brute-force python2's string hash randomization (-R option) given a list
- // of pairs (s,hash(s))
- // caveat: this doesnt incorporate a xor with the string length (trivial to
- // add), thus all given strings must have the same length!
- // written for plaidCTF 2015's 'radhos' challenge in a hurry, don't expect
- // great code quality.
- // g++ -fopenmp -O3 -std=c++14 -Wall -Wextra -o brute brute.cpp
- using namespace std;
- int main(int argc, char *argv[])
- {
- typedef pair<string, uint32_t> strhash;
- vector<strhash> input_hashes;
- while (!cin.eof()) {
- string str;
- uint32_t hash;
- cin >> str;
- cin >> hash;
- if (cin.good()) {
- input_hashes.push_back(make_pair(str,hash));
- } else {
- break;
- }
- }
- size_t len = input_hashes.size();
- //cout << "loaded " << len << " str/hash combinations." << endl;
- static const uint32_t M = 2021759595;
- #pragma omp parallel for
- for (uint64_t suffix=0; suffix <= 0xffffffff; ++suffix) {
- uint32_t prefix=0;
- size_t prefix_count=0;
- for (strhash &s: input_hashes) {
- uint32_t h=s.second;
- const string &str = s.first;
- h^=suffix;
- unsigned char last=0;
- for (const unsigned char &c: str) {
- h^=c;
- h*=M;
- last = c;
- }
- h ^= last << 7;
- if (prefix == h) {
- prefix_count++;
- if (prefix_count == len)
- #pragma omp critical
- {
- cout << prefix << "," << suffix << endl;
- exit(0);
- }
- } else if (prefix_count == 0) {
- prefix = h;
- prefix_count++;
- } else{
- break;
- }
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement