Skip to main content

Posts

Showing posts from July, 2017

How to use boost sha1 with python hashlib

I need to have a sha1 digest from both C++ code and python code. Here is a code snip to match both results. This code avoids a potential problem that the digest has some 0s on top of the digest array element. This doesn't matter if you stick to one implementation, but just in case, you need to match two worlds: C++ and python, this code might be useful. /// get sha1 digest as a std::string /// /// \param[in] mes message to be hashed /// \return digest string std::string get_sha1_digest(const std::string& mes) { boost::uuids::detail::sha1 sha1; sha1.process_bytes(mes.c_str(), mes.size()); const int DIGEST_SIZE = 5; unsigned int sha1_hash[DIGEST_SIZE]; sha1.get_digest(sha1_hash); std::stringstream sstr; for (std::size_t i=0; i < DIGEST_SIZE; ++i) { sstr << std::setfill('0') << std::setw(8) << std::hex << sha1_hash[i]; } return sstr.str(); } This function's output matches with the fo

2017-07-04: rights