Select Git revision
Hasher.cpp 1.03 KiB
#include "Hasher.h"
#include <openssl/sha.h>
#include <sstream>
#include <iomanip>
std::vector<unsigned char> SIGN::Hasher::sha256(const unsigned char* data, size_t length)
{
SHA256_CTX context;
SHA256_Init(&context);
SHA256_Update(&context, data, length);
std::vector<unsigned char> buffer(SHA256_DIGEST_LENGTH);
SHA256_Final(buffer.data(), &context);
return buffer;
}
SIGN::Hasher::Hasher()
{
}
SIGN::Hasher::~Hasher()
{
}
std::vector<unsigned char> SIGN::Hasher::hash()
{
return sha256(data.data(), data.size());
}
void SIGN::Hasher::reset()
{
data.clear();
}
size_t SIGN::Hasher::size(void)
{
return SHA256_DIGEST_LENGTH; ;
}
std::string SIGN::Hasher::print(std::vector<unsigned char> bytes, bool uppercase)
{
std::ostringstream result;
for (std::string::size_type i = 0; i < bytes.size();i++)
{
result << std::hex << std::setfill('0') << std::setw(2) << (uppercase ? std::uppercase : std::nouppercase) << (int)bytes[i];
if (i != bytes.size() - 1)
{
result << std::setw(1) << " ";
}
}
return result.str();
}