Question

I'm using Crypto++ to generate a MD4-Hash from a given password. But the generated hash doesn't seem to be correct. I think I'm misusing the CryptoPP functions somewhere.

CryptoPP::Weak1::MD4 hash2;
byte digest2[CryptoPP::Weak1::MD4::DIGESTSIZE];
hash.CalculateDigest(digest2, (byte*)password, strlen(password));
CryptoPP::HexEncoder encoder2;
std::string output2;
encoder2.Attach(new CryptoPP::StringSink(output2));
encoder2.Put(digest,sizeof(digest2));
encoder2.MessageEnd();
printf("END %s \n", output2.c_str());

My variable password contains the value "test". The printed output is:

END 3CC942AE509EC070B2548515E00F8CE8

The expected value, tested by some MD4 Hash Generators, is:

db346d691d7acc4dc2625db19f9e3f52

Any ideas?

Was it helpful?

Solution

Okay, I found the solution by myself. It doesn't work the way I posted above.

Here the correct code, it may be useful for someone else:

std::string value;
CryptoPP::Weak1::MD4 hashmd4;
CryptoPP::StringSource (password, true,
  new CryptoPP::HashFilter( hashmd4,
    new CryptoPP::HexEncoder(
      new CryptoPP::StringSink(value)
    )
  )
);

OTHER TIPS

hash.CalculateDigest(digest2, (byte*)password, strlen(password));

This should be:

 hash2.CalculateDigest(digest2, (byte*)password, strlen(password));

That is, hash2, not hash.


encoder2.Put(digest,sizeof(digest2));

This should be:

encoder2.Put(digest2,sizeof(digest2));

That is, digest2, not digest.


The expected value, tested by some MD4 Hash Generators, is:

db346d691d7acc4dc2625db19f9e3f52

Yep, that's what I get using the code you posted after the typos were fixed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top