Skip to content

Instantly share code, notes, and snippets.

@zhuangh
Forked from sambatyon/Sha1Sum.cc
Created April 22, 2013 17:06
Show Gist options
  • Save zhuangh/5436766 to your computer and use it in GitHub Desktop.
Save zhuangh/5436766 to your computer and use it in GitHub Desktop.
#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