CngAlgorithm.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.Security.Cryptography.CngAlgorithm
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. namespace System.Security.Cryptography {
  30. // note: CNG stands for "Cryptography API: Next Generation"
  31. [Serializable]
  32. public sealed class CngAlgorithm : IEquatable<CngAlgorithm> {
  33. private string algo;
  34. public CngAlgorithm (string algorithm)
  35. {
  36. if (algorithm == null)
  37. throw new ArgumentNullException ("algorithm");
  38. if (algorithm.Length == 0)
  39. throw new ArgumentException ("algorithm");
  40. algo = algorithm;
  41. }
  42. public string Algorithm {
  43. get { return algo; }
  44. }
  45. public bool Equals (CngAlgorithm other)
  46. {
  47. if (other == null)
  48. return false;
  49. return algo == other.algo;
  50. }
  51. public override bool Equals (object obj)
  52. {
  53. return Equals (obj as CngAlgorithm);
  54. }
  55. public override int GetHashCode ()
  56. {
  57. return algo.GetHashCode ();
  58. }
  59. public override string ToString ()
  60. {
  61. return algo;
  62. }
  63. // static
  64. static CngAlgorithm dh256;
  65. static CngAlgorithm dh384;
  66. static CngAlgorithm dh521;
  67. static CngAlgorithm dsa256;
  68. static CngAlgorithm dsa384;
  69. static CngAlgorithm dsa521;
  70. static CngAlgorithm md5;
  71. static CngAlgorithm sha1;
  72. static CngAlgorithm sha256;
  73. static CngAlgorithm sha384;
  74. static CngAlgorithm sha512;
  75. public static CngAlgorithm ECDiffieHellmanP256 {
  76. get {
  77. if (dh256 == null)
  78. dh256 = new CngAlgorithm ("ECDH_P256");
  79. return dh256;
  80. }
  81. }
  82. public static CngAlgorithm ECDiffieHellmanP384 {
  83. get {
  84. if (dh384 == null)
  85. dh384 = new CngAlgorithm ("ECDH_P384");
  86. return dh384;
  87. }
  88. }
  89. public static CngAlgorithm ECDiffieHellmanP521 {
  90. get {
  91. if (dh521 == null)
  92. dh521 = new CngAlgorithm ("ECDH_P521");
  93. return dh521;
  94. }
  95. }
  96. public static CngAlgorithm ECDsaP256 {
  97. get {
  98. if (dsa256 == null)
  99. dsa256 = new CngAlgorithm ("ECDSA_P256");
  100. return dsa256;
  101. }
  102. }
  103. public static CngAlgorithm ECDsaP384 {
  104. get {
  105. if (dsa384 == null)
  106. dsa384 = new CngAlgorithm ("ECDSA_P384");
  107. return dsa384;
  108. }
  109. }
  110. public static CngAlgorithm ECDsaP521 {
  111. get {
  112. if (dsa521 == null)
  113. dsa521 = new CngAlgorithm ("ECDSA_P521");
  114. return dsa521;
  115. }
  116. }
  117. public static CngAlgorithm MD5 {
  118. get {
  119. if (md5 == null)
  120. md5 = new CngAlgorithm ("MD5");
  121. return md5;
  122. }
  123. }
  124. public static CngAlgorithm Sha1 {
  125. get {
  126. if (sha1 == null)
  127. sha1 = new CngAlgorithm ("SHA1");
  128. return sha1;
  129. }
  130. }
  131. public static CngAlgorithm Sha256 {
  132. get {
  133. if (sha256 == null)
  134. sha256 = new CngAlgorithm ("SHA256");
  135. return sha256;
  136. }
  137. }
  138. public static CngAlgorithm Sha384 {
  139. get {
  140. if (sha384 == null)
  141. sha384 = new CngAlgorithm ("SHA384");
  142. return sha384;
  143. }
  144. }
  145. public static CngAlgorithm Sha512 {
  146. get {
  147. if (sha512 == null)
  148. sha512 = new CngAlgorithm ("SHA512");
  149. return sha512;
  150. }
  151. }
  152. public static bool operator == (CngAlgorithm left, CngAlgorithm right)
  153. {
  154. if ((object)left == null)
  155. return ((object)right == null);
  156. if ((object)right == null)
  157. return false;
  158. return left.algo == right.algo;
  159. }
  160. public static bool operator != (CngAlgorithm left, CngAlgorithm right)
  161. {
  162. if ((object)left == null)
  163. return ((object)right != null);
  164. if ((object)right == null)
  165. return true;
  166. return left.algo != right.algo;
  167. }
  168. }
  169. }