Advertisement
Guest User

Untitled

a guest
May 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <inttypes.h>
  3. #include <assert.h>
  4. #include <vector>
  5.  
  6.  
  7. // brute-force python2's string hash randomization (-R option) given a list
  8. // of pairs (s,hash(s))
  9. // caveat: this doesnt incorporate a xor with the string length (trivial to
  10. // add), thus all given strings must have the same length!
  11.  
  12. // written for plaidCTF 2015's 'radhos' challenge in a hurry, don't expect
  13. // great code quality.
  14.  
  15. // g++ -fopenmp -O3 -std=c++14 -Wall -Wextra -o brute brute.cpp
  16.  
  17.  
  18.  
  19. using namespace std;
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23. typedef pair<string, uint32_t> strhash;
  24. vector<strhash> input_hashes;
  25. while (!cin.eof()) {
  26. string str;
  27. uint32_t hash;
  28. cin >> str;
  29. cin >> hash;
  30. if (cin.good()) {
  31. input_hashes.push_back(make_pair(str,hash));
  32. } else {
  33. break;
  34. }
  35. }
  36. size_t len = input_hashes.size();
  37. //cout << "loaded " << len << " str/hash combinations." << endl;
  38. static const uint32_t M = 2021759595;
  39. #pragma omp parallel for
  40. for (uint64_t suffix=0; suffix <= 0xffffffff; ++suffix) {
  41. uint32_t prefix=0;
  42. size_t prefix_count=0;
  43. for (strhash &s: input_hashes) {
  44. uint32_t h=s.second;
  45. const string &str = s.first;
  46. h^=suffix;
  47. unsigned char last=0;
  48. for (const unsigned char &c: str) {
  49. h^=c;
  50. h*=M;
  51. last = c;
  52. }
  53. h ^= last << 7;
  54. if (prefix == h) {
  55. prefix_count++;
  56. if (prefix_count == len)
  57. #pragma omp critical
  58. {
  59. cout << prefix << "," << suffix << endl;
  60. exit(0);
  61. }
  62. } else if (prefix_count == 0) {
  63. prefix = h;
  64. prefix_count++;
  65. } else{
  66. break;
  67. }
  68. }
  69. }
  70. return 1;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement