libxcks  0.1.0.1
sha224_256.hpp
Go to the documentation of this file.
1 /*
2  * libxcks
3  * Copyright (C) 2022 Julien Couot
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 #ifndef INC_SHA224_256_HPP_178C3693_9F7E_475C_A474_9DBDA16BCC5B
25 #define INC_SHA224_256_HPP_178C3693_9F7E_475C_A474_9DBDA16BCC5B
26 
27 //---------------------------------------------------------------------------
28 #include "libgcrypthash.hpp"
29 //---------------------------------------------------------------------------
30 
31 
32 namespace libxcks
33 {
45 {
46  protected:
47  // State of computation between the single steps.
48  uint32_t h0;
49  uint32_t h1;
50  uint32_t h2;
51  uint32_t h3;
52  uint32_t h4;
53  uint32_t h5;
54  uint32_t h6;
55  uint32_t h7;
56 
57  uint32_t nblocks;
58  int count;
59  uint8_t ibuffer[64];
60 
61  public:
65  void reset() override = 0;
66 
73  void update(const uint8_t* buf, size_t len) override final;
74 
75  protected:
80  void finish();
81 
89  void transform(uint8_t* data);
90 };
91 //---------------------------------------------------------------------------
92 
93 
116 class SHA224 final : public AbstractSHA256Impl
117 {
118  public:
122  SHA224();
123 
127  void reset() override;
128 
137  uint8_t* getValue(uint8_t* buffer) const override;
138 
146  size_t getSize() const override { return 28; }
147 
153  std::string getName() const override
154  {
155  return getHashName();
156  }
157 
163  ArrayString getAltNames() const override
164  {
165  return getAlternativeNames();
166  }
167 
177  ChecksumAlgoId getID() const override
178  {
179  return getIdentifier();
180  }
181 
187  static std::string getHashName()
188  {
189  return "SHA2-224";
190  }
191 
198 
208  static constexpr ChecksumAlgoId getIdentifier()
209  {
210  return ChecksumAlgoId::SHA2_224;
211  }
212 
222  {
223  return new SHA224();
224  }
225 };
226 //---------------------------------------------------------------------------
227 
228 
251 class SHA256 final : public AbstractSHA256Impl
252 {
253  public:
257  SHA256();
258 
262  void reset() override;
263 
272  uint8_t* getValue(uint8_t* buffer) const override;
273 
281  size_t getSize() const override { return 32; }
282 
288  std::string getName() const override
289  {
290  return getHashName();
291  }
292 
298  ArrayString getAltNames() const override
299  {
300  return getAlternativeNames();
301  }
302 
312  ChecksumAlgoId getID() const override
313  {
314  return getIdentifier();
315  }
316 
322  static std::string getHashName()
323  {
324  return "SHA2-256";
325  }
326 
333 
343  static constexpr ChecksumAlgoId getIdentifier()
344  {
345  return ChecksumAlgoId::SHA2_256;
346  }
347 
357  {
358  return new SHA256();
359  }
360 };
361 //---------------------------------------------------------------------------
362 } // namespace libxcks
363 //---------------------------------------------------------------------------
364 
365 #endif // INC_SHA224_256_HPP_178C3693_9F7E_475C_A474_9DBDA16BCC5B
Computes the SHA256 hash from a byte stream.
Definition: sha224_256.hpp:45
uint32_t h3
Fourth part of the state of computation.
Definition: sha224_256.hpp:51
uint32_t h6
Seventh part of the state of computation.
Definition: sha224_256.hpp:54
uint32_t h2
Third part of the state of computation.
Definition: sha224_256.hpp:50
void finish()
Process the remaining bytes in the internal buffer and the usual prolog according to the standard.
Definition: sha224_256.cpp:81
void transform(uint8_t *data)
Transform the message X which consists of 16 32-bit-words.
Definition: sha224_256.cpp:211
uint32_t h4
Fifth part of the state of computation.
Definition: sha224_256.hpp:52
uint8_t ibuffer[64]
Input buffer.
Definition: sha224_256.hpp:59
int count
Current size of the input buffer.
Definition: sha224_256.hpp:58
uint32_t h0
First part of the state of computation.
Definition: sha224_256.hpp:48
uint32_t h7
Eighth part of the state of computation.
Definition: sha224_256.hpp:55
void update(const uint8_t *buf, size_t len) override final
Updates the SHA256 hash with specified array of bytes.
Definition: sha224_256.cpp:151
uint32_t h1
Second part of the state of computation.
Definition: sha224_256.hpp:49
uint32_t nblocks
Number of blocks.
Definition: sha224_256.hpp:57
uint32_t h5
Sixth part of the state of computation.
Definition: sha224_256.hpp:53
void reset() override=0
Resets the checksum to initial value.
Computes a checksum from a byte stream.
Definition: checksum.hpp:54
Computes a hash from a byte stream.
Computes the SHA224 hash from a byte stream.
Definition: sha224_256.hpp:117
uint8_t * getValue(uint8_t *buffer) const override
Returns the SHA224 hash value in the first 28 bytes of the given address.
Definition: sha224_256.cpp:326
ArrayString getAltNames() const override
Returns the alternative names of the SHA224 hash algorithm.
Definition: sha224_256.hpp:163
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: sha224_256.hpp:187
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: sha224_256.hpp:153
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: sha224_256.hpp:177
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: sha224_256.hpp:221
SHA224()
Default constructor.
Definition: sha224_256.cpp:297
void reset() override
Resets the SHA224 hash to initial state of computation.
Definition: sha224_256.cpp:307
size_t getSize() const override
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: sha224_256.hpp:146
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA224 hash algorithm.
Definition: sha224_256.cpp:341
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: sha224_256.hpp:208
Computes the SHA256 hash from a byte stream.
Definition: sha224_256.hpp:252
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: sha224_256.hpp:343
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: sha224_256.hpp:356
ArrayString getAltNames() const override
Returns the alternative names of the SHA256 hash algorithm.
Definition: sha224_256.hpp:298
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: sha224_256.hpp:288
uint8_t * getValue(uint8_t *buffer) const override
Returns the SHA256 hash value in the first 32 bytes of the given address.
Definition: sha224_256.cpp:385
SHA256()
Default constructor.
Definition: sha224_256.cpp:356
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: sha224_256.hpp:322
size_t getSize() const override
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: sha224_256.hpp:281
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: sha224_256.hpp:312
void reset() override
Resets the SHA256 hash to initial state of computation.
Definition: sha224_256.cpp:366
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA256 hash algorithm.
Definition: sha224_256.cpp:400
Interface for classes from the Libgcrypt that compute hashes.
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65
std::vector< std::string > ArrayString
Array of strings.
Definition: types.hpp:44