Menu

[r513]: / phprpc_3.0 / dotNET / MD5.cs  Maximize  Restore  History

Download this file

181 lines (176 with data), 9.5 kB

  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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| MD5.cs |
| |
| Release 3.0.1 |
| Copyright (c) 2005-2008 by Team-PHPRPC |
| |
| WebSite: http://www.phprpc.org/ |
| http://www.phprpc.net/ |
| http://www.phprpc.com/ |
| http://sourceforge.net/projects/php-rpc/ |
| |
| Authors: Ma Bingyao <andot@ujn.edu.cn> |
| |
| This file may be distributed and/or modified under the |
| terms of the GNU Lesser General Public License (LGPL) |
| version 3.0 as published by the Free Software Foundation |
| and appearing in the included file LICENSE. |
| |
\**********************************************************/
/* MD5 class.
*
* Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>
* Version: 3.0
* LastModified: Feb 17, 2008
* This library is free. You can redistribute it and/or modify it.
*/
namespace org.phprpc.util {
public sealed class MD5 {
private MD5() {
}
private static uint bitrol(uint n, byte c) {
return (n << c) | (n >> (32 - c));
}
private static uint cmn(uint q, uint a, uint b, uint x, byte s, uint t) {
return bitrol(a + q + x + t, s) + b;
}
private static uint ff(uint a, uint b, uint c, uint d, uint x, byte s, uint t) {
return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
private static uint gg(uint a, uint b, uint c, uint d, uint x, byte s, uint t) {
return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
private static uint hh(uint a, uint b, uint c, uint d, uint x, byte s, uint t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
}
private static uint ii(uint a, uint b, uint c, uint d, uint x, byte s, uint t) {
return cmn(c ^ (b | (~d)), a, b, x, s, t);
}
private static uint[] unpack(byte[] data) {
int length = data.Length;
int count = ((length + 72) >> 6) << 4;
uint[] x = new uint[count];
for (int i = 0; i < length; i++) {
x[i >> 2] |= (uint)data[i] << ((i & 3) << 3);
}
x[length >> 2] |= (uint)(0x80) << ((length & 3) << 3);
ulong bitlen = ((ulong)length) << 3;
x[count - 2] = (uint)(bitlen & 0xffffffff);
x[count - 1] = (uint)(bitlen >> 32);
return x;
}
public static byte[] Hash(byte[] data) {
uint[] x = unpack(data);
uint a = 0x67452301;
uint b = 0xefcdab89;
uint c = 0x98badcfe;
uint d = 0x10325476;
for (int i = 0, count = x.Length; i < count; i += 16) {
uint olda = a;
uint oldb = b;
uint oldc = c;
uint oldd = d;
a = ff(a, b, c, d, x[i + 0], 7, 0xd76aa478); /* 1 */
d = ff(d, a, b, c, x[i + 1], 12, 0xe8c7b756); /* 2 */
c = ff(c, d, a, b, x[i + 2], 17, 0x242070db); /* 3 */
b = ff(b, c, d, a, x[i + 3], 22, 0xc1bdceee); /* 4 */
a = ff(a, b, c, d, x[i + 4], 7, 0xf57c0faf); /* 5 */
d = ff(d, a, b, c, x[i + 5], 12, 0x4787c62a); /* 6 */
c = ff(c, d, a, b, x[i + 6], 17, 0xa8304613); /* 7 */
b = ff(b, c, d, a, x[i + 7], 22, 0xfd469501); /* 8 */
a = ff(a, b, c, d, x[i + 8], 7, 0x698098d8); /* 9 */
d = ff(d, a, b, c, x[i + 9], 12, 0x8b44f7af); /* 10 */
c = ff(c, d, a, b, x[i + 10], 17, 0xffff5bb1); /* 11 */
b = ff(b, c, d, a, x[i + 11], 22, 0x895cd7be); /* 12 */
a = ff(a, b, c, d, x[i + 12], 7, 0x6b901122); /* 13 */
d = ff(d, a, b, c, x[i + 13], 12, 0xfd987193); /* 14 */
c = ff(c, d, a, b, x[i + 14], 17, 0xa679438e); /* 15 */
b = ff(b, c, d, a, x[i + 15], 22, 0x49b40821); /* 16 */
// Round 2
a = gg(a, b, c, d, x[i + 1], 5, 0xf61e2562); /* 17 */
d = gg(d, a, b, c, x[i + 6], 9, 0xc040b340); /* 18 */
c = gg(c, d, a, b, x[i + 11], 14, 0x265e5a51); /* 19 */
b = gg(b, c, d, a, x[i + 0], 20, 0xe9b6c7aa); /* 20 */
a = gg(a, b, c, d, x[i + 5], 5, 0xd62f105d); /* 21 */
d = gg(d, a, b, c, x[i + 10], 9, 0x2441453); /* 22 */
c = gg(c, d, a, b, x[i + 15], 14, 0xd8a1e681); /* 23 */
b = gg(b, c, d, a, x[i + 4], 20, 0xe7d3fbc8); /* 24 */
a = gg(a, b, c, d, x[i + 9], 5, 0x21e1cde6); /* 25 */
d = gg(d, a, b, c, x[i + 14], 9, 0xc33707d6); /* 26 */
c = gg(c, d, a, b, x[i + 3], 14, 0xf4d50d87); /* 27 */
b = gg(b, c, d, a, x[i + 8], 20, 0x455a14ed); /* 28 */
a = gg(a, b, c, d, x[i + 13], 5, 0xa9e3e905); /* 29 */
d = gg(d, a, b, c, x[i + 2], 9, 0xfcefa3f8); /* 30 */
c = gg(c, d, a, b, x[i + 7], 14, 0x676f02d9); /* 31 */
b = gg(b, c, d, a, x[i + 12], 20, 0x8d2a4c8a); /* 32 */
// Round 3
a = hh(a, b, c, d, x[i + 5], 4, 0xfffa3942); /* 33 */
d = hh(d, a, b, c, x[i + 8], 11, 0x8771f681); /* 34 */
c = hh(c, d, a, b, x[i + 11], 16, 0x6d9d6122); /* 35 */
b = hh(b, c, d, a, x[i + 14], 23, 0xfde5380c); /* 36 */
a = hh(a, b, c, d, x[i + 1], 4, 0xa4beea44); /* 37 */
d = hh(d, a, b, c, x[i + 4], 11, 0x4bdecfa9); /* 38 */
c = hh(c, d, a, b, x[i + 7], 16, 0xf6bb4b60); /* 39 */
b = hh(b, c, d, a, x[i + 10], 23, 0xbebfbc70); /* 40 */
a = hh(a, b, c, d, x[i + 13], 4, 0x289b7ec6); /* 41 */
d = hh(d, a, b, c, x[i + 0], 11, 0xeaa127fa); /* 42 */
c = hh(c, d, a, b, x[i + 3], 16, 0xd4ef3085); /* 43 */
b = hh(b, c, d, a, x[i + 6], 23, 0x4881d05); /* 44 */
a = hh(a, b, c, d, x[i + 9], 4, 0xd9d4d039); /* 45 */
d = hh(d, a, b, c, x[i + 12], 11, 0xe6db99e5); /* 46 */
c = hh(c, d, a, b, x[i + 15], 16, 0x1fa27cf8); /* 47 */
b = hh(b, c, d, a, x[i + 2], 23, 0xc4ac5665); /* 48 */
// Round 4
a = ii(a, b, c, d, x[i + 0], 6, 0xf4292244); /* 49 */
d = ii(d, a, b, c, x[i + 7], 10, 0x432aff97); /* 50 */
c = ii(c, d, a, b, x[i + 14], 15, 0xab9423a7); /* 51 */
b = ii(b, c, d, a, x[i + 5], 21, 0xfc93a039); /* 52 */
a = ii(a, b, c, d, x[i + 12], 6, 0x655b59c3); /* 53 */
d = ii(d, a, b, c, x[i + 3], 10, 0x8f0ccc92); /* 54 */
c = ii(c, d, a, b, x[i + 10], 15, 0xffeff47d); /* 55 */
b = ii(b, c, d, a, x[i + 1], 21, 0x85845dd1); /* 56 */
a = ii(a, b, c, d, x[i + 8], 6, 0x6fa87e4f); /* 57 */
d = ii(d, a, b, c, x[i + 15], 10, 0xfe2ce6e0); /* 58 */
c = ii(c, d, a, b, x[i + 6], 15, 0xa3014314); /* 59 */
b = ii(b, c, d, a, x[i + 13], 21, 0x4e0811a1); /* 60 */
a = ii(a, b, c, d, x[i + 4], 6, 0xf7537e82); /* 61 */
d = ii(d, a, b, c, x[i + 11], 10, 0xbd3af235); /* 62 */
c = ii(c, d, a, b, x[i + 2], 15, 0x2ad7d2bb); /* 63 */
b = ii(b, c, d, a, x[i + 9], 21, 0xeb86d391); /* 64 */
a += olda;
b += oldb;
c += oldc;
d += oldd;
}
byte[] result = new byte[16];
result[0] = (byte)(a & 0xff);
result[1] = (byte)((a >> 8) & 0xff);
result[2] = (byte)((a >> 16) & 0xff);
result[3] = (byte)((a >> 24) & 0xff);
result[4] = (byte)(b & 0xff);
result[5] = (byte)((b >> 8) & 0xff);
result[6] = (byte)((b >> 16) & 0xff);
result[7] = (byte)((b >> 24) & 0xff);
result[8] = (byte)(c & 0xff);
result[9] = (byte)((c >> 8) & 0xff);
result[10] = (byte)((c >> 16) & 0xff);
result[11] = (byte)((c >> 24) & 0xff);
result[12] = (byte)(d & 0xff);
result[13] = (byte)((d >> 8) & 0xff);
result[14] = (byte)((d >> 16) & 0xff);
result[15] = (byte)((d >> 24) & 0xff);
return result;
}
public static string HexHash(byte[] data) {
byte[] d = Hash(data);
string s = string.Empty;
for (int i = 0; i < 16; i++) {
s += d[i].ToString("x2");
}
return s;
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.