fork download
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <sstream>
  6. #include <stdio.h>
  7.  
  8. static const size_t DIGEST_SIZE = 32;
  9.  
  10. template< typename T >
  11. std::string int_to_hex( T i )
  12. {
  13. char buffer [3];
  14. buffer[2] = 0;
  15. sprintf(buffer, "%02x", i);
  16. return std::string(buffer);
  17. }
  18.  
  19. std::string antiDigest(const std::string& a)
  20. {
  21. if(a.empty())
  22. {
  23. throw "cannot digest empty string";
  24. }
  25. char r[DIGEST_SIZE ] = {0};
  26. int block_size = std::min(DIGEST_SIZE, a.length());
  27. int block_count = 1 + DIGEST_SIZE / a.length();
  28. for(int i=0; i<block_size; i++)
  29. {
  30. int hlp = 0;
  31. int bc = 0;
  32.  
  33. while(bc < block_count)
  34. {
  35. int idx = i + bc * block_size;
  36. if(idx >= a.length()) break;
  37. hlp += a[idx];
  38. bc ++;
  39. }
  40. hlp = (int)(hlp << 3) + hlp;
  41. unsigned int hlp2 = 0;
  42. while(hlp)
  43. {
  44. int t = hlp - ((hlp/10) * 10);
  45. hlp2 += t;
  46. hlp /= 10;
  47. }
  48. bc = 0;
  49. while(bc < block_count)
  50. {
  51. int idx = i + bc * block_size;
  52. if(idx >= DIGEST_SIZE) break;
  53. r[idx] = ( (hlp2 / 10) + (hlp2-(hlp2/10)*10)) ;
  54. bc++;
  55. }
  56.  
  57. }
  58.  
  59. std::stringstream result;
  60. for(int i=0; i<DIGEST_SIZE; i++)
  61. {
  62. result << int_to_hex(r[i]) ;
  63. }
  64. return result.str();
  65. }
  66.  
  67. int main()
  68. {
  69. std::string a = "the little brown fox jump over the lazy gardener", b = "the whale swims in the shallow waters of prince caspian sea", c = "blue";
  70. std::string adigest = antiDigest(a);
  71. std::string bdigest = antiDigest(b);
  72. std::string cdigest = antiDigest(c);
  73. std::cout << "adigest=" << adigest << " bdigest=" << bdigest << " cdigest=" << cdigest << std::endl;
  74. }
  75.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
adigest=0909090909090909090909090909090909090909090909090909090909090909 bdigest=0909090909090909090909090909090909090909090909090909090909090909 cdigest=0909090909090909090909090909090909090909090909090909090909090909