tlx
Loading...
Searching...
No Matches
md5.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/digest/md5.hpp
3 *
4 * Public domain implementation of MD-5 processor. Based on LibTomCrypt from
5 * https://github.com/libtom/libtomcrypt.git
6 *
7 * Part of tlx - http://panthema.net/tlx
8 *
9 * Copyright (C) 2018 Timo Bingmann <tb@panthema.net>
10 *
11 * All rights reserved. Published under the Boost Software License, Version 1.0
12 ******************************************************************************/
13
14#ifndef TLX_DIGEST_MD5_HEADER
15#define TLX_DIGEST_MD5_HEADER
16
17#include <cstdint>
18#include <string>
19
20namespace tlx {
21
22//! \addtogroup tlx_digest
23//! \{
24
25/*!
26 * MD-5 processor without external dependencies.
27 */
28class MD5
29{
30public:
31 //! construct empty object.
32 MD5();
33 //! construct context and process data range
34 MD5(const void* data, std::uint32_t size);
35 //! construct context and process string
36 explicit MD5(const std::string& str);
37
38 //! process more data
39 void process(const void* data, std::uint32_t size);
40 //! process more data
41 void process(const std::string& str);
42
43 //! digest length in bytes
44 static constexpr size_t kDigestLength = 16;
45
46 //! finalize computation and output 16 byte (128 bit) digest
47 void finalize(void* digest);
48
49 //! finalize computation and return 16 byte (128 bit) digest
50 std::string digest();
51 //! finalize computation and return 16 byte (128 bit) digest hex encoded
52 std::string digest_hex();
53 //! finalize computation and return 16 byte (128 bit) digest upper-case hex
54 std::string digest_hex_uc();
55
56private:
57 std::uint64_t length_;
58 std::uint32_t state_[4];
59 std::uint32_t curlen_;
60 std::uint8_t buf_[64];
61};
62
63//! process data and return 16 byte (128 bit) digest hex encoded
64std::string md5_hex(const void* data, std::uint32_t size);
65//! process data and return 16 byte (128 bit) digest hex encoded
66std::string md5_hex(const std::string& str);
67
68//! process data and return 16 byte (128 bit) digest upper-case hex encoded
69std::string md5_hex_uc(const void* data, std::uint32_t size);
70//! process data and return 16 byte (128 bit) digest upper-case hex encoded
71std::string md5_hex_uc(const std::string& str);
72
73//! \}
74
75} // namespace tlx
76
77#endif // !TLX_DIGEST_MD5_HEADER
78
79/******************************************************************************/
MD-5 processor without external dependencies.
Definition: md5.hpp:29
void finalize(void *digest)
finalize computation and output 16 byte (128 bit) digest
Definition: md5.cpp:222
std::string digest_hex()
finalize computation and return 16 byte (128 bit) digest hex encoded
Definition: md5.cpp:260
std::string digest()
finalize computation and return 16 byte (128 bit) digest
Definition: md5.cpp:254
std::string digest_hex_uc()
finalize computation and return 16 byte (128 bit) digest upper-case hex
Definition: md5.cpp:266
std::uint32_t curlen_
Definition: md5.hpp:59
MD5()
construct empty object.
Definition: md5.cpp:167
static constexpr size_t kDigestLength
digest length in bytes
Definition: md5.hpp:44
std::uint8_t buf_[64]
Definition: md5.hpp:60
void process(const void *data, std::uint32_t size)
process more data
Definition: md5.cpp:184
std::uint64_t length_
Definition: md5.hpp:57
std::uint32_t state_[4]
Definition: md5.hpp:58
std::string md5_hex_uc(const void *data, std::uint32_t size)
process data and return 16 byte (128 bit) digest upper-case hex encoded
Definition: md5.cpp:280
std::string md5_hex(const void *data, std::uint32_t size)
process data and return 16 byte (128 bit) digest hex encoded
Definition: md5.cpp:272