2 using System.Security.Cryptography;
4 namespace Hammock.Mono.Security.Cryptography
6 internal class HMACAlgorithm
8 private HashAlgorithm algo;
9 private BlockProcessor block;
11 private string hashName;
14 public HashAlgorithm Algo
22 public string HashName
30 this.CreateHash(value);
42 if (value != null && value.Length > 64)
43 this.key = this.algo.ComputeHash(value);
44 else if (value != null)
45 this.key = (byte[])value.Clone();
49 public HMACAlgorithm(string algoName)
51 this.CreateHash(algoName);
59 private void CreateHash(string algoName)
61 this.algo = HashAlgorithm.Create(algoName);
62 this.hashName = algoName;
63 this.block = new BlockProcessor((ICryptoTransform)this.algo, 8);
69 Array.Clear((Array)this.key, 0, this.key.Length);
72 public void Initialize()
74 this.hash = (byte[])null;
75 this.block.Initialize();
76 byte[] rgb = HMACAlgorithm.KeySetup(this.key, (byte)54);
77 this.algo.Initialize();
79 Array.Clear((Array)rgb, 0, rgb.Length);
82 private static byte[] KeySetup(byte[] key, byte padding)
84 byte[] numArray = new byte[64];
85 for (int index = 0; index < key.Length; ++index)
86 numArray[index] = (byte)((uint)key[index] ^ (uint)padding);
87 for (int length = key.Length; length < 64; ++length)
88 numArray[length] = padding;
92 public void Core(byte[] rgb, int ib, int cb)
94 this.block.Core(rgb, ib, cb);
100 byte[] hash = this.algo.Hash;
101 byte[] numArray = HMACAlgorithm.KeySetup(this.key, (byte)92);
102 this.algo.Initialize();
103 this.algo.TransformBlock(numArray, 0, numArray.Length, numArray, 0);
104 this.algo.TransformFinalBlock(hash, 0, hash.Length);
105 this.hash = this.algo.Hash;
106 Array.Clear((Array)numArray, 0, numArray.Length);
107 Array.Clear((Array)hash, 0, hash.Length);