Created
February 13, 2012 19:17
-
-
Save sambatyon/1819240 to your computer and use it in GitHub Desktop.
Small function to compute the sha1 sum of data in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/uuid/sha1.hpp> | |
#include <sstream> | |
#include <cstddef> | |
#include <string> | |
std::string Sha1sum(void *data, std::size_t count) { | |
boost::uuids::detail::sha1 hasher; | |
char hash[20]; | |
hasher.process_bytes(data, count); | |
unsigned int digest[5]; | |
hasher.get_digest(digest); | |
for(int i = 0; i < 5; ++i) { | |
const char *tmp = reinterpret_cast<char *>(digest); | |
hash[i * 4] = tmp[i * 4 + 3]; | |
hash[i * 4 + 1] = tmp[i * 4 + 2]; | |
hash[i * 4 + 2] = tmp[i * 4 + 1]; | |
hash[i * 4 + 3] = tmp[i * 4]; | |
} | |
std::stringstream res; | |
res << std::hex; | |
for(int i = 0; i < 20; ++i) { | |
res << ((hash[i] & 0x000000F0) >> 4) | |
<< (hash[i] & 0x0000000F); | |
} | |
return res.str(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment