sha256.hh
Go to the documentation of this file.
#pragma once
#include <vector>
#include <openssl/sha.h>
#include <openssl/objects.h>
class sha256 {
SHA256_CTX _ctx;
public:
explicit sha256(): _ctx{} { };
virtual ~sha256() { };
inline int generate(const std::vector<unsigned char> p_buffer, std::vector<unsigned char>& p_hash) {
// Sanity check
if (p_buffer.size() == 0) {
return -1;
}
return generate(p_buffer.data(), p_buffer.size(), p_hash);
};
inline int generate(const unsigned char *p_buffer, const size_t p_length, std::vector<unsigned char>& p_hash) {
// Sanity check
if (p_buffer == nullptr) {
return -1;
}
// Resize data buffer
p_hash.resize(SHA256_DIGEST_LENGTH);
// Compute the hash value
::SHA256_Init(&_ctx);
::SHA256_Update(&_ctx, p_buffer, p_length);
::SHA256_Final(static_cast<unsigned char*>(p_hash.data()), &_ctx);
return 0;
};
}; // End of class sha256
sha256
This class provides description of SHA-256 helper methods.
Definition: sha256.hh:22
sha256::~sha256
virtual ~sha256()
Default destructor.
Definition: sha256.hh:33
sha256::_ctx
SHA256_CTX _ctx
Definition: sha256.hh:23
sha256::generate
int generate(const unsigned char *p_buffer, const size_t p_length, std::vector< unsigned char > &p_hash)
Definition: sha256.hh:61
sha256::generate
int generate(const std::vector< unsigned char > p_buffer, std::vector< unsigned char > &p_hash)
Receive bytes formated data from the lower layers.
Definition: sha256.hh:43
sha256::sha256
sha256()
SHA context.
Definition: sha256.hh:29