Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CryptoPP::HexDecoder( )

I am playing with Cryptopp for ther first time and I found an example to encode to Hex...All is well. Now I want to decode std::string produced into original string but all I get is empty string.

#include "stdafx.h"

#include "../cryptopp562/sha.h"
#include "../cryptopp562/filters.h"
#include "../cryptopp562/hex.h"
#include <iostream>
#include <string>

int main() {
    CryptoPP::SHA1 sha1;
    std::string source = "Panawara";  
    std::string hash = "";
    std::string original= "" ;

    CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));
    std::cout << hash;
    std::cout << "\n";

    CryptoPP::StringSource (hash, new CryptoPP::HexDecoder(new CryptoPP::StringSink(original)));  // the result is always empty String
    std::cout << original;
    std::cout << "\n";

    system("pause");
}
like image 953
xtensa1408 Avatar asked Nov 01 '22 17:11

xtensa1408


1 Answers

CryptoPP::StringSource(source, true,
    new CryptoPP::HashFilter(sha1,
        new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));

Don't use anonymous declarations. Name your variables. Use this instead:

CryptoPP::StringSource ss(source, true,
    new CryptoPP::HashFilter(sha1,
        new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash)
    )));

Do it for all of them.

Also see CryptoPP HexDecoder output empty.

like image 59
jww Avatar answered Nov 15 '22 04:11

jww