sptk2 logo
SPTK Home Page
md5.h
1/* MD5
2 converted to C++ class by Frank Thilo (thilo@unix-ag.org)
3 for bzflag (http://www.bzflag.org)
4
5 based on:
6
7 md5.h and md5.c
8 reference implementation of RFC 1321
9
10 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
11rights reserved.
12
13License to copy and use this software is granted provided that it
14is identified as the "RSA Data Security, Inc. MD5 Message-Digest
15Algorithm" in all material mentioning or referencing this software
16or this function.
17
18License is also granted to make and use derivative works provided
19that such works are identified as "derived from the RSA Data
20Security, Inc. MD5 Message-Digest Algorithm" in all material
21mentioning or referencing the derived work.
22
23RSA Data Security, Inc. makes no representations concerning either
24the merchantability of this software or the suitability of this
25software for any particular purpose. It is provided "as is"
26without express or implied warranty of any kind.
27
28These notices must be retained in any copies of any part of this
29documentation and/or software.
30
31*/
32
33#pragma once
34
35#include <sptk5/sptk.h>
36#include <sptk5/String.h>
37#include <iostream>
38#include "Buffer.h"
39
40namespace sptk {
41
53 class SP_EXPORT MD5
54 {
55 public:
59 using size_type = unsigned int; // must be 32bit
60
64 MD5();
65
73 explicit MD5(const Buffer& data);
74
80 void update(const unsigned char* buffer, size_type length);
81
87 void update(const char* buffer, size_type length);
88
93
98
102 friend std::ostream& operator<<(std::ostream&, MD5 md5);
103
104 private:
105
109 void init();
110
114 using uint1 = uint8_t;
115
119 using uint4 = uint32_t;
120
124 static constexpr int blocksize = 64;
125
129 void transform(const uint1* block);
130
134 static void decode(uint4* output, const uint1* input, size_type len);
135
139 static void encode(uint1* output, const uint4* input, size_type len);
140
144 bool finalized {false};
145
149 std::array<uint1, blocksize> buffer {};
150
154 std::array<uint4, 2> count {};
155
159 std::array<uint4, 4> state {};
160
164 std::array<uint1, 16> digest {};
165
166
167 // low level logic operations
168 static inline uint4 F(uint4 x, uint4 y, uint4 z);
169
170 static inline uint4 G(uint4 x, uint4 y, uint4 z);
171
172 static inline uint4 H(uint4 x, uint4 y, uint4 z);
173
174 static inline uint4 I(uint4 x, uint4 y, uint4 z);
175
176 static inline uint4 rotate_left(uint4 x, int n);
177
178 static inline void FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
179
180 static inline void GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
181
182 static inline void HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
183
184 static inline void II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
185 };
186
190 SP_EXPORT String md5(const Buffer& data);
191
195 SP_EXPORT String md5(const String& data);
196
197}
198
Definition: Buffer.h:51
Definition: md5.h:54
MD5(const Buffer &data)
friend std::ostream & operator<<(std::ostream &, MD5 md5)
MD5 & finalize()
void update(const char *buffer, size_type length)
String hexdigest() const
unsigned int size_type
Definition: md5.h:59
void update(const unsigned char *buffer, size_type length)
Definition: String.h:49

Fri Oct 14 2022 09:58:31: SPTK 5.4.1