lhashes  1.0.0
C++ hashes library
lhashes_md5.h
Go to the documentation of this file.
1 /*
2  * ======================================================================================
3  * __ ____ __ __ ____ ___
4  * ( ) (_ _)( \/ )( ___)/ __)
5  * )(__ _)(_ ) ( )__) \__ \
6  * (____)(____)(_/\/\_)(____)(___/
7  *
8  * This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
9  *
10  * Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
11  *
12  * ======================================================================================
13  */
14 
15 #pragma once
16 
17 #include <string>
18 #include <string_view>
19 #include <cstdint>
20 #include "lhashes/lhashes_Export.h"
21 #include "lhashes/lhashes_hash.h"
22 
29 namespace limes::hash
30 {
31 
39 class LHASH_EXPORT MD5 final : public Hasher // cppcheck-suppress noConstructor
40 {
41 public:
43  void update (const unsigned char* input, std::size_t length) final;
44 
46  [[nodiscard]] std::string getHash() final;
47 
49  [[nodiscard]] std::size_t getLengthOfHash() const final;
50 
51 private:
52  void transform (const std::uint8_t* block) noexcept;
53 
54  static constexpr auto blocksize = 64;
55 
56  std::uint8_t buffer[blocksize] = {};
57  std::uint32_t count[2] = { 0, 0 };
58  std::uint32_t state[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
59  std::uint8_t digest[16] = {};
60 };
61 
67 [[nodiscard]] LHASH_EXPORT std::string md5 (const char* input, std::size_t length);
68 
74 [[nodiscard]] LHASH_EXPORT std::string md5 (std::string_view input);
75 
76 } // namespace limes::hash
A base class representing an object that calculates a hash function.
Definition: lhashes_hash.h:69
A Hasher object that calculates an MD5 hash.
Definition: lhashes_md5.h:40
std::string getHash() final
Retrieves the calculated MD5 hash value as a string.
void update(const unsigned char *input, std::size_t length) final
Updates the internal state of the hasher with new data.
LHASH_EXPORT std::string md5(const char *input, std::size_t length)
Calculates an MD5 hash for the given data.
LHASH_EXPORT std::string md5(std::string_view input)
Calculates an MD5 hash for the given string.
This file defines the hash::Hasher class and the hash::hash() free functions.
Cryptographic hash functions.