libxcks  0.1.0.1
md5.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_MD5_HPP_2148BE88_EEAD_409F_85F0_E0E0420BC431
25 #define INC_MD5_HPP_2148BE88_EEAD_409F_85F0_E0E0420BC431
26 
27 //---------------------------------------------------------------------------
28 #include "libgcrypthash.hpp"
29 //---------------------------------------------------------------------------
30 
31 
32 namespace libxcks
33 {
55 class MD5 final : public LibgcryptHash
56 {
57  protected:
58  // State of computation between the single steps.
59  uint32_t A_;
60  uint32_t B_;
61  uint32_t C_;
62  uint32_t D_;
63 
64  uint32_t nblocks;
65  int count;
66  uint8_t ibuffer[64];
67 
68  public:
72  MD5();
73 
77  void reset() override;
78 
89  uint8_t* getValue(uint8_t* buffer) const override;
90 
98  size_t getSize() const override{ return 16; }
99 
106  void update(const uint8_t* buf, size_t len) override;
107 
108  protected:
113  void finish();
114 
122  void transform(uint8_t* data);
123 
124  public:
130  std::string getName() const override
131  {
132  return getHashName();
133  }
134 
144  ChecksumAlgoId getID() const override
145  {
146  return getIdentifier();
147  }
148 
154  static std::string getHashName()
155  {
156  return "MD5";
157  }
158 
168  static constexpr ChecksumAlgoId getIdentifier()
169  {
170  return ChecksumAlgoId::MD5;
171  }
172 
182  {
183  return new MD5();
184  }
185 };
186 //---------------------------------------------------------------------------
187 } // namespace libxcks
188 //---------------------------------------------------------------------------
189 
190 #endif // INC_MD5_HPP_2148BE88_EEAD_409F_85F0_E0E0420BC431
Computes a checksum from a byte stream.
Definition: checksum.hpp:54
Computes a hash from a byte stream.
Computes the MD5 hash from a byte stream.
Definition: md5.hpp:56
void update(const uint8_t *buf, size_t len) override
Updates the MD5 hash with specified array of bytes.
Definition: md5.cpp:150
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: md5.hpp:154
size_t getSize() const override
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: md5.hpp:98
uint32_t B_
Second part of the state of computation.
Definition: md5.hpp:60
uint32_t nblocks
Number of blocks.
Definition: md5.hpp:64
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: md5.hpp:130
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: md5.hpp:168
uint32_t C_
Third part of the state of computation.
Definition: md5.hpp:61
uint8_t * getValue(uint8_t *buffer) const override
Returns the MD5 hash value in the first 16 bytes of the given address.
Definition: md5.cpp:347
uint32_t A_
First part of the state of computation.
Definition: md5.hpp:59
int count
Current size of the input buffer.
Definition: md5.hpp:65
void reset() override
Resets the MD5 hash to initial state of computation.
Definition: md5.cpp:67
MD5()
Default constructor.
Definition: md5.cpp:57
void finish()
Process the remaining bytes in the internal buffer and the usual prolog according to the standard.
Definition: md5.cpp:84
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: md5.hpp:181
uint8_t ibuffer[64]
Input buffer.
Definition: md5.hpp:66
uint32_t D_
Fourth part of the state of computation.
Definition: md5.hpp:62
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: md5.hpp:144
void transform(uint8_t *data)
Transform n*64 bytes.
Definition: md5.cpp:201
Interface for classes from the Libgcrypt that compute hashes.
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65