2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Cryptography / RNGCryptoServiceProvider.cs
blob03e906afd8beaee692154f75e7ef36ede979a848
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)
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.
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
36 using System.Globalization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Text;
41 namespace System.Security.Cryptography {
43 #if !NET_2_1
44 [ComVisible (true)]
45 #endif
46 public sealed class RNGCryptoServiceProvider : RandomNumberGenerator {
47 private static object _lock;
48 private IntPtr _handle;
50 static RNGCryptoServiceProvider ()
52 if (RngOpen ())
53 _lock = new object ();
56 public RNGCryptoServiceProvider ()
58 _handle = RngInitialize (null);
59 Check ();
61 #if !NET_2_1
62 public RNGCryptoServiceProvider (byte[] rgb)
64 _handle = RngInitialize (rgb);
65 Check ();
68 public RNGCryptoServiceProvider (CspParameters cspParams)
70 // CSP selection isn't supported but we still return
71 // random data (no exception) for compatibility
72 _handle = RngInitialize (null);
73 Check ();
76 public RNGCryptoServiceProvider (string str)
78 if (str == null)
79 _handle = RngInitialize (null);
80 else
81 _handle = RngInitialize (Encoding.UTF8.GetBytes (str));
82 Check ();
84 #endif
85 private void Check ()
87 if (_handle == IntPtr.Zero) {
88 throw new CryptographicException (
89 Locale.GetText ("Couldn't access random source."));
93 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94 private static extern bool RngOpen ();
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 private static extern IntPtr RngInitialize (byte[] seed);
99 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100 private static extern IntPtr RngGetBytes (IntPtr handle, byte[] data);
102 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103 private static extern void RngClose (IntPtr handle);
105 public override void GetBytes (byte[] data)
107 if (data == null)
108 throw new ArgumentNullException ("data");
110 if (_lock == null) {
111 _handle = RngGetBytes (_handle, data);
112 } else {
113 // using a global handle for randomness
114 lock (_lock) {
115 _handle = RngGetBytes (_handle, data);
118 Check ();
121 public override void GetNonZeroBytes (byte[] data)
123 if (data == null)
124 throw new ArgumentNullException ("data");
126 byte[] random = new byte [data.Length * 2];
127 int i = 0;
128 // one pass should be enough but hey this is random ;-)
129 while (i < data.Length) {
130 _handle = RngGetBytes (_handle, random);
131 Check ();
132 for (int j=0; j < random.Length; j++) {
133 if (i == data.Length)
134 break;
135 if (random [j] != 0)
136 data [i++] = random [j];
141 ~RNGCryptoServiceProvider ()
143 if (_handle != IntPtr.Zero) {
144 RngClose (_handle);
145 _handle = IntPtr.Zero;