2
using System.Security.Cryptography;
4
namespace System.Security.Cryptography
9
public class HMACMD5 : KeyedHashAlgorithm
11
private MD5 hash1 = null;
12
private MD5 hash2 = null;
13
private bool bHashing = false;
14
private byte[] rgbInner = new byte[64];
15
private byte[] rgbOuter = new byte[64];
20
/// <param name="rgbKey"></param>
21
public HMACMD5 (byte[] rgbKey)
24
// Create the hash algorithms.
34
public override byte[] Key
36
get { return (byte[])KeyValue.Clone(); }
40
throw new Exception("Cannot change key during hash operation");
42
if(value.Length > 64){
43
KeyValue = hash1.ComputeHash(value);
44
// No need to call Initialize, ComputeHash does it automatically.
47
KeyValue = (byte[]) value.Clone();
49
// Compute rgbInner and rgbOuter.
55
for(i=0;i<KeyValue.Length;i++){
56
rgbInner[i] ^= KeyValue[i];
57
rgbOuter[i] ^= KeyValue[i];
65
public override void Initialize()
75
/// <param name="rgb"></param>
76
/// <param name="ib"></param>
77
/// <param name="cb"></param>
78
protected override void HashCore(byte[] rgb,int ib,int cb)
80
if(bHashing == false){
81
hash1.TransformBlock(rgbInner,0,64,rgbInner, 0);
84
hash1.TransformBlock(rgb,ib,cb,rgb,ib);
90
/// <returns></returns>
91
protected override byte[] HashFinal()
93
if(bHashing == false){
94
hash1.TransformBlock(rgbInner,0,64,rgbInner,0);
97
// Finalize the original hash.
98
hash1.TransformFinalBlock(new byte[0],0,0);
99
// Write the outer array.
100
hash2.TransformBlock(rgbOuter,0,64,rgbOuter, 0);
101
// Write the inner hash and finalize the hash.
102
hash2.TransformFinalBlock(hash1.Hash,0,hash1.Hash.Length);