-
Notifications
You must be signed in to change notification settings - Fork 0
/
MD5.h
executable file
·107 lines (88 loc) · 2.62 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
#ifndef MD5_H
#define MD5_H
// Code adapted from http://sourceforge.net/projects/hashlib2plus
#include <string>
#include <stdio.h>
using namespace std;
// Define useful typedef, and padding array
typedef unsigned char *POINTER;
static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0 };
// Define constants for md5
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
// Define functions required for MD5
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
#define ROTATE_LEFT(x, n) (((x) << (n)) | (( (unsigned int) x) >> (32-(n))))
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
// Define struct to contain MD5 context
typedef struct {
// state (ABCD)
unsigned long int state[4];
// number of bits, modulo 2^64 (lsb first)
unsigned long int count[2];
// input buffer
unsigned char buffer[64];
} MD5_CTX;
class MD5 {
private:
MD5_CTX ctx;
string hashIt(void);
string convToString(unsigned char *data);
void updateContext(unsigned char *data, unsigned int len);
void resetContext(void);
void MD5Transform(unsigned long int state[4], unsigned char block[64]);
void Encode(unsigned char* output, unsigned long int *input,
unsigned int len);
void Decode(unsigned long int *output, unsigned char *input,
unsigned int len);
void MD5_memcpy(POINTER output, POINTER input, unsigned int len);
void MD5_memset(POINTER output, int value, unsigned int len);
public:
MD5();
~MD5();
string getHashFromFile(string filename);
void MD5Init(MD5_CTX* context);
void MD5Update(MD5_CTX* context, unsigned char *input,
unsigned int inputLen);
void MD5Final(unsigned char digest[16], MD5_CTX* context);
};
#endif