#include <string>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
static const size_t DIGEST_SIZE = 32;
template< typename T >
std::string int_to_hex( T i )
{
char buffer [3];
buffer[2] = 0;
sprintf(buffer, "%02x", i);
return std::string(buffer);
}
std::string antiDigest(const std::string& a)
{
if(a.empty())
{
throw "cannot digest empty string";
}
char r[DIGEST_SIZE ] = {0};
int block_size = std::min(DIGEST_SIZE, a.length());
int block_count = 1 + DIGEST_SIZE / a.length();
for(int i=0; i<block_size; i++)
{
int hlp = 0;
int bc = 0;
while(bc < block_count)
{
int idx = i + bc * block_size;
if(idx >= a.length()) break;
hlp += a[idx];
bc ++;
}
hlp = (int)(hlp << 3) + hlp;
unsigned int hlp2 = 0;
while(hlp)
{
int t = hlp - ((hlp/10) * 10);
hlp2 += t;
hlp /= 10;
}
bc = 0;
while(bc < block_count)
{
int idx = i + bc * block_size;
if(idx >= DIGEST_SIZE) break;
r[idx] = ( (hlp2 / 10) + (hlp2-(hlp2/10)*10)) ;
bc++;
}
}
std::stringstream result;
for(int i=0; i<DIGEST_SIZE; i++)
{
result << int_to_hex(r[i]) ;
}
return result.str();
}
int main()
{
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";
std::string adigest = antiDigest(a);
std::string bdigest = antiDigest(b);
std::string cdigest = antiDigest(c);
std::cout << "adigest=" << adigest << " bdigest=" << bdigest << " cdigest=" << cdigest << std::endl;
}