Blender
V5.0
source
blender
blenlib
intern
hash_md5.cc
Go to the documentation of this file.
1
/* SPDX-FileCopyrightText: 1995 Free Software Foundation, Inc.
2
*
3
* SPDX-License-Identifier: GPL-2.0-or-later
4
* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>. */
5
12
13
#include <cstdio>
14
#include <cstdlib>
15
#include <cstring>
16
#include <sys/types.h>
17
18
#include "
BLI_hash_md5.hh
"
/* own include */
19
20
#if defined HAVE_LIMITS_H || defined _LIBC
21
# include <limits.h>
22
#endif
23
24
/* The following contortions are an attempt to use the C preprocessor to determine an unsigned
25
* integral type that is 32 bits wide.
26
* An alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but doing that would require
27
* that the configure script compile and *run* the resulting executable.
28
* Locally running cross-compiled executables is usually not possible.
29
*/
30
31
#if defined __STDC__ && __STDC__
32
# define UINT_MAX_32_BITS 4294967295U
33
#else
34
# define UINT_MAX_32_BITS 0xFFFFFFFF
35
#endif
36
37
/* If UINT_MAX isn't defined, assume it's a 32-bit type.
38
* This should be valid for all systems GNU cares about
39
* because that doesn't include 16-bit systems, and only modern systems
40
* (that certainly have <limits.h>) have 64+-bit integral types.
41
*/
42
43
#ifndef UINT_MAX
44
# define UINT_MAX UINT_MAX_32_BITS
45
#endif
46
47
#if UINT_MAX == UINT_MAX_32_BITS
48
using
md5_uint32
=
uint
;
49
#else
50
# if USHRT_MAX == UINT_MAX_32_BITS
51
using
md5_uint32
=
unsigned
short;
52
# else
53
# if ULONG_MAX == UINT_MAX_32_BITS
54
using
md5_uint32
=
unsigned
long
;
55
# else
56
/* The following line is intended to evoke an error. Using #error is not portable enough. */
57
"Cannot determine unsigned 32-bit data type."
58
# endif
59
# endif
60
#endif
61
62
/* Following code is low level, upon which are built up the functions
63
* #BLI_hash_md5_stream and #BLI_hash_md5_buffer. */
64
65
/* Structure to save state of computation between the single steps. */
66
struct
md5_ctx
{
67
md5_uint32
A
;
68
md5_uint32
B
;
69
md5_uint32
C
;
70
md5_uint32
D
;
71
};
72
73
/* NOTE: this is endianness-sensitive. */
74
#define SWAP(n) (n)
75
76
/* This array contains the bytes used to pad the buffer to the next 64-byte boundary.
77
* (RFC 1321, 3.1: Step 1) */
78
static
const
uchar
fillbuf
[64] = {0x80, 0
/* , 0, 0, ... */
};
79
84
static
void
md5_init_ctx
(
md5_ctx
*ctx)
85
{
86
ctx->
A
= 0x67452301;
87
ctx->
B
= 0xefcdab89;
88
ctx->
C
= 0x98badcfe;
89
ctx->
D
= 0x10325476;
90
}
91
97
static
void
md5_process_block
(
const
void
*buffer,
size_t
len
,
md5_ctx
*ctx)
98
{
99
/* These are the four functions used in the four steps of the MD5 algorithm and defined in the
100
* RFC 1321. The first function is a little bit optimized
101
* (as found in Colin Plumbs public domain implementation).
102
*/
103
// #define FF(b, c, d) ((b & c) | (~b & d))
104
#define FF(b, c, d) (d ^ (b & (c ^ d)))
105
#define FG(b, c, d) FF(d, b, c)
106
#define FH(b, c, d) (b ^ c ^ d)
107
#define FI(b, c, d) (c ^ (b | ~d))
108
109
/* It is unfortunate that C does not provide an operator for cyclic rotation.
110
* Hope the C compiler is smart enough. */
111
#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
112
113
md5_uint32
correct_words[16];
114
const
md5_uint32
*words =
static_cast<
const
md5_uint32
*
>
(buffer);
115
size_t
nwords =
len
/
sizeof
(
md5_uint32
);
116
const
md5_uint32
*endp = words + nwords;
117
md5_uint32
A
= ctx->
A
;
118
md5_uint32
B
= ctx->
B
;
119
md5_uint32
C
= ctx->
C
;
120
md5_uint32
D
= ctx->
D
;
121
122
/* Process all bytes in the buffer with 64 bytes in each round of the loop. */
123
while
(words < endp) {
124
md5_uint32
*cwp = correct_words;
125
md5_uint32
A_save =
A
;
126
md5_uint32
B_save =
B
;
127
md5_uint32
C_save =
C
;
128
md5_uint32
D_save =
D
;
129
130
/* First round: using the given function, the context and a constant the next context is
131
* computed. Because the algorithms processing unit is a 32-bit word and it is determined
132
* to work on words in little endian byte order we perhaps have to change the byte order
133
* before the computation. To reduce the work for the next steps we store the swapped words
134
* in the array CORRECT_WORDS.
135
*/
136
#define OP(a, b, c, d, s, T) \
137
a += FF(b, c, d) + (*cwp++ = SWAP(*words)) + T; \
138
words++; \
139
CYCLIC(a, s); \
140
a += b; \
141
(void)0
142
143
/* Before we start, one word to the strange constants. They are defined in RFC 1321 as:
144
* `T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64`
145
*/
146
147
/* Round 1. */
148
OP
(
A
,
B
,
C
,
D
, 7, 0xd76aa478);
149
OP
(
D
,
A
,
B
,
C
, 12, 0xe8c7b756);
150
OP
(
C
,
D
,
A
,
B
, 17, 0x242070db);
151
OP
(
B
,
C
,
D
,
A
, 22, 0xc1bdceee);
152
OP
(
A
,
B
,
C
,
D
, 7, 0xf57c0faf);
153
OP
(
D
,
A
,
B
,
C
, 12, 0x4787c62a);
154
OP
(
C
,
D
,
A
,
B
, 17, 0xa8304613);
155
OP
(
B
,
C
,
D
,
A
, 22, 0xfd469501);
156
OP
(
A
,
B
,
C
,
D
, 7, 0x698098d8);
157
OP
(
D
,
A
,
B
,
C
, 12, 0x8b44f7af);
158
OP
(
C
,
D
,
A
,
B
, 17, 0xffff5bb1);
159
OP
(
B
,
C
,
D
,
A
, 22, 0x895cd7be);
160
OP
(
A
,
B
,
C
,
D
, 7, 0x6b901122);
161
OP
(
D
,
A
,
B
,
C
, 12, 0xfd987193);
162
OP
(
C
,
D
,
A
,
B
, 17, 0xa679438e);
163
OP
(
B
,
C
,
D
,
A
, 22, 0x49b40821);
164
165
#undef OP
166
167
/* For the second to fourth round we have the possibly swapped words in CORRECT_WORDS.
168
* Redefine the macro to take an additional first argument specifying the function to use.
169
*/
170
#define OP(f, a, b, c, d, k, s, T) \
171
a += f(b, c, d) + correct_words[k] + T; \
172
CYCLIC(a, s); \
173
a += b; \
174
(void)0
175
176
/* Round 2. */
177
OP
(
FG
,
A
,
B
,
C
,
D
, 1, 5, 0xf61e2562);
178
OP
(
FG
,
D
,
A
,
B
,
C
, 6, 9, 0xc040b340);
179
OP
(
FG
,
C
,
D
,
A
,
B
, 11, 14, 0x265e5a51);
180
OP
(
FG
,
B
,
C
,
D
,
A
, 0, 20, 0xe9b6c7aa);
181
OP
(
FG
,
A
,
B
,
C
,
D
, 5, 5, 0xd62f105d);
182
OP
(
FG
,
D
,
A
,
B
,
C
, 10, 9, 0x02441453);
183
OP
(
FG
,
C
,
D
,
A
,
B
, 15, 14, 0xd8a1e681);
184
OP
(
FG
,
B
,
C
,
D
,
A
, 4, 20, 0xe7d3fbc8);
185
OP
(
FG
,
A
,
B
,
C
,
D
, 9, 5, 0x21e1cde6);
186
OP
(
FG
,
D
,
A
,
B
,
C
, 14, 9, 0xc33707d6);
187
OP
(
FG
,
C
,
D
,
A
,
B
, 3, 14, 0xf4d50d87);
188
OP
(
FG
,
B
,
C
,
D
,
A
, 8, 20, 0x455a14ed);
189
OP
(
FG
,
A
,
B
,
C
,
D
, 13, 5, 0xa9e3e905);
190
OP
(
FG
,
D
,
A
,
B
,
C
, 2, 9, 0xfcefa3f8);
191
OP
(
FG
,
C
,
D
,
A
,
B
, 7, 14, 0x676f02d9);
192
OP
(
FG
,
B
,
C
,
D
,
A
, 12, 20, 0x8d2a4c8a);
193
194
/* Round 3. */
195
OP
(
FH
,
A
,
B
,
C
,
D
, 5, 4, 0xfffa3942);
196
OP
(
FH
,
D
,
A
,
B
,
C
, 8, 11, 0x8771f681);
197
OP
(
FH
,
C
,
D
,
A
,
B
, 11, 16, 0x6d9d6122);
198
OP
(
FH
,
B
,
C
,
D
,
A
, 14, 23, 0xfde5380c);
199
OP
(
FH
,
A
,
B
,
C
,
D
, 1, 4, 0xa4beea44);
200
OP
(
FH
,
D
,
A
,
B
,
C
, 4, 11, 0x4bdecfa9);
201
OP
(
FH
,
C
,
D
,
A
,
B
, 7, 16, 0xf6bb4b60);
202
OP
(
FH
,
B
,
C
,
D
,
A
, 10, 23, 0xbebfbc70);
203
OP
(
FH
,
A
,
B
,
C
,
D
, 13, 4, 0x289b7ec6);
204
OP
(
FH
,
D
,
A
,
B
,
C
, 0, 11, 0xeaa127fa);
205
OP
(
FH
,
C
,
D
,
A
,
B
, 3, 16, 0xd4ef3085);
206
OP
(
FH
,
B
,
C
,
D
,
A
, 6, 23, 0x04881d05);
207
OP
(
FH
,
A
,
B
,
C
,
D
, 9, 4, 0xd9d4d039);
208
OP
(
FH
,
D
,
A
,
B
,
C
, 12, 11, 0xe6db99e5);
209
OP
(
FH
,
C
,
D
,
A
,
B
, 15, 16, 0x1fa27cf8);
210
OP
(
FH
,
B
,
C
,
D
,
A
, 2, 23, 0xc4ac5665);
211
212
/* Round 4. */
213
OP
(
FI
,
A
,
B
,
C
,
D
, 0, 6, 0xf4292244);
214
OP
(
FI
,
D
,
A
,
B
,
C
, 7, 10, 0x432aff97);
215
OP
(
FI
,
C
,
D
,
A
,
B
, 14, 15, 0xab9423a7);
216
OP
(
FI
,
B
,
C
,
D
,
A
, 5, 21, 0xfc93a039);
217
OP
(
FI
,
A
,
B
,
C
,
D
, 12, 6, 0x655b59c3);
218
OP
(
FI
,
D
,
A
,
B
,
C
, 3, 10, 0x8f0ccc92);
219
OP
(
FI
,
C
,
D
,
A
,
B
, 10, 15, 0xffeff47d);
220
OP
(
FI
,
B
,
C
,
D
,
A
, 1, 21, 0x85845dd1);
221
OP
(
FI
,
A
,
B
,
C
,
D
, 8, 6, 0x6fa87e4f);
222
OP
(
FI
,
D
,
A
,
B
,
C
, 15, 10, 0xfe2ce6e0);
223
OP
(
FI
,
C
,
D
,
A
,
B
, 6, 15, 0xa3014314);
224
OP
(
FI
,
B
,
C
,
D
,
A
, 13, 21, 0x4e0811a1);
225
OP
(
FI
,
A
,
B
,
C
,
D
, 4, 6, 0xf7537e82);
226
OP
(
FI
,
D
,
A
,
B
,
C
, 11, 10, 0xbd3af235);
227
OP
(
FI
,
C
,
D
,
A
,
B
, 2, 15, 0x2ad7d2bb);
228
OP
(
FI
,
B
,
C
,
D
,
A
, 9, 21, 0xeb86d391);
229
230
#undef OP
231
232
/* Add the starting values of the context. */
233
A
+= A_save;
234
B
+= B_save;
235
C
+= C_save;
236
D
+= D_save;
237
}
238
239
/* Put checksum in context given as argument. */
240
ctx->
A
=
A
;
241
ctx->
B
=
B
;
242
ctx->
C
=
C
;
243
ctx->
D
=
D
;
244
245
#undef FF
246
#undef FG
247
#undef FH
248
#undef FI
249
#undef CYCLIC
250
}
251
257
static
void
*
md5_read_ctx
(
const
md5_ctx
*ctx,
void
*resbuf)
258
{
259
md5_uint32
*digest =
static_cast<
md5_uint32
*
>
(resbuf);
260
digest[0] =
SWAP
(ctx->
A
);
261
digest[1] =
SWAP
(ctx->
B
);
262
digest[2] =
SWAP
(ctx->
C
);
263
digest[3] =
SWAP
(ctx->
D
);
264
265
return
resbuf;
266
}
267
268
/* Top level public functions. */
269
270
int
BLI_hash_md5_stream
(FILE *stream,
void
*resblock)
271
{
272
#define BLOCKSIZE 4096
/* IMPORTANT: must be a multiple of 64. */
273
md5_ctx
ctx;
274
md5_uint32
len
[2];
275
char
buffer[
BLOCKSIZE
+ 72];
276
size_t
pad
,
sum
;
277
278
/* Initialize the computation context. */
279
md5_init_ctx
(&ctx);
280
281
len
[0] = 0;
282
len
[1] = 0;
283
284
/* Iterate over full file contents. */
285
while
(
true
) {
286
/* We read the file in blocks of BLOCKSIZE bytes.
287
* One call of the computation function processes the whole buffer
288
* so that with the next round of the loop another block can be read.
289
*/
290
size_t
n;
291
sum
= 0;
292
293
/* Read block. Take care for partial reads. */
294
do
{
295
n = fread(buffer, 1,
BLOCKSIZE
-
sum
, stream);
296
sum
+= n;
297
}
while
(
sum
<
BLOCKSIZE
&& n != 0);
298
299
if
(n == 0 && ferror(stream)) {
300
return
1;
301
}
302
303
/* RFC 1321 specifies the possible length of the file up to 2^64 bits.
304
* Here we only compute the number of bytes. Do a double word increment.
305
*/
306
len
[0] +=
sum
;
307
if
(
len
[0] <
sum
) {
308
++
len
[1];
309
}
310
311
/* If end of file is reached, end the loop. */
312
if
(n == 0) {
313
break
;
314
}
315
316
/* Process buffer with BLOCKSIZE bytes. Note that `BLOCKSIZE % 64 == 0`. */
317
md5_process_block
(buffer,
BLOCKSIZE
, &ctx);
318
}
319
320
/* We can copy 64 bytes because the buffer is always big enough.
321
* 'fillbuf' contains the needed bits. */
322
memcpy(&buffer[
sum
],
fillbuf
, 64);
323
324
/* Compute amount of padding bytes needed. Alignment is done to `(N + PAD) % 64 == 56`.
325
* There is always at least one byte padded, i.e. if the alignment is correctly aligned,
326
* 64 padding bytes are added.
327
*/
328
pad
=
sum
& 63;
329
pad
=
pad
>= 56 ? 64 + 56 -
pad
: 56 -
pad
;
330
331
/* Put the 64-bit file length in *bits* at the end of the buffer. */
332
*(
md5_uint32
*)&buffer[
sum
+
pad
] =
SWAP
(
len
[0] << 3);
333
*(
md5_uint32
*)&buffer[
sum
+
pad
+ 4] =
SWAP
((
len
[1] << 3) | (
len
[0] >> 29));
334
335
/* Process last bytes. */
336
md5_process_block
(buffer,
sum
+
pad
+ 8, &ctx);
337
338
/* Construct result in desired memory. */
339
md5_read_ctx
(&ctx, resblock);
340
return
0;
341
}
342
343
void
*
BLI_hash_md5_buffer
(
const
char
*buffer,
size_t
len
,
void
*resblock)
344
{
345
md5_ctx
ctx;
346
char
restbuf[64 + 72];
347
size_t
blocks =
len
& ~63;
348
size_t
pad
, rest;
349
350
/* Initialize the computation context. */
351
md5_init_ctx
(&ctx);
352
353
/* Process whole buffer but last len % 64 bytes. */
354
md5_process_block
(buffer, blocks, &ctx);
355
356
/* REST bytes are not processed yet. */
357
rest =
len
- blocks;
358
/* Copy to own buffer. */
359
memcpy(restbuf, &buffer[blocks], rest);
360
/* Append needed fill bytes at end of buffer.
361
* We can copy 64 bytes because the buffer is always big enough. */
362
memcpy(&restbuf[rest],
fillbuf
, 64);
363
364
/* PAD bytes are used for padding to correct alignment.
365
* Note that always at least one byte is padded. */
366
pad
= rest >= 56 ? 64 + 56 - rest : 56 - rest;
367
368
/* Put length of buffer in *bits* in last eight bytes. */
369
*(
md5_uint32
*)&restbuf[rest +
pad
] = (
md5_uint32
)
SWAP
(
len
<< 3);
370
*(
md5_uint32
*)&restbuf[rest +
pad
+ 4] = (
md5_uint32
)
SWAP
(
len
>> 29);
371
372
/* Process last bytes. */
373
md5_process_block
(restbuf, rest +
pad
+ 8, &ctx);
374
375
/* Put result in desired memory area. */
376
return
md5_read_ctx
(&ctx, resblock);
377
}
378
379
char
*
BLI_hash_md5_to_hexdigest
(
const
void
*resblock,
char
r_hex_digest[33])
380
{
381
static
const
char
hex_map[17] =
"0123456789abcdef"
;
382
const
uchar
*p;
383
char
*q;
384
short
len
;
385
386
for
(q = r_hex_digest, p = (
const
uchar
*)resblock,
len
= 0;
len
< 16; p++,
len
++) {
387
const
uchar
c = *p;
388
*q++ = hex_map[c >> 4];
389
*q++ = hex_map[c & 15];
390
}
391
*q =
'\0'
;
392
393
return
r_hex_digest;
394
}
D
#define D
BLI_hash_md5.hh
uchar
unsigned char uchar
Definition
BLI_sys_types.h:67
uint
unsigned int uint
Definition
BLI_sys_types.h:64
C
#define C
Definition
RandGen.cpp:29
pad
int pad[32 - sizeof(int)]
Definition
atomic_ops_unix.h:3
A
#define A
sum
static T sum(const btAlignedObjectArray< T > &items)
Definition
btSoftBodyHelpers.cpp:94
long
#define long
Definition
gpu_shader_compat_cxx.hh:164
fillbuf
static const uchar fillbuf[64]
Definition
hash_md5.cc:78
FH
#define FH(b, c, d)
md5_uint32
uint md5_uint32
Definition
hash_md5.cc:48
FG
#define FG(b, c, d)
md5_read_ctx
static void * md5_read_ctx(const md5_ctx *ctx, void *resbuf)
Definition
hash_md5.cc:257
OP
#define OP(a, b, c, d, s, T)
BLI_hash_md5_to_hexdigest
char * BLI_hash_md5_to_hexdigest(const void *resblock, char r_hex_digest[33])
Definition
hash_md5.cc:379
md5_init_ctx
static void md5_init_ctx(md5_ctx *ctx)
Definition
hash_md5.cc:84
BLI_hash_md5_stream
int BLI_hash_md5_stream(FILE *stream, void *resblock)
Definition
hash_md5.cc:270
FI
#define FI(b, c, d)
SWAP
#define SWAP(n)
Definition
hash_md5.cc:74
md5_process_block
static void md5_process_block(const void *buffer, size_t len, md5_ctx *ctx)
Definition
hash_md5.cc:97
BLI_hash_md5_buffer
void * BLI_hash_md5_buffer(const char *buffer, size_t len, void *resblock)
Definition
hash_md5.cc:343
BLOCKSIZE
#define BLOCKSIZE
B
#define B
Definition
mball_tessellate.cc:273
md5_ctx
Definition
hash_md5.cc:66
md5_ctx::B
md5_uint32 B
Definition
hash_md5.cc:68
md5_ctx::C
md5_uint32 C
Definition
hash_md5.cc:69
md5_ctx::A
md5_uint32 A
Definition
hash_md5.cc:67
md5_ctx::D
md5_uint32 D
Definition
hash_md5.cc:70
len
uint len
Definition
uvedit_unwrap_ops.cc:2126
Generated on
for Blender by
doxygen
1.16.1