[docs] Import of Microsoft BCL Documentation.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / RSACryptoServiceProvider.cs
index bf4de13a90906925ed10d4c4fb9272b0f404585b..4cd3779172c0cecba59890f2de6cb83dbb5b89ee 100644 (file)
@@ -29,8 +29,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if !MOONLIGHT
-
 using System.IO;
 using System.Runtime.InteropServices;
 
@@ -54,6 +52,7 @@ namespace System.Security.Cryptography {
                private RSAManaged rsa;
        
                public RSACryptoServiceProvider ()
+                       : this (1024)
                {
                        // Here it's not clear if we need to generate a keypair
                        // (note: MS implementation generates a keypair in this case).
@@ -63,29 +62,31 @@ namespace System.Security.Cryptography {
                        // So we'll generate the keypair only when (and if) it's being
                        // used (or exported). This should save us a lot of time (at 
                        // least in the unit tests).
-                       Common (1024, null);
                }
        
                public RSACryptoServiceProvider (CspParameters parameters) 
+                       : this (1024, parameters)
                {
-                       Common (1024, parameters);
                        // no keypair generation done at this stage
                }
        
                public RSACryptoServiceProvider (int dwKeySize) 
                {
                        // Here it's clear that we need to generate a new keypair
-                       Common (dwKeySize, null);
+                       Common (dwKeySize, false);
                        // no keypair generation done at this stage
                }
        
                public RSACryptoServiceProvider (int dwKeySize, CspParameters parameters) 
                {
-                       Common (dwKeySize, parameters);
+                       bool has_parameters = parameters != null;
+                       Common (dwKeySize, has_parameters);
+                       if (has_parameters)
+                               Common (parameters);
                        // no keypair generation done at this stage
                }
        
-               private void Common (int dwKeySize, CspParameters p
+               void Common (int dwKeySize, bool parameters
                {
                        // Microsoft RSA CSP can do between 384 and 16384 bits keypair
                        LegalKeySizesValue = new KeySizes [1];
@@ -95,30 +96,33 @@ namespace System.Security.Cryptography {
                        rsa = new RSAManaged (KeySize);
                        rsa.KeyGenerated += new RSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
 
-                       persistKey = (p != null);
-                       if (p == null) {
-                               p = new CspParameters (PROV_RSA_FULL);
-                               if (useMachineKeyStore)
-                                       p.Flags |= CspProviderFlags.UseMachineKeyStore;
-                               store = new KeyPairPersistence (p);
-                               // no need to load - it cannot exists
-                       }
-                       else {
-                               store = new KeyPairPersistence (p);
-                               bool exists = store.Load ();
-                               bool required = (p.Flags & CspProviderFlags.UseExistingKey) != 0;
+                       persistKey = parameters;
+                       if (parameters)
+                               return;
 
-                               if (required && !exists)
-                                       throw new CryptographicException ("Keyset does not exist");
+                       // no need to load - it cannot exists
+                       var p = new CspParameters (PROV_RSA_FULL);
+                       if (useMachineKeyStore)
+                               p.Flags |= CspProviderFlags.UseMachineKeyStore;
+                       store = new KeyPairPersistence (p);
+               }
 
-                               if (store.KeyValue != null) {
-                                       persisted = true;
-                                       this.FromXmlString (store.KeyValue);
-                               }
+               void Common (CspParameters p)
+               {
+                       store = new KeyPairPersistence (p);
+                       bool exists = store.Load ();
+                       bool required = (p.Flags & CspProviderFlags.UseExistingKey) != 0;
+
+                       if (required && !exists)
+                               throw new CryptographicException ("Keyset does not exist");
+
+                       if (store.KeyValue != null) {
+                               persisted = true;
+                               FromXmlString (store.KeyValue);
                        }
                }
 
-               private static bool useMachineKeyStore = false;
+               private static bool useMachineKeyStore;
 
                public static bool UseMachineKeyStore {
                        get { return useMachineKeyStore; }
@@ -231,7 +235,7 @@ namespace System.Security.Cryptography {
 
                        HashAlgorithm hash = null;
                        if (halg is String)
-                               hash = HashAlgorithm.Create ((String)halg);
+                               hash = GetHashFromString ((string) halg);
                        else if (halg is HashAlgorithm)
                                hash = (HashAlgorithm) halg;
                        else if (halg is Type)
@@ -239,8 +243,25 @@ namespace System.Security.Cryptography {
                        else
                                throw new ArgumentException ("halg");
 
+                       if (hash == null)
+                               throw new ArgumentException (
+                                               "Could not find provider for halg='" + halg + "'.",
+                                               "halg");
+
                        return hash;
                }
+
+               private HashAlgorithm GetHashFromString (string name)
+               {
+                       HashAlgorithm hash = HashAlgorithm.Create (name);
+                       if (hash != null)
+                               return hash;
+                       try {
+                               return HashAlgorithm.Create (GetHashNameFromOID (name));
+                       } catch (CryptographicException e) {
+                               throw new ArgumentException (e.Message, "halg", e);
+                       }
+               }
        
                // NOTE: this method can work with ANY configured (OID in machine.config) 
                // HashAlgorithm descendant
@@ -408,6 +429,3 @@ namespace System.Security.Cryptography {
                }
        }
 }
-
-#endif
-