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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*****************************************************************************/
// Copyright 2006-2007 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/
/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_fingerprint.h#2 $ */
/* $DateTime: 2012/07/11 10:36:56 $ */
/* $Change: 838485 $ */
/* $Author: tknoll $ */
/** \file
* Fingerprint (cryptographic hashing) support for generating strong hashes of image
* data.
*/
/*****************************************************************************/
#ifndef __dng_fingerprint__
#define __dng_fingerprint__
/*****************************************************************************/
#include "dng_exceptions.h"
#include "dng_types.h"
#include "dng_stream.h"
#include <cstring>
/*****************************************************************************/
/// \brief Container fingerprint (MD5 only at present).
class dng_fingerprint
{
public:
static const size_t kDNGFingerprintSize = 16;
uint8 data [kDNGFingerprintSize];
public:
dng_fingerprint ();
/// Check if fingerprint is all zeros.
bool IsNull () const;
/// Same as IsNull but expresses intention of testing validity.
bool IsValid () const
{
return !IsNull ();
}
/// Set to all zeros, a value used to indicate an invalid fingerprint.
void Clear ()
{
*this = dng_fingerprint ();
}
/// Test if two fingerprints are equal.
bool operator== (const dng_fingerprint &print) const;
/// Test if two fingerprints are not equal.
bool operator!= (const dng_fingerprint &print) const
{
return !(*this == print);
}
/// Produce a 32-bit hash value from fingerprint used for faster hashing of
/// fingerprints.
uint32 Collapse32 () const;
/// Convert fingerprint to UTF-8 string.
///
/// \param resultStr The output array to which the UTF-8 encoding of the
/// fingerprint will be written.
void ToUtf8HexString (char resultStr [2 * kDNGFingerprintSize + 1]) const;
/// Convert UTF-8 string to fingerprint. Returns true on success, false on
/// failure.
///
/// \param inputStr The input array from which the UTF-8 encoding of the
/// fingerprint will be read.
///
/// \retval True indicates success.
bool FromUtf8HexString (const char inputStr [2 * kDNGFingerprintSize + 1]);
};
/*****************************************************************************/
/// \brief Utility to compare fingerprints (e.g., for sorting).
struct dng_fingerprint_less_than
{
/// Less-than comparison.
bool operator() (const dng_fingerprint &a,
const dng_fingerprint &b) const
{
return memcmp (a.data,
b.data,
sizeof (a.data)) < 0;
}
};
/******************************************************************************/
// Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
// rights reserved.
//
// License to copy and use this software is granted provided that it
// is identified as the "RSA Data Security, Inc. MD5 Message-Digest
// Algorithm" in all material mentioning or referencing this software
// or this function.
//
// License is also granted to make and use derivative works provided
// that such works are identified as "derived from the RSA Data
// Security, Inc. MD5 Message-Digest Algorithm" in all material
// mentioning or referencing the derived work.
//
// RSA Data Security, Inc. makes no representations concerning either
// the merchantability of this software or the suitability of this
// software for any particular purpose. It is provided "as is"
// without express or implied warranty of any kind.
//
// These notices must be retained in any copies of any part of this
// documentation and/or software.
/// \brief Class to hash binary data to a fingerprint using the MD5 Message-Digest
/// Algorithm.
class dng_md5_printer
{
public:
dng_md5_printer ();
virtual ~dng_md5_printer ()
{
}
/// Reset the fingerprint.
void Reset ();
/// Append the data to the stream to be hashed.
/// \param data The data to be hashed.
/// \param inputLen The length of data, in bytes.
void Process (const void *data,
uint32 inputLen);
/// Append the string to the stream to be hashed.
/// \param text The string to be hashed.
void Process (const char *text)
{
Process (text, (uint32) strlen (text));
}
/// Get the fingerprint (i.e., result of the hash).
const dng_fingerprint & Result ();
private:
static void Encode (uint8 *output,
const uint32 *input,
uint32 len);
static void Decode (uint32 *output,
const uint8 *input,
uint32 len);
// F, G, H and I are basic MD5 functions.
static inline uint32 F (uint32 x,
uint32 y,
uint32 z)
{
return (x & y) | (~x & z);
}
static inline uint32 G (uint32 x,
uint32 y,
uint32 z)
{
return (x & z) | (y & ~z);
}
static inline uint32 H (uint32 x,
uint32 y,
uint32 z)
{
return x ^ y ^ z;
}
static inline uint32 I (uint32 x,
uint32 y,
uint32 z)
{
return y ^ (x | ~z);
}
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
static inline void FF (uint32 &a,
uint32 b,
uint32 c,
uint32 d,
uint32 x,
uint32 s,
uint32 ac)
{
a += F (b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
}
static inline void GG (uint32 &a,
uint32 b,
uint32 c,
uint32 d,
uint32 x,
uint32 s,
uint32 ac)
{
a += G (b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
}
static inline void HH (uint32 &a,
uint32 b,
uint32 c,
uint32 d,
uint32 x,
uint32 s,
uint32 ac)
{
a += H (b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
}
static inline void II (uint32 &a,
uint32 b,
uint32 c,
uint32 d,
uint32 x,
uint32 s,
uint32 ac)
{
a += I (b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
}
static void MD5Transform (uint32 state [4],
const uint8 block [64]);
private:
uint32 state [4];
uint32 count [2];
uint8 buffer [64];
bool final;
dng_fingerprint result;
};
/*****************************************************************************/
/// \brief A dng_stream based interface to the MD5 printing logic.
class dng_md5_printer_stream : public dng_stream, dng_md5_printer
{
private:
uint64 fNextOffset;
public:
/// Create an empty MD5 printer stream.
dng_md5_printer_stream ()
: fNextOffset (0)
{
}
virtual uint64 DoGetLength ()
{
return fNextOffset;
}
virtual void DoRead (void * /* data */,
uint32 /* count */,
uint64 /* offset */)
{
ThrowProgramError ();
}
virtual void DoSetLength (uint64 length)
{
if (length != fNextOffset)
{
ThrowProgramError ();
}
}
virtual void DoWrite (const void *data,
uint32 count2,
uint64 offset)
{
if (offset != fNextOffset)
{
ThrowProgramError ();
}
Process (data, count2);
fNextOffset += count2;
}
const dng_fingerprint & Result ()
{
Flush ();
return dng_md5_printer::Result ();
}
};
/*****************************************************************************/
#endif
/*****************************************************************************/
|