Version: SMASH-1.8
sha256.h
Go to the documentation of this file.
1 #ifndef SRC_INCLUDE_SHA256_H_
2 #define SRC_INCLUDE_SHA256_H_
3 
4 #include <array>
5 #include <cstdint>
6 #include <cstdio>
7 #include <string>
8 
9 namespace smash {
10 namespace sha256 {
11 
13 constexpr size_t HASH_SIZE = 256 / 8;
14 
16 typedef std::array<uint8_t, HASH_SIZE> Hash;
17 
19 class Context {
20  private:
22  uint64_t length_;
24  uint32_t state_[8];
26  size_t curlen_;
28  uint8_t buf_[64];
29 
33  void transform_function(uint8_t const* buffer);
34 
35  public:
37  Context() { reset(); }
38 
40  void reset();
41 
47  void update(uint8_t const* buffer, size_t buffer_size);
48 
54  void update(const std::string& buffer);
55 
61  Hash finalize();
62 };
63 
67 Hash calculate(uint8_t const* buffer, size_t buffer_size);
68 
72 std::string hash_to_string(Hash hash);
73 
74 } // namespace sha256
75 } // namespace smash
76 
77 #endif // SRC_INCLUDE_SHA256_H_
smash
Definition: action.h:24
smash::sha256::HASH_SIZE
constexpr size_t HASH_SIZE
Size of a SHA256 hash.
Definition: sha256.h:13
smash::sha256::Context::state_
uint32_t state_[8]
State of the SHA256 hash.
Definition: sha256.h:24
smash::sha256::Context::update
void update(uint8_t const *buffer, size_t buffer_size)
Add data to the SHA256 context.
Definition: sha256.cc:153
smash::sha256::Context::transform_function
void transform_function(uint8_t const *buffer)
Compress 512-bits.
Definition: sha256.cc:95
smash::sha256::Context
A SHA256 context.
Definition: sha256.h:19
smash::sha256::Context::length_
uint64_t length_
Length of the SHA256 hash.
Definition: sha256.h:22
smash::sha256::Context::Context
Context()
Construct a SHA256 context.
Definition: sha256.h:37
smash::sha256::Context::buf_
uint8_t buf_[64]
Buffer of the SHA256 hash.
Definition: sha256.h:28
smash::sha256::Context::reset
void reset()
Reset the SHA256 context.
Definition: sha256.cc:140
smash::sha256::Hash
std::array< uint8_t, HASH_SIZE > Hash
A SHA256 hash.
Definition: sha256.h:16
smash::sha256::hash_to_string
std::string hash_to_string(Hash hash)
Convert a SHA256 hash to a hexadecimal string.
Definition: sha256.cc:230
smash::sha256::calculate
Hash calculate(uint8_t const *buffer, size_t buffer_size)
Calculate the SHA256 hash of the buffer.
Definition: sha256.cc:224
smash::sha256::Context::finalize
Hash finalize()
Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit h...
Definition: sha256.cc:185
smash::sha256::Context::curlen_
size_t curlen_
Current length of the SHA256 hash.
Definition: sha256.h:26