Added hammock project to debug streaming issues
[pithos-ms-client] / trunk / hammock / src / netCF / Hammock.Compact / Security / Cryptography / HMACSHA1.cs
1 using System;
2 using System.Security.Cryptography;
3 using Hammock.Mono.Security.Cryptography;
4
5 namespace Hammock.Security.Cryptography
6 {
7   public class HMACSHA1 : KeyedHashAlgorithm
8   {
9     private readonly HMACAlgorithm hmac;
10     private bool m_disposed;
11
12     public override sealed byte[] Key
13     {
14       get
15       {
16         return base.Key;
17       }
18       set
19       {
20         this.hmac.Key = value;
21         base.Key = value;
22       }
23     }
24
25     public string HashName
26     {
27       get
28       {
29         return this.hmac.HashName;
30       }
31       set
32       {
33         if (this.State == 0)
34           this.hmac.HashName = value;
35       }
36     }
37
38     public HMACSHA1()
39       : this(KeyBuilder.Key(8))
40     {
41     }
42
43     public HMACSHA1(byte[] rgbKey)
44     {
45       this.hmac = new HMACAlgorithm("SHA1");
46       this.HashSizeValue = 160;
47       this.Key = rgbKey;
48       this.m_disposed = false;
49     }
50
51     ~HMACSHA1()
52     {
53       this.Dispose(false);
54     }
55
56     protected override void Dispose(bool disposing)
57     {
58       if (!this.m_disposed)
59       {
60         if (this.hmac != null)
61           this.hmac.Dispose();
62         base.Dispose(disposing);
63         this.m_disposed = true;
64       }
65     }
66
67     public override void Initialize()
68     {
69       if (this.m_disposed)
70         throw new ObjectDisposedException("HMACSHA1");
71       else if (!(this.hmac.Algo is SHA1))
72       {
73         throw new InvalidCastException(string.Format("Invalid hash algorithm '{0}', expected '{1}'.", this.hmac.Algo == null ? (object)"none" : (object)this.hmac.Algo.GetType().ToString(), (object)"SHA1"));
74       }
75       else
76       {
77         this.State = 0;
78         this.hmac.Initialize();
79       }
80     }
81
82     protected override void HashCore(byte[] rgb, int ib, int cb)
83     {
84       if (this.m_disposed)
85       {
86         throw new ObjectDisposedException("HMACSHA1");
87       }
88       else
89       {
90         if (this.State == 0)
91         {
92           this.Initialize();
93           this.State = 1;
94         }
95         this.hmac.Core(rgb, ib, cb);
96       }
97     }
98
99     protected override byte[] HashFinal()
100     {
101       if (this.m_disposed)
102       {
103         throw new ObjectDisposedException("HMACSHA1");
104       }
105       else
106       {
107         this.State = 0;
108         return this.hmac.Final();
109       }
110     }
111   }
112 }