imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / corlib / System.Security.Cryptography / RNGCryptoServiceProvider.cs
1 //
2 // System.Security.Cryptography.RNGCryptoServiceProvider
3 //
4 // Authors:
5 //      Mark Crichton (crichton@gimp.org)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // (C) 2002
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 // "In the beginning there was Chaos,
32 // and within this Chaos was Power,
33 // Great Power without form."
34 // -- The Verrah Rubicon of Verena, Book One
35
36 using System.Globalization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Text;
40
41 namespace System.Security.Cryptography {
42         
43 #if NET_1_0
44         public class RNGCryptoServiceProvider : RandomNumberGenerator {
45 #else
46         #if NET_2_0
47         [ComVisible (true)]
48         #endif
49         public sealed class RNGCryptoServiceProvider : RandomNumberGenerator {
50 #endif
51                 private static object _lock;
52                 private IntPtr _handle;
53
54                 static RNGCryptoServiceProvider ()
55                 {
56                         if (RngOpen ())
57                                 _lock = new object ();
58                 }
59
60                 public RNGCryptoServiceProvider ()
61                 {
62                         _handle = RngInitialize (null);
63                         Check ();
64                 }
65                 
66                 public RNGCryptoServiceProvider (byte[] rgb)
67                 {
68                         _handle = RngInitialize (rgb);
69                         Check ();
70                 }
71                 
72                 public RNGCryptoServiceProvider (CspParameters cspParams)
73                 {
74                         // CSP selection isn't supported but we still return 
75                         // random data (no exception) for compatibility
76                         _handle = RngInitialize (null);
77                         Check ();
78                 }
79                 
80                 public RNGCryptoServiceProvider (string str) 
81                 {
82                         if (str == null)
83                                 _handle = RngInitialize (null);
84                         else
85                                 _handle = RngInitialize (Encoding.UTF8.GetBytes (str));
86                         Check ();
87                 }
88
89                 private void Check () 
90                 {
91                         if (_handle == IntPtr.Zero) {
92                                 throw new CryptographicException (
93                                         Locale.GetText ("Couldn't access random source."));
94                         }
95                 }
96
97                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
98                 private static extern bool RngOpen ();
99                 
100                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
101                 private static extern IntPtr RngInitialize (byte[] seed);
102
103                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104                 private static extern IntPtr RngGetBytes (IntPtr handle, byte[] data);
105
106                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
107                 private static extern void RngClose (IntPtr handle);
108                 
109                 public override void GetBytes (byte[] data) 
110                 {
111                         if (data == null)
112                                 throw new ArgumentNullException ("data");
113
114                         if (_lock == null) {
115                                 _handle = RngGetBytes (_handle, data);
116                         } else {
117                                 // using a global handle for randomness
118                                 lock (_lock) {
119                                         _handle = RngGetBytes (_handle, data);
120                                 }
121                         }
122                         Check ();
123                 }
124                 
125                 public override void GetNonZeroBytes (byte[] data) 
126                 {
127                         if (data == null)
128                                 throw new ArgumentNullException ("data");
129
130                         byte[] random = new byte [data.Length * 2];
131                         int i = 0;
132                         // one pass should be enough but hey this is random ;-)
133                         while (i < data.Length) {
134                                 _handle = RngGetBytes (_handle, random);
135                                 Check ();
136                                 for (int j=0; j < random.Length; j++) {
137                                         if (i == data.Length)
138                                                 break;
139                                         if (random [j] != 0)
140                                                 data [i++] = random [j];
141                                 }
142                         }
143                 }
144                 
145                 ~RNGCryptoServiceProvider () 
146                 {
147                         if (_handle != IntPtr.Zero) {
148                                 RngClose (_handle);
149                                 _handle = IntPtr.Zero;
150                         }
151                 }
152         }
153 }