beh.
[mono.git] / mcs / class / corlib / System.Security.Permissions / StrongNamePublicKeyBlob.cs
1 //
2 // StrongNamePublicKeyBlob.cs: Strong Name Public Key Blob
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Text;
12
13 namespace System.Security.Permissions {
14
15 [Serializable]
16 public sealed class StrongNamePublicKeyBlob {
17
18         internal byte[] pubkey;
19
20         public StrongNamePublicKeyBlob (byte[] publicKey) 
21         {
22                 if (publicKey == null)
23                         throw new ArgumentNullException ("publicKey");
24                 // Note: No sanity check ?
25                 pubkey = publicKey;
26         }
27
28         internal static StrongNamePublicKeyBlob FromString (string s)
29         {
30                 int length = s.Length / 2;
31         
32                 byte [] array = new byte [length];
33
34                 for (int i = 0, j = 0; i < s.Length; i += 2, j ++) {
35                         byte left = CharToByte (s [i]);
36                         byte right = CharToByte (s [i+1]);
37                         array [j] = Convert.ToByte (left * 16 + right);
38                 }
39                 
40                 return new StrongNamePublicKeyBlob (array);
41         }
42
43         static byte CharToByte (char c)
44         {
45                 char ch = Char.ToLower (c);
46                 
47                 if (Char.IsDigit (ch))
48                         return (byte) (ch - '0');
49                 else 
50                         return (byte) (ch - 'a' + 10);
51         }
52         
53         public override bool Equals (object obj) 
54         {
55                 bool result = (obj is StrongNamePublicKeyBlob);
56                 if (result) {
57                         StrongNamePublicKeyBlob snpkb = (obj as StrongNamePublicKeyBlob);
58                         result = (pubkey.Length == snpkb.pubkey.Length);
59                         if (result) {
60                                 for (int i = 0; i < pubkey.Length; i++) {
61                                         if (pubkey[i] != snpkb.pubkey[i])
62                                                 return false;
63                                 }
64                         }
65                 }
66                 return result;
67         }
68
69         // LAMESPEC: non standard get hash code - (a) Why ??? (b) How ???
70         // It seems to be the first four bytes of the public key data
71         // which seems like non sense as all valid public key will have the same header ?
72         public override int GetHashCode () 
73         {
74                 int hash = 0;
75                 int i = 0;
76                 // a BAD public key can be less than 4 bytes
77                 int n = Math.Min (pubkey.Length, 4);
78                 while (i < n)
79                         hash = (hash << 8) + pubkey [i++];
80                 return hash;
81         }
82
83         public override string ToString () 
84         {
85                 StringBuilder sb = new StringBuilder ();
86                 for (int i=0; i < pubkey.Length; i++)
87                         sb.Append (pubkey[i].ToString ("X2"));
88                 return sb.ToString ();
89         }
90 }
91
92 }