libsidplayfp 2.15.0
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 <stdint.h>
36
37#include "MD5_Defs.h"
38
39typedef uint8_t md5_byte_t;
40typedef uint32_t md5_word_t;
41
42class MD5
43{
44 public:
45 // Initialize the algorithm. Reset starting values.
46 MD5();
47
48 // Append a string to the message.
49 void append(const void* data, int nbytes);
50
51 // Finish the message.
52 void finish();
53
54 // Return pointer to 16-byte fingerprint.
55 const md5_byte_t* getDigest();
56
57 // Initialize the algorithm. Reset starting values.
58 void reset();
59
60 private:
61
62 /* Define the state of the MD5 Algorithm. */
63 md5_word_t count[2]; /* message length in bits, lsw first */
64 md5_word_t abcd[4]; /* digest buffer */
65 md5_byte_t buf[64]; /* accumulate block */
66
67 md5_byte_t digest[16];
68
69 md5_word_t tmpBuf[16];
70 const md5_word_t* X;
71
72 void
73 process(const md5_byte_t data[64]);
74
75 md5_word_t
76 ROTATE_LEFT(const md5_word_t x, const int n);
77
78 md5_word_t
79 F(const md5_word_t x, const md5_word_t y, const md5_word_t z);
80
81 md5_word_t
82 G(const md5_word_t x, const md5_word_t y, const md5_word_t z);
83
84 md5_word_t
85 H(const md5_word_t x, const md5_word_t y, const md5_word_t z);
86
87 md5_word_t
88 I(const md5_word_t x, const md5_word_t y, const md5_word_t z);
89
90 typedef md5_word_t (MD5::*md5func)(const md5_word_t x, const md5_word_t y, const md5_word_t z);
91
92 void
93 SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
94 md5_word_t& d, const int k, const int s,
95 const md5_word_t Ti);
96};
97
98inline md5_word_t
99MD5::ROTATE_LEFT(const md5_word_t x, const int n)
100{
101 return ( (x<<n) | (x>>(32-n)) );
102}
103
104inline md5_word_t
105MD5::F(const md5_word_t x, const md5_word_t y, const md5_word_t z)
106{
107 return ( (x&y) | (~x&z) );
108}
109
110inline md5_word_t
111MD5::G(const md5_word_t x, const md5_word_t y, const md5_word_t z)
112{
113 return ( (x&z) | (y&~z) );
114}
115
116inline md5_word_t
117MD5::H(const md5_word_t x, const md5_word_t y, const md5_word_t z)
118{
119 return ( x^y^z );
120}
121
122inline md5_word_t
123MD5::I(const md5_word_t x, const md5_word_t y, const md5_word_t z)
124{
125 return ( y ^ (x|~z) );
126}
127
128inline void
129MD5::SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
130 md5_word_t& d, const int k, const int s,
131 const md5_word_t Ti)
132{
133 md5_word_t t = a + (this->*func)(b,c,d) + X[k] + Ti;
134 a = ROTATE_LEFT(t, s) + b;
135}
136
137#endif /* MD5_H */
Definition MD5.h:43