2 // System.Security.Cryptography.RNGCryptoServiceProvider
5 // Mark Crichton (crichton@gimp.org)
6 // Sebastien Pouliot (sebastien@ximian.com)
9 // (C) 2004 Novell (http://www.novell.com)
12 // "In the beginning there was Chaos,
13 // and within this Chaos was Power,
14 // Great Power without form."
15 // -- The Verrah Rubicon of Verena, Book One
18 using System.Runtime.CompilerServices;
21 namespace System.Security.Cryptography {
24 public class RNGCryptoServiceProvider : RandomNumberGenerator {
26 public sealed class RNGCryptoServiceProvider : RandomNumberGenerator {
28 public RNGCryptoServiceProvider ()
32 public RNGCryptoServiceProvider (byte[] rgb)
37 public RNGCryptoServiceProvider (CspParameters cspParams)
39 // CSP selection isn't supported
40 // but we still return random (no exception) for compatibility
43 public RNGCryptoServiceProvider (string str)
45 Seed (Encoding.UTF8.GetBytes (str));
48 [MethodImplAttribute(MethodImplOptions.InternalCall)]
49 private static extern void Seed (byte[] data);
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 private extern void InternalGetBytes (byte[] data);
54 public override void GetBytes (byte[] data)
57 throw new ArgumentNullException ("data");
59 InternalGetBytes (data);
62 public override void GetNonZeroBytes (byte[] data)
65 throw new ArgumentNullException ("data");
67 byte[] random = new byte [data.Length * 2];
69 // one pass should be enough but hey this is random ;-)
70 while (i < data.Length) {
72 for (int j=0; j < random.Length; j++) {
76 data [i++] = random [j];
81 /* Commented as we don't require this right now (and it will perform better that way)
82 ~RNGCryptoServiceProvider ()
84 // in our case we have nothing unmanaged to dispose