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