libxcks  0.1.0.1
sha384_512.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_SHA384_512_HPP_4C735FA2_1C10_4B7A_B78C_F6FBF70A87EC
25 #define INC_SHA384_512_HPP_4C735FA2_1C10_4B7A_B78C_F6FBF70A87EC
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  uint64_t h0;
49  uint64_t h1;
50  uint64_t h2;
51  uint64_t h3;
52  uint64_t h4;
53  uint64_t h5;
54  uint64_t h6;
55  uint64_t h7;
56 
57  uint64_t nblocks;
58  int count;
59  uint8_t ibuffer[128];
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 SHA384 final : public AbstractSHA512Impl
117 {
118  public:
122  SHA384();
123 
127  void reset() override;
128 
137  uint8_t* getValue(uint8_t* buffer) const override;
138 
146  size_t getSize() const override { return 48; }
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-384";
190  }
191 
198 
208  static constexpr ChecksumAlgoId getIdentifier()
209  {
210  return ChecksumAlgoId::SHA2_384;
211  }
212 
222  {
223  return new SHA384();
224  }
225 };
226 //---------------------------------------------------------------------------
227 
228 
251 class SHA512 final : public AbstractSHA512Impl
252 {
253  public:
257  SHA512();
258 
262  void reset() override;
263 
272  uint8_t* getValue(uint8_t* buffer) const override;
273 
281  size_t getSize() const override { return 64; }
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 
317 
323  static std::string getHashName()
324  {
325  return "SHA2-512";
326  }
327 
334 
344  static constexpr ChecksumAlgoId getIdentifier()
345  {
346  return ChecksumAlgoId::SHA2_512;
347  }
348 
358  {
359  return new SHA512();
360  }
361 };
362 //---------------------------------------------------------------------------
363 } // namespace libxcks
364 //---------------------------------------------------------------------------
365 
366 #endif // INC_SHA384_512_HPP_4C735FA2_1C10_4B7A_B78C_F6FBF70A87EC
Computes the SHA512 hash from a byte stream.
Definition: sha384_512.hpp:45
uint64_t h4
Fifth part of the state of computation.
Definition: sha384_512.hpp:52
void transform(uint8_t *data)
Transform the message X which consists of 16 64-bit-words.
Definition: sha384_512.cpp:205
uint64_t h7
Eighth part of the state of computation.
Definition: sha384_512.hpp:55
int count
Current size of the input buffer.
Definition: sha384_512.hpp:58
void update(const uint8_t *buf, size_t len) override final
Updates the SHA512 hash with specified array of bytes.
Definition: sha384_512.cpp:164
uint64_t h2
Third part of the state of computation.
Definition: sha384_512.hpp:50
void finish()
Process the remaining bytes in the internal buffer and the usual prolog according to the standard.
Definition: sha384_512.cpp:81
uint64_t h6
Seventh part of the state of computation.
Definition: sha384_512.hpp:54
uint64_t h5
Sixth part of the state of computation.
Definition: sha384_512.hpp:53
uint64_t nblocks
Number of blocks.
Definition: sha384_512.hpp:57
uint64_t h1
Second part of the state of computation.
Definition: sha384_512.hpp:49
void reset() override=0
Resets the checksum to initial value.
uint64_t h0
First part of the state of computation.
Definition: sha384_512.hpp:48
uint64_t h3
Fourth part of the state of computation.
Definition: sha384_512.hpp:51
uint8_t ibuffer[128]
Input buffer.
Definition: sha384_512.hpp:59
Computes a checksum from a byte stream.
Definition: checksum.hpp:54
Computes a hash from a byte stream.
Computes the SHA384 hash from a byte stream.
Definition: sha384_512.hpp:117
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: sha384_512.hpp:177
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: sha384_512.hpp:208
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: sha384_512.hpp:153
uint8_t * getValue(uint8_t *buffer) const override
Returns the SHA384 hash value in the first 48 bytes of the given address.
Definition: sha384_512.cpp:362
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: sha384_512.hpp:221
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA384 hash algorithm.
Definition: sha384_512.cpp:377
void reset() override
Resets the SHA384 hash to initial state of computation.
Definition: sha384_512.cpp:342
size_t getSize() const override
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: sha384_512.hpp:146
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: sha384_512.hpp:187
SHA384()
Default constructor.
Definition: sha384_512.cpp:332
ArrayString getAltNames() const override
Returns the alternative names of the SHA384 hash algorithm.
Definition: sha384_512.hpp:163
Computes the SHA512 hash from a byte stream.
Definition: sha384_512.hpp:252
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: sha384_512.hpp:344
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: sha384_512.hpp:357
SHA512()
Default constructor.
Definition: sha384_512.cpp:393
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA512 hash algorithm.
Definition: sha384_512.cpp:438
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: sha384_512.hpp:288
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: sha384_512.hpp:323
uint8_t * getValue(uint8_t *buffer) const override
Returns the SHA512 hash value in the first 64 bytes of the given address.
Definition: sha384_512.cpp:423
size_t getSize() const override
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: sha384_512.hpp:281
void reset() override
Resets the SHA512 hash to initial state of computation.
Definition: sha384_512.cpp:403
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: sha384_512.hpp:312
ArrayString getAltNames() const override
Returns the alternative names of the SHA512 hash algorithm.
Definition: sha384_512.hpp:298
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