AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MD5Checksum.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 /*
18 This source code has been taken and modified by Algoryx Simulation AB
19 from the source and under the license given below.
20 */
21 
22 /*****************************************************************************************
23 
24 *** MD5Checksum.h: interface for the MD5Checksum class.
25 
26 *** Developed by Langfine Ltd.
27 *** Released to the public domain 12/Nov/2001.
28 *** Please visit our website www.langfine.com
29 
30 *** Any modifications must be clearly commented to distinguish them from Langfine's
31 *** original source code. Please advise Langfine of useful modifications so that we
32 *** can make them generally available.
33 
34 *****************************************************************************************/
35 
36 
37  /****************************************************************************************
38  This software is derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
39  Incorporation of this statement is a condition of use; please see the RSA
40  Data Security Inc copyright notice below:-
41 
42  Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
43  rights reserved.
44 
45  RSA Data Security, Inc. makes no representations concerning either
46  the merchantability of this software or the suitability of this
47  software for any particular purpose. It is provided "as is"
48  without express or implied warranty of any kind.
49 
50  These notices must be retained in any copies of any part of this
51  documentation and/or software.
52 
53  Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
54  rights reserved.
55  License to copy and use this software is granted provided that it
56  is identified as the "RSA Data Security, Inc. MD5 Message-Digest
57  Algorithm" in all material mentioning or referencing this software
58  or this function.
59  License is also granted to make and use derivative works provided
60  that such works are identified as "derived from the RSA Data
61  Security, Inc. MD5 Message-Digest Algorithm" in all material
62  mentioning or referencing the derived work.
63  RSA Data Security, Inc. makes no representations concerning either
64  the merchantability of this software or the suitability of this
65  software for any particular purpose. It is provided "as is"
66  without express or implied warranty of any kind.
67 
68  These notices must be retained in any copies of any part of this
69  documentation and/or software.
70  *****************************************************************************************/
71 
72  /****************************************************************************************
73  This implementation of the RSA MD5 Algorithm was written by Langfine Ltd.
74 
75  Langfine Ltd makes no representations concerning either
76  the merchantability of this software or the suitability of this
77  software for any particular purpose. It is provided "as is"
78  without express or implied warranty of any kind.
79 
80  In addition to the above, Langfine make no warrant or assurances regarding the
81  accuracy of this implementation of the MD5 checksum algorithm nor any assurances regarding
82  its suitability for any purposes.
83 
84  This implementation may be used freely provided that Langfine is credited
85  in a copyright or similar notices (eg, RSA MD5 Algorithm implemented by Langfine
86  Ltd.) and provided that the RSA Data Security notices are complied with.
87 
88  Langfine may be contacted at mail@langfine.com
89  */
90 
91  /*****************************************************************************************
92  CLASS: CMD5Checksum
93  DESCRIPTION: Implements the "RSA Data Security, Inc. MD5 Message-Digest Algorithm".
94  NOTES: Calculates the RSA MD5 checksum for a file or contiguous array of data.
95 
96  Below are extracts from a memo on The MD5 Message-Digest Algorithm by R. Rivest of MIT
97  Laboratory for Computer Science and RSA Data Security, Inc., April 1992.
98 
99  1. Executive Summary
100  This document describes the MD5 message-digest algorithm. The
101  algorithm takes as input a message of arbitrary length and produces
102  as output a 128-bit "fingerprint" or "message digest" of the input.
103  It is conjectured that it is computationally infeasible to produce
104  two messages having the same message digest, or to produce any
105  message having a given prespecified target message digest. The MD5
106  algorithm is intended for digital signature applications, where a
107  large file must be "compressed" in a secure manner before being
108  encrypted with a private (secret) key under a public-key cryptosystem
109  such as RSA.
110 
111  The MD5 algorithm is designed to be quite fast on 32-bit machines. In
112  addition, the MD5 algorithm does not require any large substitution
113  tables; the algorithm can be coded quite compactly.
114  The MD5 algorithm is an extension of the MD4 message-digest algorithm
115  1,2]. MD5 is slightly slower than MD4, but is more "conservative" in
116  design. MD5 was designed because it was felt that MD4 was perhaps
117  being adopted for use more quickly than justified by the existing
118  critical review; because MD4 was designed to be exceptionally fast,
119  it is "at the edge" in terms of risking successful cryptanalytic
120  attack. MD5 backs off a bit, giving up a little in speed for a much
121  greater likelihood of ultimate security. It incorporates some
122  suggestions made by various reviewers, and contains additional
123  optimizations. The MD5 algorithm is being placed in the public domain
124  for review and possible adoption as a standard.
125 
126 
127  2. Terminology and Notation
128  In this document a "word" is a 32-bit quantity and a "byte" is an
129  eight-bit quantity. A sequence of bits can be interpreted in a
130  natural manner as a sequence of bytes, where each consecutive group
131  of eight bits is interpreted as a byte with the high-order (most
132  significant) bit of each byte listed first. Similarly, a sequence of
133  bytes can be interpreted as a sequence of 32-bit words, where each
134  consecutive group of four bytes is interpreted as a word with the
135  low-order (least significant) byte given first.
136  Let x_i denote "x sub i". If the subscript is an expression, we
137  surround it in braces, as in x_{i+1}. Similarly, we use ^ for
138  superscripts (exponentiation), so that x^i denotes x to the i-th power.
139  Let the symbol "+" denote addition of words (i.e., modulo-2^32
140  addition). Let X <<< s denote the 32-bit value obtained by circularly
141  shifting (rotating) X left by s bit positions. Let not(X) denote the
142  bit-wise complement of X, and let X v Y denote the bit-wise OR of X
143  and Y. Let X xor Y denote the bit-wise XOR of X and Y, and let XY
144  denote the bit-wise AND of X and Y.
145 
146 
147  3. MD5 Algorithm Description
148  We begin by supposing that we have a b-bit message as input, and that
149  we wish to find its message digest. Here b is an arbitrary
150  nonnegative integer; b may be zero, it need not be a multiple of
151  eight, and it may be arbitrarily large. We imagine the bits of the
152  message written down as follows: m_0 m_1 ... m_{b-1}
153  The following five steps are performed to compute the message digest
154  of the message.
155 
156  3.1 Step 1. Append Padding Bits
157  The message is "padded" (extended) so that its length (in bits) is
158  congruent to 448, modulo 512. That is, the message is extended so
159  that it is just 64 bits shy of being a multiple of 512 bits long.
160  Padding is always performed, even if the length of the message is
161  already congruent to 448, modulo 512.
162  Padding is performed as follows: a single "1" bit is appended to the
163  message, and then "0" bits are appended so that the length in bits of
164  the padded message becomes congruent to 448, modulo 512. In all, at
165  least one bit and at most 512 bits are appended.
166 
167  3.2 Step 2. Append Length
168  A 64-bit representation of b (the length of the message before the
169  padding bits were added) is appended to the result of the previous
170  step. In the unlikely event that b is greater than 2^64, then only
171  the low-order 64 bits of b are used. (These bits are appended as two
172  32-bit words and appended low-order word first in accordance with the
173  previous conventions.)
174  At this point the resulting message (after padding with bits and with
175  b) has a length that is an exact multiple of 512 bits. Equivalently,
176  this message has a length that is an exact multiple of 16 (32-bit)
177  words. Let M[0 ... N-1] denote the words of the resulting message,
178  where N is a multiple of 16.
179 
180  3.3 Step 3. Initialize MD Buffer
181  A four-word buffer (A,B,C,D) is used to compute the message digest.
182  Here each of A, B, C, D is a 32-bit register. These registers are
183  initialized to the following values in hexadecimal, low-order bytes first):
184  word A: 01 23 45 67 word B: 89 ab cd ef
185  word C: fe dc ba 98 word D: 76 54 32 10
186 
187  3.4 Step 4. Process Message in 16-Word Blocks
188  We first define four auxiliary functions that each take as input
189  three 32-bit words and produce as output one 32-bit word.
190  F(X,Y,Z) = XY v not(X) Z G(X,Y,Z) = XZ v Y not(Z)
191  H(X,Y,Z) = X xor Y xor Z I(X,Y,Z) = Y xor (X v not(Z))
192  In each bit position F acts as a conditional: if X then Y else Z.
193  The function F could have been defined using + instead of v since XY
194  and not(X)Z will never have 1's in the same bit position.) It is
195  interesting to note that if the bits of X, Y, and Z are independent
196  and unbiased, the each bit of F(X,Y,Z) will be independent and unbiased.
197  The functions G, H, and I are similar to the function F, in that they
198  act in "bitwise parallel" to produce their output from the bits of X,
199  Y, and Z, in such a manner that if the corresponding bits of X, Y,
200  and Z are independent and unbiased, then each bit of G(X,Y,Z),
201  H(X,Y,Z), and I(X,Y,Z) will be independent and unbiased. Note that
202  the function H is the bit-wise "xor" or "parity" function of its inputs.
203  This step uses a 64-element table T[1 ... 64] constructed from the
204  sine function. Let T[i] denote the i-th element of the table, which
205  is equal to the integer part of 4294967296 times abs(sin(i)), where i
206  is in radians. The elements of the table are given in the appendix.
207  Do the following:
208 
209  //Process each 16-word block.
210  For i = 0 to N/16-1 do // Copy block i into X.
211  For j = 0 to 15 do
212  Set X[j] to M[i*16+j].
213  end //of loop on j
214 
215  // Save A as AA, B as BB, C as CC, and D as DD.
216  AA = A BB = B
217  CC = C DD = D
218 
219  // Round 1.
220  // Let [abcd k s i] denote the operation
221  // a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s).
222  // Do the following 16 operations.
223  [ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4]
224  [ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8]
225  [ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12]
226  [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16]
227 
228  // Round 2.
229  // Let [abcd k s i] denote the operation
230  // a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s).
231  // Do the following 16 operations.
232  [ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20]
233  [ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24]
234  [ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28]
235  [ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32]
236 
237  // Round 3.
238  // Let [abcd k s t] denote the operation
239  // a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s).
240  // Do the following 16 operations.
241  [ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36]
242  [ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40]
243  [ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44]
244  [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48]
245 
246  // Round 4.
247  // Let [abcd k s t] denote the operation
248  // a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s).
249  // Do the following 16 operations.
250  [ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52]
251  [ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56]
252  [ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60]
253  [ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64]
254 
255  // Then perform the following additions. (That is increment each
256  // of the four registers by the value it had before this block
257  // was started.)
258  A = A + AA B = B + BB C = C + CC D = D + DD
259 
260  end // of loop on i
261 
262  3.5 Step 5. Output
263  The message digest produced as output is A, B, C, D. That is, we
264  begin with the low-order byte of A, and end with the high-order byte of D.
265  This completes the description of MD5.
266 
267  Summary
268  The MD5 message-digest algorithm is simple to implement, and provides
269  a "fingerprint" or message digest of a message of arbitrary length.
270  It is conjectured that the difficulty of coming up with two messages
271  having the same message digest is on the order of 2^64 operations,
272  and that the difficulty of coming up with any message having a given
273  message digest is on the order of 2^128 operations. The MD5 algorithm
274  has been carefully scrutinized for weaknesses. It is, however, a
275  relatively new algorithm and further security analysis is of course
276  justified, as is the case with any new proposal of this sort.
277 
278 
279  5. Differences Between MD4 and MD5
280  The following are the differences between MD4 and MD5:
281  1. A fourth round has been added.
282  2. Each step now has a unique additive constant.
283  3. The function g in round 2 was changed from (XY v XZ v YZ) to
284  (XZ v Y not(Z)) to make g less symmetric.
285  4. Each step now adds in the result of the previous step. This
286  promotes a faster "avalanche effect".
287  5. The order in which input words are accessed in rounds 2 and
288  3 is changed, to make these patterns less like each other.
289  6. The shift amounts in each round have been approximately
290  optimized, to yield a faster "avalanche effect." The shifts in
291  different rounds are distinct.
292 
293  References
294  [1] Rivest, R., "The MD4 Message Digest Algorithm", RFC 1320, MIT and
295  RSA Data Security, Inc., April 1992.
296  [2] Rivest, R., "The MD4 message digest algorithm", in A.J. Menezes
297  and S.A. Vanstone, editors, Advances in Cryptology - CRYPTO '90
298  Proceedings, pages 303-311, Springer-Verlag, 1991.
299  [3] CCITT Recommendation X.509 (1988), "The Directory -
300  Authentication Framework."APPENDIX A - Reference Implementation
301 
302 
303  The level of security discussed in this memo is considered to be
304  sufficient for implementing very high security hybrid digital-
305  signature schemes based on MD5 and a public-key cryptosystem.
306  Author's Address
307  Ronald L. Rivest Massachusetts Institute of Technology
308  Laboratory for Computer Science NE43-324 545 Technology Square
309  Cambridge, MA 02139-1986 Phone: (617) 253-5880
310  EMail: rivest@theory.lcs.mit.edu
311 
312 
313  *****************************************************************************************/
314 #pragma once
315 
316 
317 #include <agx/agxPhysics_export.h>
318 
319 #include <fstream>
320 #include <agx/stdint.h>
321 
322 namespace agxStream {
323 
325  {
326  public:
332  static std::string getMD5(const char* pBuf, size_t numBytes);
333 
339  static std::string getMD5(std::istream& stream);
340 
346  static std::string getMD5(const std::string& strFilePath);
347 
353  static std::string getMD5(std::istream& stream, size_t numBytes);
354 
355  //constructor/destructor
356  MD5Checksum();
357  virtual ~MD5Checksum() {}
358 
359  protected:
360 
361  //RSA MD5 implementation
362  void transform(const char Block[64]);
363  void update(const char* Input, size_t nInputLen);
364  std::string final();
365  inline uint32_t rotateLeft(uint32_t x, int n);
366  inline void FF( uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t X, uint32_t S, uint32_t T);
367  inline void GG( uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t X, uint32_t S, uint32_t T);
368  inline void HH( uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t X, uint32_t S, uint32_t T);
369  inline void II( uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t X, uint32_t S, uint32_t T);
370 
371  //utility functions
372  inline void dWordToByte(char* Output, uint32_t* Input, uint32_t nLength);
373  inline void byteToDWord(uint32_t* Output, const char* Input, uint32_t nLength);
374 
375  private:
376  char m_lpszBuffer[64]; //input buffer
377  uint32_t m_nCount[2]; //number of bits, modulo 2^64 (lsb first)
378  uint32_t m_lMD5[4]; //MD5 checksum
379  };
380 
381 } // namespace agxStream
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
#define AGXPHYSICS_EXPORT