-
Notifications
You must be signed in to change notification settings - Fork 2
/
md5.h
165 lines (130 loc) · 3.99 KB
/
md5.h
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
// This file is part of par2cmdline (a PAR 2.0 compatible file verification and
// repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
//
// Copyright (c) 2003 Peter Brian Clements
//
// par2cmdline is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// par2cmdline is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef __MD5_H__
#define __MD5_H__
// This file defines the MD5Hash and MD5Context objects which are used
// to compute and manipulate the MD5 Hash values for a block of data.
// Usage:
//
// MD5Context context;
// context.Update(buffer, length);
//
// MD5Hash hash;
// context.Final(hash);
// MD5 Hash value
class MD5Hash
{
public:
// removed this to stop GCC warnings about using the 'pack' attribute on
// non-POD members in classes such as PACKET_HEADER:
// Constructor does not initialise the value
//MD5Hash(void) {};
// Comparison operators
bool operator==(const MD5Hash &other) const;
bool operator!=(const MD5Hash &other) const;
bool operator<(const MD5Hash &other) const;
bool operator>=(const MD5Hash &other) const;
bool operator>(const MD5Hash &other) const;
bool operator<=(const MD5Hash &other) const;
// Convert value to hex
friend ostream& operator<<(ostream &s, const MD5Hash &hash);
string print(void) const;
// removed these to stop GCC warnings about using the 'pack' attribute on
// non-POD members in classes such as PACKET_HEADER:
// Copy and assignment
//MD5Hash(const MD5Hash &other);
//MD5Hash& operator=(const MD5Hash &other);
public:
u8 hash[16]; // 16 byte MD5 Hash value
};
// Intermediate computation state
class MD5State
{
public:
MD5State(void);
void Reset(void);
public:
void UpdateState(const u32 (&block)[16]);
protected:
u32 state[4]; // 16 byte MD5 computation state
};
// MD5 computation context with 64 byte buffer
class MD5Context : public MD5State
{
public:
MD5Context(void);
~MD5Context(void) {};
void Reset(void);
// Process data from a buffer
void Update(const void *buffer, size_t length);
// Process 0 bytes
void Update(size_t length);
// Compute the final hash value
void Final(MD5Hash &output);
// Get the Hash value and the total number of bytes processed.
MD5Hash Hash(void) const;
u64 Bytes(void) const {return bytes;}
friend ostream& operator<<(ostream &s, const MD5Context &context);
string print(void) const;
protected:
enum {buffersize = 64};
unsigned char block[buffersize];
size_t used;
u64 bytes;
};
// Compare hash values
inline bool MD5Hash::operator==(const MD5Hash &other) const
{
return (0==memcmp(&hash, &other.hash, sizeof(hash)));
}
inline bool MD5Hash::operator!=(const MD5Hash &other) const
{
return !operator==(other);
}
inline bool MD5Hash::operator<(const MD5Hash &other) const
{
size_t index = 15;
while (index > 0 && hash[index] == other.hash[index])
{
index--;
}
return hash[index] < other.hash[index];
}
inline bool MD5Hash::operator>=(const MD5Hash &other) const
{
return !operator<(other);
}
inline bool MD5Hash::operator>(const MD5Hash &other) const
{
return other.operator<(*this);
}
inline bool MD5Hash::operator<=(const MD5Hash &other) const
{
return !other.operator<(*this);
}
/*inline MD5Hash::MD5Hash(const MD5Hash &other)
{
memcpy(&hash, &other.hash, sizeof(hash));
}
inline MD5Hash& MD5Hash::operator=(const MD5Hash &other)
{
memcpy(&hash, &other.hash, sizeof(hash));
return *this;
}*/
#endif // __MD5_H__