HashMembershipCondition.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // System.Security.Policy.HashMembershipCondition
  3. //
  4. // Author(s):
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2002 Jackson Harper, All rights reserved
  8. //
  9. using System.Text;
  10. using System.Reflection;
  11. using System.Security.Cryptography;
  12. namespace System.Security.Policy {
  13. public sealed class HashMembershipCondition : IMembershipCondition,
  14. ISecurityEncodable, ISecurityPolicyEncodable {
  15. private static readonly string XmlTag = "IMembershipCondition";
  16. private HashAlgorithm hash_algorithm;
  17. private byte[] hash_value;
  18. public HashMembershipCondition (HashAlgorithm hash_algorithm,
  19. byte[] hash_value)
  20. {
  21. if (hash_algorithm == null || hash_value == null)
  22. throw new ArgumentNullException ();
  23. this.hash_algorithm = hash_algorithm;
  24. this.hash_value = hash_value;
  25. }
  26. //
  27. // Public Properties
  28. //
  29. public HashAlgorithm HashAlgorithm {
  30. get { return hash_algorithm; }
  31. set {
  32. if (value == null)
  33. throw new ArgumentNullException ();
  34. hash_algorithm = value;
  35. }
  36. }
  37. public byte[] HashValue {
  38. get { return hash_value; }
  39. set {
  40. if (value == null)
  41. throw new ArgumentNullException ();
  42. hash_value = value;
  43. }
  44. }
  45. //
  46. // Public Methods
  47. //
  48. public bool Check (Evidence evidence)
  49. {
  50. if (evidence == null)
  51. throw new ArgumentNullException ();
  52. // Loop through evidence finding the first Hash object
  53. foreach (object obj in evidence) {
  54. Hash hash = obj as Hash;
  55. if (hash == null)
  56. continue;
  57. if (EqualsHashValue (hash.GenerateHash (hash_algorithm)))
  58. return true;
  59. break;
  60. }
  61. return false;
  62. }
  63. public IMembershipCondition Copy ()
  64. {
  65. return new HashMembershipCondition (hash_algorithm, hash_value);
  66. }
  67. public override bool Equals (object o)
  68. {
  69. HashMembershipCondition other;
  70. if (!(o is HashMembershipCondition))
  71. return false;
  72. other = (HashMembershipCondition)o;
  73. return (other.HashAlgorithm == hash_algorithm &&
  74. other.HashValue == hash_value);
  75. }
  76. public SecurityElement ToXml()
  77. {
  78. return ToXml (null);
  79. }
  80. public SecurityElement ToXml (PolicyLevel level)
  81. {
  82. SecurityElement se = new SecurityElement (XmlTag);
  83. Type type = this.GetType ();
  84. string classString = type.FullName + ", " + type.Assembly;
  85. se.AddAttribute ("class", classString);
  86. se.AddAttribute ("version", "1");
  87. se.AddAttribute ("HashValue", Encoding.Default.GetString (hash_value));
  88. se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
  89. return se;
  90. }
  91. public void FromXml (SecurityElement element)
  92. {
  93. FromXml (element, null);
  94. }
  95. public void FromXml (SecurityElement e,
  96. PolicyLevel level)
  97. {
  98. if (e == null)
  99. throw new ArgumentNullException ();
  100. if (e.Tag != XmlTag)
  101. throw new ArgumentException(
  102. "e","The Tag of SecurityElement must be " + XmlTag);
  103. string value = (string)e.Attributes["HashValue"];
  104. string algorithm = (string)e.Attributes["HashAlgorithm"];
  105. if (value == null || algorithm == null )
  106. throw new ArgumentException ();
  107. hash_value = Encoding.Default.GetBytes (value);
  108. hash_algorithm = (HashAlgorithm)Assembly.GetExecutingAssembly ().CreateInstance (algorithm);
  109. }
  110. [MonoTODO("This is not right")]
  111. public override int GetHashCode ()
  112. {
  113. return hash_value.GetHashCode ();
  114. }
  115. public override string ToString ()
  116. {
  117. StringBuilder builder = new StringBuilder ();
  118. Type alg_type = hash_algorithm.GetType ();
  119. builder.Append ("Hash -");
  120. builder.AppendFormat ("{0} {1}", alg_type.FullName,
  121. alg_type.Assembly);
  122. builder.AppendFormat (" = ", Encoding.Default.GetString (hash_value));
  123. return builder.ToString ();
  124. }
  125. //
  126. // Private Methods
  127. //
  128. private bool EqualsHashValue (byte[] value)
  129. {
  130. int len;
  131. if (value.Length != hash_value.Length)
  132. return false;
  133. len = value.Length;
  134. for (int i=0; i<len; i++ ) {
  135. if (value[i] != hash_value[i])
  136. return false;
  137. }
  138. return true;
  139. }
  140. }
  141. }