Implementing Hash Chains with Crypto++

16 views
Skip to first unread message

Benjamin Oribe Mendieta

unread,
Oct 17, 2019, 1:54:22 PM10/17/19
to Crypto++ Users
I'm new to cryptography and to crypto++ in general, but I need to try a hash chain with the library, I got this code but I have an issue

    CryptoPP::Integer iRandomValue;


    CryptoPP::AutoSeededRandomPool prng;


    CryptoPP::SHA1 hash;


    std::string digest,tmp;


    std::ostringstream ossRandomValue;


    //RANDOM VALUE


    iRandomValue.Randomize(prng, bitsize);


    ossRandomValue << iRandomValue;


    tmp=ossRandomValue.str();


     std::cout<<"DEC VALUE :"<<std::dec<<iRandomValue<<std::endl;


    int i=0;

   while(i<2)

   {


        std::string sHash1;


        CryptoPP::HexEncoder hEncoder1(new CryptoPP::StringSink(sHash1));


       


        CryptoPP::StringSource(tmp, true, /*PumpAll*/


                                  new CryptoPP::Redirector(hEncoder1)


                                  );


        std::cout<<"ORIGINAL VALUE:"<<tmp<<":"<<std::endl;


        std::cout<<"HEX ORIGINAL VALUE:"<<sHash1<<std::endl;


        int x=0;


       


        if(i<=0)


            x=1;


        else


            x=0;


        hash.Update((const CryptoPP::byte*)sHash1.data(), sHash1.size());


        digest.resize(hash.DigestSize());


        hash.Final((CryptoPP::byte*)&digest[0]);


        CryptoPP::Integer result;


        result.Decode((CryptoPP::byte*)&digest[0],hash.DigestSize());


        std::ostringstream ossHashDecimal;


        ossHashDecimal << result;


        tmp=digest;


       


        std::cout<<"HASH VALUE DEC:"<<tmp<<std::endl;


       


        std::string sHash;


        CryptoPP::HexEncoder hEncoder(new CryptoPP::StringSink(sHash));


       


       


        CryptoPP::StringSource(digest, true, /*PumpAll*/


                                  new CryptoPP::Redirector(hEncoder)


                                  );


       


        std::cout<<"HASH VALUE:"<<sHash<<std::endl;


        i++;




    }



The first hash work pretty well, but when the result of the first hash is sent to create a new diggest the printed result doesn't match with another calculated hash, I tried online apliying two hashes to the same values, but don't get the same value as with these code.

Also, if anyone know if I need something to make faster the continuous hashing I'll be most thankful

I hope someone an solve my issue.



Reply all
Reply to author
Forward
0 new messages