Search code examples
c++hashcrypto++

MD4 hashing with Crypto++ results in wrong hash?


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?


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)
        )
      )
    );