forked from Parchive/par2cmdline
-
Notifications
You must be signed in to change notification settings - Fork 2
/
md5.h
153 lines (122 loc) · 3.55 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
// 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__
#ifdef WIN32
#pragma pack(push, 1)
#define PACKED
#else
#define PACKED __attribute__ ((packed))
#endif
// 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
struct MD5Hash;
ostream& operator<<(ostream &s, const MD5Hash &hash);
struct MD5Hash
{
// 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;
u8 hash[16]; // 16 byte MD5 Hash value
} PACKED;
// 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);
}
#ifdef WIN32
#pragma pack(pop)
#endif
#undef PACKED
#endif // __MD5_H__