imradv3 / src /EchoDictLib /Ripemd128.cs
fasdfsa's picture
加入完整 mdict 查询功能
38cc2e9
using System;
namespace Echodict
{
public class Ripemd128
{
private static readonly uint[] KA = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e };
private static readonly uint[] KB = { 0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9 };
private static readonly int[] ROTA = {
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, 7, 6, 8, 13,
11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9, 13, 15,
14, 8, 13, 6, 5, 12, 7, 5, 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6,
8, 6, 5, 12, 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
};
private static readonly int[] ROTB = {
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, 9, 13, 15, 7,
12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8, 6, 6, 14,
12, 13, 5, 14, 13, 13, 7, 5, 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9,
12, 5, 15, 8, 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
};
private static readonly int[] WA = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 4, 13, 1,
10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8, 1,
2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15,
14, 5, 6, 2, 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
};
private static readonly int[] WB = {
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 6, 11, 3, 7,
0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14, 6, 9,
11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13,
9, 7, 10, 14, 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
};
private ulong _count;
private byte[] _buffer = new byte[64];
private uint[] _state = new uint[4];
public Ripemd128()
{
_state[0] = 0x67452301;
_state[1] = 0xEFCDAB89;
_state[2] = 0x98BADCFE;
_state[3] = 0x10325476;
_count = 0;
}
private static uint Rol(uint value, int bits)
{
return (value << bits) | (value >> (32 - bits));
}
private void Transform(byte[] buffer, int offset = 0)
{
uint a = _state[0];
uint b = _state[1];
uint c = _state[2];
uint d = _state[3];
uint e = _state[0];
uint f = _state[1];
uint g = _state[2];
uint h = _state[3];
uint[] block = new uint[16];
for (int i = 0; i < 16; i++)
{
block[i] = BitConverter.ToUInt32(buffer, offset + 4 * i);
}
int n = 0;
// Round 1 (0-15)
// R128_0
for (int k = 0; k < 4; k++)
{
// Round0To15(a, b, c, d, e, f, g, h)
a = Rol(a + ((b ^ c ^ d) + block[WA[n]]), ROTA[n]);
e = Rol(e + ((((f ^ g) & h) ^ g) + block[WB[n]] + KB[0]), ROTB[n]);
n++;
// Round0To15(d, a, b, c, h, e, f, g)
d = Rol(d + ((a ^ b ^ c) + block[WA[n]]), ROTA[n]);
h = Rol(h + ((((e ^ f) & g) ^ f) + block[WB[n]] + KB[0]), ROTB[n]);
n++;
// Round0To15(c, d, a, b, g, h, e, f)
c = Rol(c + ((d ^ a ^ b) + block[WA[n]]), ROTA[n]);
g = Rol(g + ((((h ^ e) & f) ^ e) + block[WB[n]] + KB[0]), ROTB[n]);
n++;
// Round0To15(b, c, d, a, f, g, h, e)
b = Rol(b + ((c ^ d ^ a) + block[WA[n]]), ROTA[n]);
f = Rol(f + ((((g ^ h) & e) ^ h) + block[WB[n]] + KB[0]), ROTB[n]);
n++;
}
// Round 2 (16-31)
// R128_16
for (int k = 0; k < 4; k++)
{
// Round16To31(a, b, c, d, e, f, g, h)
// f1 = ((c^d)&b)^d; f2 = (~g|f)^h
a = Rol(a + ((((c ^ d) & b) ^ d) + block[WA[n]] + KA[0]), ROTA[n]);
e = Rol(e + (((~g | f) ^ h) + block[WB[n]] + KB[1]), ROTB[n]);
n++;
// Round16To31(d, a, b, c, h, e, f, g)
// f1 = ((b^c)&a)^c; f2 = (~f|e)^g
d = Rol(d + ((((b ^ c) & a) ^ c) + block[WA[n]] + KA[0]), ROTA[n]);
h = Rol(h + (((~f | e) ^ g) + block[WB[n]] + KB[1]), ROTB[n]);
n++;
// Round16To31(c, d, a, b, g, h, e, f)
// f1 = ((a^b)&d)^b; f2 = (~e|h)^f
c = Rol(c + ((((a ^ b) & d) ^ b) + block[WA[n]] + KA[0]), ROTA[n]);
g = Rol(g + (((~e | h) ^ f) + block[WB[n]] + KB[1]), ROTB[n]);
n++;
// Round16To31(b, c, d, a, f, g, h, e)
// f1 = ((d^a)&c)^a; f2 = (~h|g)^e
b = Rol(b + ((((d ^ a) & c) ^ a) + block[WA[n]] + KA[0]), ROTA[n]);
f = Rol(f + (((~h | g) ^ e) + block[WB[n]] + KB[1]), ROTB[n]);
n++;
}
// Round 3 (32-47)
// R128_32
for (int k = 0; k < 4; k++)
{
// Round32To47(a, b, c, d, e, f, g, h)
// f1 = (~c|b)^d; f2 = ((g^h)&f)^h
a = Rol(a + (((~c | b) ^ d) + block[WA[n]] + KA[1]), ROTA[n]);
e = Rol(e + ((((g ^ h) & f) ^ h) + block[WB[n]] + KB[2]), ROTB[n]);
n++;
// Round32To47(d, a, b, c, h, e, f, g)
// f1 = (~b|a)^c; f2 = ((f^g)&e)^g
d = Rol(d + (((~b | a) ^ c) + block[WA[n]] + KA[1]), ROTA[n]);
h = Rol(h + ((((f ^ g) & e) ^ g) + block[WB[n]] + KB[2]), ROTB[n]);
n++;
// Round32To47(c, d, a, b, g, h, e, f)
// f1 = (~a|d)^b; f2 = ((e^f)&h)^f
c = Rol(c + (((~a | d) ^ b) + block[WA[n]] + KA[1]), ROTA[n]);
g = Rol(g + ((((e ^ f) & h) ^ f) + block[WB[n]] + KB[2]), ROTB[n]);
n++;
// Round32To47(b, c, d, a, f, g, h, e)
// f1 = (~d|c)^a; f2 = ((h^e)&g)^e
b = Rol(b + (((~d | c) ^ a) + block[WA[n]] + KA[1]), ROTA[n]);
f = Rol(f + ((((h ^ e) & g) ^ e) + block[WB[n]] + KB[2]), ROTB[n]);
n++;
}
// Round 4 (48-63)
// R128_48
for (int k = 0; k < 4; k++)
{
// Round48To63(a, b, c, d, e, f, g, h)
// f1 = ((b^c)&d)^c; f2 = f^g^h
a = Rol(a + ((((b ^ c) & d) ^ c) + block[WA[n]] + KA[2]), ROTA[n]);
e = Rol(e + ((f ^ g ^ h) + block[WB[n]]), ROTB[n]);
n++;
// Round48To63(d, a, b, c, h, e, f, g)
// f1 = ((a^b)&c)^b; f2 = e^f^g
d = Rol(d + ((((a ^ b) & c) ^ b) + block[WA[n]] + KA[2]), ROTA[n]);
h = Rol(h + ((e ^ f ^ g) + block[WB[n]]), ROTB[n]);
n++;
// Round48To63(c, d, a, b, g, h, e, f)
// f1 = ((d^a)&b)^a; f2 = h^e^f
c = Rol(c + ((((d ^ a) & b) ^ a) + block[WA[n]] + KA[2]), ROTA[n]);
g = Rol(g + ((h ^ e ^ f) + block[WB[n]]), ROTB[n]);
n++;
// Round48To63(b, c, d, a, f, g, h, e)
// f1 = ((c^d)&a)^d; f2 = g^h^e
b = Rol(b + ((((c ^ d) & a) ^ d) + block[WA[n]] + KA[2]), ROTA[n]);
f = Rol(f + ((g ^ h ^ e) + block[WB[n]]), ROTB[n]);
n++;
}
h += c + _state[1];
_state[1] = _state[2] + d + e;
_state[2] = _state[3] + a + f;
_state[3] = _state[0] + b + g;
_state[0] = h;
}
public void Update(byte[] data, int offset, int len)
{
int i, j;
j = (int)(_count & 63);
_count += (ulong)len;
if ((j + len) > 63)
{
Array.Copy(data, offset, _buffer, j, 64 - j);
Transform(_buffer);
i = 64 - j;
for (; i + 63 < len; i += 64)
{
Transform(data, offset + i);
}
j = 0;
}
else
{
i = 0;
}
Array.Copy(data, offset + i, _buffer, j, len - i);
}
public void Update(byte[] data)
{
Update(data, 0, data.Length);
}
public byte[] Digest()
{
ulong finalcount = _count << 3;
Update(new byte[] { 0x80 }, 0, 1);
while ((_count & 63) != 56)
{
Update(new byte[] { 0 }, 0, 1);
}
byte[] countBytes = BitConverter.GetBytes(finalcount); // Little endian
Update(countBytes, 0, 8);
byte[] digest = new byte[16];
for (int i = 0; i < 4; i++)
{
byte[] b = BitConverter.GetBytes(_state[i]);
Array.Copy(b, 0, digest, i * 4, 4);
}
return digest;
}
}
}