2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.Security.Win32 / Mono.Security.Cryptography / SHA1CryptoServiceProvider.cs
1 //
2 // Mono.Security.Cryptography.SHA1CryptoServiceProvider
3 //
4 // Authors:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Security.Cryptography;
12
13 namespace Mono.Security.Cryptography {
14
15 public class SHA1CryptoServiceProvider : SHA1 {
16
17         private CapiHash hash;
18
19         public SHA1CryptoServiceProvider ()
20         {
21                 hash = null;
22         }
23
24         ~SHA1CryptoServiceProvider () 
25         {
26                 Dispose (true);
27         }
28
29         // 2 cases:
30         // a. we were calculing a hash and want to abort
31         // b. we haven't started yet
32         public override void Initialize () 
33         {
34                 State = 0;
35                 if (hash == null) {
36                         hash = new CapiHash (CryptoAPI.CALG_SHA1);
37                 }
38         }
39
40         protected override void Dispose (bool disposing) 
41         {
42                 if (hash != null) {
43                         hash.Dispose ();
44                         hash = null;
45                         // there's no unmanaged resources (so disposing isn't used)
46                 }
47         }
48
49         protected override void HashCore (byte[] rgb, int ibStart, int cbSize) 
50         {
51                 if (State == 0)
52                         Initialize ();
53                 if (hash == null)
54                         throw new ObjectDisposedException ("SHA1CryptoServiceProvider");
55                 State = 1;
56                 hash.HashCore (rgb, ibStart, cbSize);
57         }
58
59         protected override byte[] HashFinal () 
60         {
61                 if (hash == null)
62                         throw new ObjectDisposedException ("SHA1CryptoServiceProvider");
63                 State = 0;
64                 byte[] result = hash.HashFinal ();
65                 Dispose (false);
66                 return result;
67         }
68 }
69
70 }