Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Md5Hash.h
Go to the documentation of this file.
1#pragma once
2#include <cstdio>
3#include <cstring>
4#include <inttypes.h>
5#include <iostream>
6#include <misc/global.h>
7
8namespace sf
9{
10
15{
16 public:
20 typedef union
21 {
22 uint8_t data[16];
23 // For faster compare.
24 int64_t ints[2];
25 } hash_type;
26
29
31 explicit Md5Hash(const hash_type& hash);
32
34 explicit Md5Hash(const uint8_t* data, size_t length);
35
37 explicit Md5Hash(const char* data, size_t length);
38
42 explicit Md5Hash(const std::string& str);
43
48 void update(const uint8_t* data, size_t length);
49
51 void update(const char* data, size_t length);
52
54 void initialize();
55
60
62 [[nodiscard]] std::string hexDigest() const;
63
65 [[nodiscard]] hash_type hash() const;
66
72 static hash_type from(const std::string& hex_str);
73
74 private:
76 static constexpr size_t blockSize{64};
78 bool _finish{false};
80 uint32_t _count[2]{0, 0};
82 uint32_t _state[4]{0, 0, 0, 0};
84 uint8_t _buffer[blockSize]{0};
86 hash_type _result{0, 0};
87
89 void transform(const uint8_t*);
90};
91
96
100inline bool operator==(const Md5Hash& h1, const Md5Hash& h2)
101{
102 return h1.hash() == h2.hash();
103}
104
108inline std::ostream& operator<<(std::ostream& os, const Md5Hash& hash)
109{
110 return os << hash.hexDigest();
111}
112
116_MISC_FUNC std::istream& operator>>(std::istream& is, Md5Hash& h);
117
118}// namespace sf
Class replacing obsolete SSL 3.0 library MD5 functionality.
Definition Md5Hash.h:15
Md5Hash(const uint8_t *data, size_t length)
Initializing constructor using an unsigned character pointer.
void update(const uint8_t *data, size_t length)
MD5 block update operation. Continues an MD5 message-digest operation, processing another message blo...
Md5Hash()
Default constructor.
hash_type hash() const
Returns the resulting hash structure/union.
Md5Hash(const std::string &str)
Initializing constructor using a std::string.
static hash_type from(const std::string &hex_str)
Get hash_type using the passed hex string.
void initialize()
Initialization or reset.
void update(const char *data, size_t length)
MD5 block update operation with unsigned char.
Md5Hash(const hash_type &hash)
Initializing constructor using an unsigned character pointer.
Md5Hash(const char *data, size_t length)
Md5Hash & finalize()
MD5 finalization. Ends an MD5 message-digest operation writing the message digest and zeroing the con...
std::string hexDigest() const
Gets hex representation of digest as string.
#define _MISC_FUNC
Definition misc/global.h:39
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
_GII_FUNC std::istream & operator>>(std::istream &is, ResultData &)
Stream operator for setting up this instance with a setup std::string.
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
_MISC_FUNC bool operator==(const Md5Hash::hash_type &h1, const Md5Hash::hash_type &h2)
Compare operator for storing an MD5 hash.
Type for storing an MD5 hash.
Definition Md5Hash.h:21