Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / randomnumbergenerator.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 //
10 // RandomNumberGenerator.cs
11 //
12
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
22     : IDisposable
23 #endif // !FEATURE_CORECLR
24     {
25         protected RandomNumberGenerator() {
26         }
27     
28         //
29         // public methods
30         //
31
32 #if (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO
33         static public RandomNumberGenerator Create() {
34 #if FULL_AOT_RUNTIME
35             return new System.Security.Cryptography.RNGCryptoServiceProvider ();
36 #else
37             return Create("System.Security.Cryptography.RandomNumberGenerator");
38 #endif
39         }
40
41         static public RandomNumberGenerator Create(String rngName) {
42             return (RandomNumberGenerator) CryptoConfig.CreateFromName(rngName);
43         }
44 #endif // (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO
45
46         public void Dispose() {
47             Dispose(true);
48             GC.SuppressFinalize(this);
49         }
50
51         protected virtual void Dispose(bool disposing) {
52             return;
53         }
54
55         public abstract void GetBytes(byte[] data);
56
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"));
62
63             if (count > 0) {
64                 byte[] tempData = new byte[count];
65                 GetBytes(tempData);
66                 Array.Copy(tempData, 0, data, offset, count);
67             }
68         }
69
70 #if (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO
71         public virtual void GetNonZeroBytes(byte[] data)
72         {
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();
78         }
79 #endif // (!FEATURE_CORECLR && !SILVERLIGHT) || FEATURE_LEGACYNETCFCRYPTO
80     }
81 }