forked from dlidstrom/Duplo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHashUtil.h
executable file
·131 lines (116 loc) · 4.9 KB
/
HashUtil.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
/** \class HashUtil
* HashUtil (MD5 hashing)
*
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
* rights reserved.
*
* License to copy and use this software is granted provided that it
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
* Algorithm" in all material mentioning or referencing this software
* or this function.
*
* License is also granted to make and use derivative works provided
* that such works are identified as "derived from the RSA Data
* Security, Inc. MD5 Message-Digest Algorithm" in all material
* mentioning or referencing the derived work.
*
* RSA Data Security, Inc. makes no representations concerning either
* the merchantability of this software or the suitability of this
* software for any particular purpose. It is provided "as is"
* without express or implied warranty of any kind.
*
* These notices must be retained in any copies of any part of this
* documentation and/or software.
*
* @author RSA Data Security, Inc. (http://www.rsasecurity.com)
* @date 21/08/04
* @see RFC1321
*/
#ifndef _HASHUTIL_H_
#define _HASHUTIL_H_
#include <array>
class HashUtil{
private:
static const unsigned int S11=7;
static const unsigned int S12=12;
static const unsigned int S13=17;
static const unsigned int S14=22;
static const unsigned int S21=5;
static const unsigned int S22=9;
static const unsigned int S23=14;
static const unsigned int S24=20;
static const unsigned int S31=4;
static const unsigned int S32=11;
static const unsigned int S33=16;
static const unsigned int S34=23;
static const unsigned int S41=6;
static const unsigned int S42=10;
static const unsigned int S43=15;
static const unsigned int S44=21;
// F, G, H and I are basic MD5 functions.
static inline unsigned int F(unsigned int x, unsigned int y, unsigned int z){
return (((x) & (y)) | ((~x) & (z)));
}
static inline unsigned int G(unsigned int x, unsigned int y, unsigned int z){
return (((x) & (z)) | ((y) & (~z)));
}
static inline unsigned int H(unsigned int x, unsigned int y, unsigned int z){
return ((x) ^ (y) ^ (z));
}
static inline unsigned int I(unsigned int x, unsigned int y, unsigned int z){
return ((y) ^ ((x) | (~z)));
}
static inline unsigned int ROTATE_LEFT(unsigned int x, unsigned int n){
return (((x) << (n)) | ((x) >> (32-(n))));
}
// FF,GG,HH, and II transformations for rounds 1,2,3, and 4. Rotation is separate from addition to prevent recomputation.
static inline void FF(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s, unsigned int ac){
a += F(b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a+=b;
}
static inline void GG(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s, unsigned int ac){
a += G (b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a+=b;
}
static inline void HH(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s, unsigned int ac){
a += H (b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a+=b;
}
static inline void II(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s, unsigned int ac){
a += I (b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a+=b;
}
typedef struct {
unsigned int state[4];
unsigned int count[2];
unsigned char buffer[64];
} MD5_CTX;
static unsigned char m_PADDING[64];
static void MD5Init(MD5_CTX *context);
static void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen);
static void MD5Final(std::array<unsigned char, 16> & digest, MD5_CTX *context);
static void MD5Transform(unsigned int state[4], unsigned char block[64]);
/**
* Encodes input (unsigned int) into output (unsigned char). Assumes len is a multiple of 4.
*/
template <unsigned int nbits>
static void MD5_Encode( std::array<unsigned char, nbits> & output, unsigned int * input )
{
for(unsigned int i = 0, j = 0; j < nbits; i++, j += 4){
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
};
static void MD5_Decode(unsigned int *, unsigned char *, unsigned int);
static void MD5_memcpy(unsigned char*, unsigned char*, unsigned int);
static void MD5_memset(unsigned char*, int, unsigned int);
public:
static void getMD5Sum(unsigned char* pData, int size, std::array<unsigned char, 16> & Digest );
};
#endif