/
MD5Managed.cs
84 lines (75 loc) · 2.38 KB
/
MD5Managed.cs
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
// Fetched from http://archive.msdn.microsoft.com/SilverlightMD5
//Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Security.Cryptography;
// **************************************************************
// * Raw implementation of the MD5 hash algorithm
// * from RFC 1321.
// *
// * Written By: Reid Borsuk and Jenny Zheng
// * Copyright (c) Microsoft Corporation. All rights reserved.
// **************************************************************
namespace Akavache
{
#if SILVERLIGHT
public class MD5Managed : HashAlgorithm
#else
public class MD5Managed : MD5
#endif
{
private byte[] _data;
private ABCDStruct _abcd;
private Int64 _totalLength;
private int _dataSize;
public MD5Managed()
{
base.HashSizeValue = 0x80;
this.Initialize();
}
public override void Initialize()
{
_data = new byte[64];
_dataSize = 0;
_totalLength = 0;
_abcd = new ABCDStruct();
//Intitial values as defined in RFC 1321
_abcd.A = 0x67452301;
_abcd.B = 0xefcdab89;
_abcd.C = 0x98badcfe;
_abcd.D = 0x10325476;
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
int startIndex = ibStart;
int totalArrayLength = _dataSize + cbSize;
if (totalArrayLength >= 64)
{
Array.Copy(array, startIndex, _data, _dataSize, 64 - _dataSize);
// Process message of 64 bytes (512 bits)
MD5Core.GetHashBlock(_data, ref _abcd, 0);
startIndex += 64 - _dataSize;
totalArrayLength -= 64;
while (totalArrayLength >= 64)
{
Array.Copy(array, startIndex, _data, 0, 64);
MD5Core.GetHashBlock(array, ref _abcd, startIndex);
totalArrayLength -= 64;
startIndex += 64;
}
_dataSize = totalArrayLength;
Array.Copy(array, startIndex, _data, 0, totalArrayLength);
}
else
{
Array.Copy(array, startIndex, _data, _dataSize, cbSize);
_dataSize = totalArrayLength;
}
_totalLength += cbSize;
}
protected override byte[] HashFinal()
{
base.HashValue = MD5Core.GetHashFinalBlock(_data, 0, _dataSize, _abcd, _totalLength * 8);
return base.HashValue;
}
}
}