This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
md5.h
207 lines (191 loc) · 6.35 KB
/
md5.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef __MD5_H__
#define __MD5_H__
/*
* Size of a standard MD5 signature in bytes. This definition is for
* external programs only. The MD5 routines themselves reference the
* signature as 4 unsigned 32-bit integers.
*/
const unsigned int MD5_SIZE = (4 * sizeof(unsigned int)); /* 16 */
const unsigned int MD5_STRING_SIZE = 2 * MD5_SIZE + 1; /* 33 */
namespace md5 {
/*
* The MD5 algorithm works on blocks of characters of 64 bytes. This
* is an internal value only and is not necessary for external use.
*/
const unsigned int BLOCK_SIZE = 64;
class md5_t {
public:
/*
* md5_t
*
* DESCRIPTION:
*
* Initialize structure containing state of MD5 computation. (RFC 1321,
* 3.3: Step 3). This is for progressive MD5 calculations only. If
* you have the complete string available, call it as below.
* process should be called for each bunch of bytes and after the last
* process call, finish should be called to get the signature.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* None.
*/
md5_t();
/*
* md5_t
*
* DESCRIPTION:
*
* This function is used to calculate a MD5 signature for a buffer of
* bytes. If you only have part of a buffer that you want to process
* then md5_t, process, and finish should be used.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* input - A buffer of bytes whose MD5 signature we are calculating.
*
* input_length - The length of the buffer.
*
* signature_ - A 16 byte buffer that will contain the MD5 signature.
*/
md5_t(const void* input, const unsigned int input_length, void* signature_ = NULL);
/*
* process
*
* DESCRIPTION:
*
* This function is used to progressively calculate an MD5 signature some
* number of bytes at a time.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* input - A buffer of bytes whose MD5 signature we are calculating.
*
* input_length - The length of the buffer.
*/
void process(const void* input, const unsigned int input_length);
/*
* finish
*
* DESCRIPTION:
*
* Finish a progressing MD5 calculation and copy the resulting MD5
* signature into the result buffer which should be 16 bytes
* (MD5_SIZE). After this call, the MD5 structure cannot be used
* to calculate a new md5, it can only return its signature.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* signature_ - A 16 byte buffer that will contain the MD5 signature.
*/
void finish(void* signature_ = NULL);
/*
* get_sig
*
* DESCRIPTION:
*
* Retrieves the previously calculated signature from the MD5 object.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* signature_ - A 16 byte buffer that will contain the MD5 signature.
*/
void get_sig(void* signature_);
/*
* get_string
*
* DESCRIPTION:
*
* Retrieves the previously calculated signature from the MD5 object in
* printable format.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* str_ - a string of characters which should be at least 33 bytes long
* (2 characters per MD5 byte and 1 for the \0).
*/
void get_string(void* str_);
private:
/* internal functions */
void initialise();
void process_block(const unsigned char*);
void get_result(void*);
unsigned int A; /* accumulator 1 */
unsigned int B; /* accumulator 2 */
unsigned int C; /* accumulator 3 */
unsigned int D; /* accumulator 4 */
unsigned int message_length[2]; /* length of data */
unsigned int stored_size; /* length of stored bytes */
unsigned char stored[md5::BLOCK_SIZE * 2]; /* stored bytes */
bool finished; /* object state */
char signature[MD5_SIZE]; /* stored signature */
char str[MD5_STRING_SIZE]; /* stored plain text hash */
};
/*
* sig_to_string
*
* DESCRIPTION:
*
* Convert a MD5 signature in a 16 byte buffer into a hexadecimal string
* representation.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* signature - a 16 byte buffer that contains the MD5 signature.
*
* str - a string of characters which should be at least 33 bytes long (2
* characters per MD5 byte and 1 for the \0).
*
* str_len - the length of the string.
*/
extern void sig_to_string(const void* signature, char* str, const int str_len);
/*
* sig_from_string
*
* DESCRIPTION:
*
* Convert a MD5 signature from a hexadecimal string representation into
* a 16 byte buffer.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* signature - A 16 byte buffer that will contain the MD5 signature.
*
* str - A string of charactes which _must_ be at least 32 bytes long (2
* characters per MD5 byte).
*/
extern void sig_from_string(void* signature, const char* str);
} // namespace md5
#endif /* ! __MD5_H__ */