Version: SMASH-3.1
sha256.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2019-2020
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_SHA256_H_
11 #define SRC_INCLUDE_SMASH_SHA256_H_
12 
13 #include <array>
14 #include <cstdint>
15 #include <cstdio>
16 #include <string>
17 
18 namespace smash {
19 namespace sha256 {
20 
22 constexpr size_t HASH_SIZE = 256 / 8;
23 
25 typedef std::array<uint8_t, HASH_SIZE> Hash;
26 
28 class Context {
29  private:
31  uint64_t length_;
33  uint32_t state_[8];
35  size_t curlen_;
37  uint8_t buf_[64];
38 
42  void transform_function(uint8_t const* buffer);
43 
44  public:
46  Context() { reset(); }
47 
49  void reset();
50 
56  void update(uint8_t const* buffer, size_t buffer_size);
57 
63  void update(const std::string& buffer);
64 
70  Hash finalize();
71 };
72 
76 Hash calculate(uint8_t const* buffer, size_t buffer_size);
77 
81 std::string hash_to_string(Hash hash);
82 
83 } // namespace sha256
84 } // namespace smash
85 
86 #endif // SRC_INCLUDE_SMASH_SHA256_H_
A SHA256 context.
Definition: sha256.h:28
void update(uint8_t const *buffer, size_t buffer_size)
Add data to the SHA256 context.
Definition: sha256.cc:153
uint32_t state_[8]
State of the SHA256 hash.
Definition: sha256.h:33
size_t curlen_
Current length of the SHA256 hash.
Definition: sha256.h:35
void transform_function(uint8_t const *buffer)
Compress 512-bits.
Definition: sha256.cc:95
void reset()
Reset the SHA256 context.
Definition: sha256.cc:140
Context()
Construct a SHA256 context.
Definition: sha256.h:46
uint64_t length_
Length of the SHA256 hash.
Definition: sha256.h:31
uint8_t buf_[64]
Buffer of the SHA256 hash.
Definition: sha256.h:37
Hash finalize()
Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit h...
Definition: sha256.cc:185
Hash calculate(uint8_t const *buffer, size_t buffer_size)
Calculate the SHA256 hash of the buffer.
Definition: sha256.cc:224
std::array< uint8_t, HASH_SIZE > Hash
A SHA256 hash.
Definition: sha256.h:25
constexpr size_t HASH_SIZE
Size of a SHA256 hash.
Definition: sha256.h:22
std::string hash_to_string(Hash hash)
Convert a SHA256 hash to a hexadecimal string.
Definition: sha256.cc:230
Definition: action.h:24