3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
10 // RandomNumberGenerator.cs
13 namespace System.Security.Cryptography {
14 #if !FEATURE_CORECLR && !SILVERLIGHT
15 [System.Runtime.InteropServices.ComVisible(true)]
16 #endif // !FEATURE_CORECLR && !SILVERLIGHT
17 public abstract class RandomNumberGenerator
18 // On Orcas RandomNumberGenerator is not disposable, so we cannot add the IDisposable implementation to the
19 // CoreCLR mscorlib. However, this type does need to be disposable since subtypes can and do hold onto
20 // native resources. Therefore, on desktop mscorlibs we add an IDisposable implementation.
21 #if !FEATURE_CORECLR || FEATURE_CORESYSTEM
23 #endif // !FEATURE_CORECLR
25 protected RandomNumberGenerator() {
32 #if (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO
33 static public RandomNumberGenerator Create() {
35 return new System.Security.Cryptography.RNGCryptoServiceProvider ();
37 return Create("System.Security.Cryptography.RandomNumberGenerator");
41 static public RandomNumberGenerator Create(String rngName) {
42 return (RandomNumberGenerator) CryptoConfig.CreateFromName(rngName);
44 #endif // (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO
46 public void Dispose() {
48 GC.SuppressFinalize(this);
51 protected virtual void Dispose(bool disposing) {
55 public abstract void GetBytes(byte[] data);
57 public virtual void GetBytes(byte[] data, int offset, int count) {
58 if (data == null) throw new ArgumentNullException("data");
59 if (offset < 0) throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
60 if (count < 0) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
61 if (offset + count > data.Length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
64 byte[] tempData = new byte[count];
66 Array.Copy(tempData, 0, data, offset, count);
70 #if (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO
71 public virtual void GetNonZeroBytes(byte[] data)
73 // This method does not exist on Silverlight, so for compatibility we cannot have it be abstract
74 // on the desktop (otherwise any type deriving from RandomNumberGenerator on Silverlight cannot
75 // compile against the desktop CLR). Since this technically is an abstract method with no
76 // implementation, we'll just throw NotImplementedException.
77 throw new NotImplementedException();
79 #endif // (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO