libsidplayfp  0.3.5
MD5.h
1 /*
2  * This code has been derived by Michael Schwendt <mschwendt@yahoo.com>
3  * from original work by L. Peter Deutsch <ghost@aladdin.com>.
4  *
5  * The original C code (md5.c, md5.h) is available here:
6  * ftp://ftp.cs.wisc.edu/ghost/packages/md5.tar.gz
7  */
8 
9 /*
10  Copyright (C) 1999 Aladdin Enterprises. All rights reserved.
11 
12  This software is provided 'as-is', without any express or implied
13  warranty. In no event will the authors be held liable for any damages
14  arising from the use of this software.
15 
16  Permission is granted to anyone to use this software for any purpose,
17  including commercial applications, and to alter it and redistribute it
18  freely, subject to the following restrictions:
19 
20  1. The origin of this software must not be misrepresented; you must not
21  claim that you wrote the original software. If you use this software
22  in a product, an acknowledgment in the product documentation would be
23  appreciated but is not required.
24  2. Altered source versions must be plainly marked as such, and must not be
25  misrepresented as being the original software.
26  3. This notice may not be removed or altered from any source distribution.
27 
28  L. Peter Deutsch
29  ghost@aladdin.com
30  */
31 
32 #ifndef MD5_H
33 #define MD5_H
34 
35 #include "MD5_Defs.h"
36 
37 typedef unsigned char md5_byte_t; // 8-bit byte
38 typedef unsigned int md5_word_t; // 32-bit word
39 
40 class MD5
41 {
42  public:
43  // Initialize the algorithm. Reset starting values.
44  MD5();
45 
46  // Append a string to the message.
47  void append(const void* data, int nbytes);
48 
49  // Finish the message.
50  void finish();
51 
52  // Return pointer to 16-byte fingerprint.
53  const md5_byte_t* getDigest();
54 
55  // Initialize the algorithm. Reset starting values.
56  void reset();
57 
58  private:
59 
60  /* Define the state of the MD5 Algorithm. */
61  md5_word_t count[2]; /* message length in bits, lsw first */
62  md5_word_t abcd[4]; /* digest buffer */
63  md5_byte_t buf[64]; /* accumulate block */
64 
65  md5_byte_t digest[16];
66 
67  md5_word_t tmpBuf[16];
68  const md5_word_t* X;
69 
70  void
71  process(const md5_byte_t data[64]);
72 
73  md5_word_t
74  ROTATE_LEFT(const md5_word_t x, const int n);
75 
76  md5_word_t
77  F(const md5_word_t x, const md5_word_t y, const md5_word_t z);
78 
79  md5_word_t
80  G(const md5_word_t x, const md5_word_t y, const md5_word_t z);
81 
82  md5_word_t
83  H(const md5_word_t x, const md5_word_t y, const md5_word_t z);
84 
85  md5_word_t
86  I(const md5_word_t x, const md5_word_t y, const md5_word_t z);
87 
88  typedef md5_word_t (MD5::*md5func)(const md5_word_t x, const md5_word_t y, const md5_word_t z);
89 
90  void
91  SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
92  md5_word_t& d, const int k, const int s,
93  const md5_word_t Ti);
94 };
95 
96 inline md5_word_t
97 MD5::ROTATE_LEFT(const md5_word_t x, const int n)
98 {
99  return ( (x<<n) | (x>>(32-n)) );
100 }
101 
102 inline md5_word_t
103 MD5::F(const md5_word_t x, const md5_word_t y, const md5_word_t z)
104 {
105  return ( (x&y) | (~x&z) );
106 }
107 
108 inline md5_word_t
109 MD5::G(const md5_word_t x, const md5_word_t y, const md5_word_t z)
110 {
111  return ( (x&z) | (y&~z) );
112 }
113 
114 inline md5_word_t
115 MD5::H(const md5_word_t x, const md5_word_t y, const md5_word_t z)
116 {
117  return ( x^y^z );
118 }
119 
120 inline md5_word_t
121 MD5::I(const md5_word_t x, const md5_word_t y, const md5_word_t z)
122 {
123  return ( y ^ (x|~z) );
124 }
125 
126 inline void
127 MD5::SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
128  md5_word_t& d, const int k, const int s,
129  const md5_word_t Ti)
130 {
131  md5_word_t t = a + (this->*func)(b,c,d) + X[k] + Ti;
132  a = ROTATE_LEFT(t, s) + b;
133 }
134 
135 #endif /* MD5_H */