StrongNamePublicKeyBlob.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // StrongNamePublicKeyBlob.cs: Strong Name Public Key Blob
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.Text;
  11. namespace System.Security.Permissions {
  12. [Serializable]
  13. public sealed class StrongNamePublicKeyBlob {
  14. internal byte[] pubkey;
  15. public StrongNamePublicKeyBlob (byte[] publicKey)
  16. {
  17. if (publicKey == null)
  18. throw new ArgumentNullException ("publicKey");
  19. // Note: No sanity check ?
  20. pubkey = publicKey;
  21. }
  22. public override bool Equals (object obj)
  23. {
  24. bool result = (obj is StrongNamePublicKeyBlob);
  25. if (result) {
  26. StrongNamePublicKeyBlob snpkb = (obj as StrongNamePublicKeyBlob);
  27. result = (pubkey.Length == snpkb.pubkey.Length);
  28. if (result) {
  29. for (int i = 0; i < pubkey.Length; i++) {
  30. if (pubkey[i] != snpkb.pubkey[i])
  31. return false;
  32. }
  33. }
  34. }
  35. return result;
  36. }
  37. // LAMESPEC: non standard get hash code - (a) Why ??? (b) How ???
  38. // It seems to be the first four bytes of the public key data
  39. // which seems like non sense as all valid public key will have the same header ?
  40. public override int GetHashCode ()
  41. {
  42. int hash = 0;
  43. int i = 0;
  44. // a BAD public key can be less than 4 bytes
  45. int n = Math.Min (pubkey.Length, 4);
  46. while (i < n)
  47. hash = (hash << 8) + pubkey [i++];
  48. return hash;
  49. }
  50. public override string ToString ()
  51. {
  52. StringBuilder sb = new StringBuilder ();
  53. for (int i=0; i < pubkey.Length; i++)
  54. sb.Append (pubkey[i].ToString ("X2"));
  55. return sb.ToString ();
  56. }
  57. }
  58. }