New tests.
[mono.git] / mcs / class / corlib / Mono.Security / StrongName.cs
old mode 100755 (executable)
new mode 100644 (file)
index 1252319..2905a03
@@ -8,8 +8,6 @@
 // (C) 2004 Novell (http://www.novell.com)
 //
 
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -116,6 +114,11 @@ namespace Mono.Security {
                {
                }
 
+               public StrongName (int keySize)
+               {
+                       rsa = new RSAManaged (keySize);
+               }
+
                public StrongName (byte[] data)
                {
                        if (data == null)
@@ -157,7 +160,7 @@ namespace Mono.Security {
                        get {
                                if (rsa == null)
                                        return false;
-#if INSIDE_CORLIB
+#if INSIDE_CORLIB && !MOONLIGHT
                                // the easy way
                                if (RSA is RSACryptoServiceProvider) {
                                        // available as internal for corlib
@@ -197,8 +200,9 @@ namespace Mono.Security {
                public byte[] PublicKey {
                        get { 
                                if (publicKey == null) {
-                                       byte[] keyPair = CryptoConvert.ToCapiKeyBlob (rsa, false); 
-                                       publicKey = new byte [32 + 128]; // always 1024 bits
+                                       byte[] keyPair = CryptoConvert.ToCapiKeyBlob (rsa, false);
+                                       // since 2.0 public keys can vary from 384 to 16384 bits
+                                       publicKey = new byte [32 + (rsa.KeySize >> 3)];
 
                                        // The first 12 bytes are documented at:
                                        // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
@@ -235,7 +239,7 @@ namespace Mono.Security {
                                        byte[] publicKey = PublicKey;
                                        if (publicKey == null)
                                                return null;
-                                       HashAlgorithm ha = SHA1.Create (TokenAlgorithm);
+                                       HashAlgorithm ha = HashAlgorithm.Create (TokenAlgorithm);
                                        byte[] hash = ha.ComputeHash (publicKey);
                                        // we need the last 8 bytes in reverse order
                                        keyToken = new byte [8];
@@ -426,11 +430,17 @@ namespace Mono.Security {
 
                public bool Verify (string fileName) 
                {
-                       StrongNameSignature sn;
+                       bool result = false;
                        using (FileStream fs = File.OpenRead (fileName)) {
-                               sn = StrongHash (fs, StrongNameOptions.Signature);
+                               result = Verify (fs);
                                fs.Close ();
                        }
+                       return result;
+               }
+
+               public bool Verify (Stream stream)
+               {
+                       StrongNameSignature sn = StrongHash (stream, StrongNameOptions.Signature);
                        if (sn.Hash == null) {
                                return false;
                        }
@@ -457,8 +467,12 @@ namespace Mono.Security {
                        if (!initialized) {
                                lock (lockObject) {
                                        if (!initialized) {
+#if NET_2_1
+                                               // Moonlight cannot depend on machine.config
+#else
                                                string config = Environment.GetMachineConfigPath ();
                                                StrongNameManager.LoadConfig (config);
+#endif
                                                initialized = true;
                                        }
                                }