Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
MD5.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_MD5
19#define INCLUDED_MD5
20
21#include "lib/code_annotation.h"
22
23#include <cstring>
24
25/**
26 * MD5 hashing algorithm. Note that MD5 is broken and must not be used for
27 * anything that requires security.
28 */
29class MD5
30{
31public:
32 static const size_t DIGESTSIZE = 16;
33
34 MD5();
35
36 void Update(const std::uint8_t* data, size_t len)
37 {
38 // (Defined inline for efficiency in the common fixed-length fits-in-buffer case)
39
40 const size_t CHUNK_SIZE = sizeof(m_Buf);
41
42 m_InputLen += len;
43
44 // GCC thinks `m_Buf + m_BufLen` can be outside the bound of `m_Buf`. That is because
45 // `m_BufLen + len < CHUNK_SIZE` can evaluate to `true` even if `m_BufLen` is bigger than
46 // `CHUNK_SIZE` due to wrapping. Tell GCC that it's not possible.
47 if (m_BufLen >= CHUNK_SIZE)
49
50 // If we have enough space in m_Buf and won't flush, simply append the input
51 if (m_BufLen + len < CHUNK_SIZE)
52 {
53 memcpy(m_Buf + m_BufLen, data, len);
54 m_BufLen += len;
55 return;
56 }
57
58 // Fall back to non-inline function if we have to do more work
59 UpdateRest(data, len);
60 }
61
62 void Final(std::uint8_t* digest);
63
64private:
65 void InitState();
66 void UpdateRest(const std::uint8_t* data, size_t len);
67 void Transform(const std::uint32_t* in);
68 std::uint32_t m_Digest[4]; // internal state
69 std::uint8_t m_Buf[64]; // buffered input bytes
70 size_t m_BufLen; // bytes in m_Buf that are valid
71 std::uint64_t m_InputLen; // bytes
72};
73
74#endif // INCLUDED_MD5
MD5 hashing algorithm.
Definition: MD5.h:30
static const size_t DIGESTSIZE
Definition: MD5.h:32
void Transform(const std::uint32_t *in)
Definition: MD5.cpp:105
void Update(const std::uint8_t *data, size_t len)
Definition: MD5.h:36
void UpdateRest(const std::uint8_t *data, size_t len)
Definition: MD5.cpp:42
std::uint32_t m_Digest[4]
Definition: MD5.h:68
size_t m_BufLen
Definition: MD5.h:70
void InitState()
Definition: MD5.cpp:31
std::uint8_t m_Buf[64]
Definition: MD5.h:69
std::uint64_t m_InputLen
Definition: MD5.h:71
MD5()
Definition: MD5.cpp:26
void Final(std::uint8_t *digest)
Definition: MD5.cpp:75
#define UNREACHABLE
"unreachable code" helpers
Definition: code_annotation.h:130