Need CryptoPP Assistance

Hello, I've been trying to use the cryptoPP library for some time now, and I've been coming about some annoying issues with it.

I have this function to use the whirlpool hash function, but however, I appear to be getting an incorrect value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "cryptopp/hex.h"
#include "cryptopp/whrlpool.h"

.
.
.

const char * CryptoppDoHash(const char * inputText) {

    CryptoPP::Whirlpool theHash;

    std::string cipher;
    {		
   	   CryptoPP::StringSink* SSINK = new CryptoPP::StringSink(cipher);
	   CryptoPP::HexEncoder* hex = new CryptoPP::HexEncoder(SSINK, false);
	   CryptoPP::HashFilter* hashA = new CryptoPP::HashFilter(theHash, hex);
	   CryptoPP::StringSource source(inputText, true, hashA);
    }
	return cipher.c_str();
}


any help would be greatly appreciated, thanks.

EDIT:

Value My Program Outputs for "test":
b913d5bbb8e461c2c5961cbe0edcdadfd29f068225ceb37da6defcf89849368f8c6c2eb6a4c4ac75775d032a0ecfdfe8550573062b653fe92fc7b8fb3b7be8d6

Online Calculator Value for "test":
e6b4aa087751b4428171777f1893ba585404c7e0171787720eba0d8bccd710dc2c42f874c572bfae4cedabf50f2c80bf923805d4e31c504b86ca3bc59265e7dd

It also appears the first two bytes of the output are missing.
Last edited on
Once the function returns, cipher goes out of scope and the pointer you returned becomes invalid.
how should I go about fixing it then?
Return the std::string itself instead of a C string.
Using that method, the output now only displays the single character: "D".

1
2
3
4
5
6
7
8
9
10
11
12
std::string CryptoppDoHash(const char * inputText) {

	string cipher;
    CryptoPP::Whirlpool theHash;
    		
    CryptoPP::StringSink* SSINK = new CryptoPP::StringSink(cipher);
	CryptoPP::HexEncoder* hex = new CryptoPP::HexEncoder(SSINK, false);
	CryptoPP::HashFilter* hashA = new CryptoPP::HashFilter(theHash, hex);
	CryptoPP::StringSource source(inputText, true, hashA);
    
	return cipher;
}
I'm really not understanding this library that well.

Even when directly calling the functions, it still comes out wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::string CryptoppDoHash(const char * inputText) {
	using namespace CryptoPP;
	
	byte cipher[CryptoPP::Whirlpool::BLOCKSIZE];
	//
	char * input1;
	unsigned char * input2;
	const byte * input3;
	input1 = (char *)inputText;
	input2 = (unsigned char *)input1;
	input3 = (byte *)input2;

	//CryptoPP::StringSink* SSINK = new CryptoPP::StringSink(cipher);
	//CryptoPP::HexEncoder* hex = new CryptoPP::HexEncoder(SSINK, false);
	//CryptoPP::HashFilter* hashA = new CryptoPP::HashFilter(theHash, hex);
	//CryptoPP::StringSource source(inputText, true, hashA);

	CryptoPP::Whirlpool hash;
	hash.Update(input3, CryptoPP::Whirlpool::BLOCKSIZE);
	hash.Final(cipher);

    std::string returnString = ((char *)cipher);
	return returnString;
}


this only outputted the single character "#". Any suggestions for fixes with either method? I really need to get these hashing functions to work for my program.
Keep in mind that Final() will write a binary array, not a string array. It's highly unlikely line 22 will work, and even if it does, it's by pure chance.
well then how do I go about converting a byte * (byte []) type to a readable std::string?
oh, this is totally my fault here.

On further inspection of the string, I compiled a .php script on my site to compare the outputs, and apparently the online calculator is incorrect in it's value.

Sorry for wasting your time, my first function works fine.

I'll be a little more clear about what I'm trying to accomplish though.

I needed the hashing functions to work with the library in an attempt to implement a x509 certificate system for my game designs.

Thank you for your help though, this is solved.
Your first function still has that bug I mentioned on my first post, though.
Topic archived. No new replies allowed.