2 // System.Reflection.StrongNameKeyPair.cs
5 // Kevin Winchester (kwin@ns.sympatico.ca)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // (C) 2002 Kevin Winchester
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2004 Novell (http://www.novell.com)
14 using System.Security.Cryptography;
17 using Mono.Security.Cryptography;
19 namespace System.Reflection {
22 public class StrongNameKeyPair
24 private byte[] publicKey;
28 public StrongNameKeyPair (byte[] keyPairArray)
30 if (keyPairArray == null)
31 throw new ArgumentNullException ("keyPairArray");
33 LoadKey (keyPairArray);
36 public StrongNameKeyPair (FileStream keyPairFile)
38 if (keyPairFile == null)
39 throw new ArgumentNullException ("keyPairFile");
41 byte[] input = new byte [keyPairFile.Length];
42 keyPairFile.Read (input, 0, input.Length);
46 public StrongNameKeyPair (string keyPairContainer)
48 // named key container
49 if (keyPairContainer == null)
50 throw new ArgumentNullException ("keyPairContainer");
52 CspParameters csp = new CspParameters ();
53 csp.KeyContainerName = keyPairContainer;
54 rsa = new RSACryptoServiceProvider (csp);
57 private void LoadKey (byte[] key)
61 if (key.Length == 16) {
64 while (i < key.Length)
68 publicKey = (byte[]) key.Clone ();
72 rsa = CryptoConvert.FromCapiKeyBlob (key);
76 // exception is thrown when getting PublicKey
77 // to match MS implementation
81 public byte[] PublicKey {
83 if (publicKey == null) {
84 // ECMA "key" is valid but doesn't produce a RSA instance
86 throw new ArgumentException ("invalid keypair");
88 byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
89 publicKey = new byte [blob.Length + 12];
90 // The first 12 bytes are documented at:
\r
91 // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
\r
92 // ALG_ID - Signature
\r
93 publicKey[0] = 0x00;
\r
94 publicKey[1] = 0x24;
\r
95 publicKey[2] = 0x00;
\r
96 publicKey[3] = 0x00;
\r
98 publicKey[4] = 0x04;
\r
99 publicKey[5] = 0x80;
\r
100 publicKey[6] = 0x00;
\r
101 publicKey[7] = 0x00;
\r
102 // Length of Public Key (in bytes)
\r
103 int lastPart = blob.Length;
\r
104 publicKey[8] = (byte)(lastPart % 256);
\r
105 publicKey[9] = (byte)(lastPart / 256); // just in case
\r
106 publicKey[10] = 0x00;
\r
107 publicKey[11] = 0x00;
\r
109 Buffer.BlockCopy (blob, 0, publicKey, 12, blob.Length);
115 internal StrongName StrongName ()
118 return new StrongName (rsa);
119 if (publicKey != null)
120 return new StrongName (publicKey);