CngProperty.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // System.Security.Cryptography.CngProperty
  3. //
  4. // Copyright (C) 2011 Juho Vähä-Herttua
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining
  7. // a copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to
  11. // permit persons to whom the Software is furnished to do so, subject to
  12. // the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. using System;
  26. namespace System.Security.Cryptography {
  27. // note: CNG stands for "Cryptography API: Next Generation"
  28. public struct CngProperty : IEquatable<CngProperty> {
  29. private string name;
  30. private byte[] val;
  31. private CngPropertyOptions opts;
  32. public CngProperty(string name, byte[] value, CngPropertyOptions options)
  33. {
  34. if (name == null)
  35. throw new ArgumentNullException("name");
  36. this.name = name;
  37. this.val = (value != null) ? (byte[]) value.Clone () : null;
  38. this.opts = options;
  39. }
  40. public string Name {
  41. get { return name; }
  42. }
  43. public CngPropertyOptions Options {
  44. get { return opts; }
  45. }
  46. public byte[] GetValue ()
  47. {
  48. if (val == null)
  49. return null;
  50. return (byte[]) val.Clone ();
  51. }
  52. public bool Equals (CngProperty other)
  53. {
  54. if (this.name != other.name || this.opts != other.opts)
  55. return false;
  56. if (this.val == null && other.val == null)
  57. return true;
  58. if (this.val == null || other.val == null)
  59. return false;
  60. if (this.val.Length != other.val.Length)
  61. return false;
  62. for (int i=0; i<val.Length; i++) {
  63. if (this.val[i] != other.val[i])
  64. return false;
  65. }
  66. return true;
  67. }
  68. public override bool Equals (object obj)
  69. {
  70. if (obj == null)
  71. return false;
  72. if (!(obj is CngProperty))
  73. return false;
  74. return Equals ((CngProperty) obj);
  75. }
  76. public override int GetHashCode ()
  77. {
  78. int ret = name.GetHashCode () ^ opts.GetHashCode ();
  79. if (val == null)
  80. return ret;
  81. for (int i=0; i<val.Length; i++) {
  82. // Handle each 4 bytes of byte array as a little-endian
  83. // integer and XOR it with the resulting hash code value
  84. ret ^= val[i] << 8*(i % 4);
  85. }
  86. return ret;
  87. }
  88. // static
  89. public static bool operator == (CngProperty left, CngProperty right)
  90. {
  91. return left.Equals (right);
  92. }
  93. public static bool operator != (CngProperty left, CngProperty right)
  94. {
  95. return !left.Equals (right);
  96. }
  97. }
  98. }