2 // StrongNamePublicKeyBlob.cs: Strong Name Public Key Blob
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
13 namespace System.Security.Permissions {
16 public sealed class StrongNamePublicKeyBlob {
18 internal byte[] pubkey;
20 public StrongNamePublicKeyBlob (byte[] publicKey)
22 if (publicKey == null)
23 throw new ArgumentNullException ("publicKey");
24 // Note: No sanity check ?
28 internal static StrongNamePublicKeyBlob FromString (string s)
30 int length = s.Length / 2;
32 byte [] array = new byte [length];
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);
40 return new StrongNamePublicKeyBlob (array);
43 static byte CharToByte (char c)
45 char ch = Char.ToLower (c);
47 if (Char.IsDigit (ch))
48 return (byte) (ch - '0');
50 return (byte) (ch - 'a' + 10);
53 public override bool Equals (object obj)
55 bool result = (obj is StrongNamePublicKeyBlob);
57 StrongNamePublicKeyBlob snpkb = (obj as StrongNamePublicKeyBlob);
58 result = (pubkey.Length == snpkb.pubkey.Length);
60 for (int i = 0; i < pubkey.Length; i++) {
61 if (pubkey[i] != snpkb.pubkey[i])
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 ()
76 // a BAD public key can be less than 4 bytes
77 int n = Math.Min (pubkey.Length, 4);
79 hash = (hash << 8) + pubkey [i++];
83 public override string ToString ()
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 ();