Quote:
Originally Posted by xAxTer
Yeah it's great and also libtom can be useful too
[Only registered and activated users can see links. Click Here To Register...]
Code:
//Hashes the input string using sha1
unsigned char* hSHA1(const std::string& input) {
hash_state hs;
sha1_init(&hs);
unsigned char* checksum = new unsigned char[sha1_desc.hashsize];
sha1_process(&hs, (const unsigned char*) input.c_str(), input.size());
sha1_done(&hs, checksum);
return checksum;
}
|
Well, I did not test it, but why do you use dynamic raw arrays?
Whoever uses this will probably (and thought futher pretty surely) forget to delete the dynamically crafted array which will cause if the hashing is used more often, which is absolutely possible heavy memory leaks.
It should work perfectly with an usual std::vector, locally created and copied (in this example, moving should work of course, too).
Code:
std::vector hSHA1(const std::string& input) {
hash_state hs;
sha1_init(&hs);
std::vector<unsigned char> checksum;
checksum.resize(sha1_desc.hashsize);
sha1_process(&hs, reinterpret_cast<const unsigned char*>(input.c_str()), input.size());
sha1_done(&hs, &checksum[0]);
return checksum;
}
I did not test this, but it should work. Correct me if I'm wrong.
But yet I think cryptopp is the better way to go. No specific reason then rather personal preference but why would you prefer to use C libraries instead of C++ ones when already programming in C++?