3 // Copyright (c) Microsoft Corporation. All rights reserved.
8 using System.Diagnostics.Contracts;
10 namespace System.Security.Cryptography {
12 /// Utility class to strongly type algorithms used with CNG. Since all CNG APIs which require an
13 /// algorithm name take the name as a string, we use this string wrapper class to specifically mark
14 /// which parameters are expected to be algorithms. We also provide a list of well known algorithm
15 /// names, which helps Intellisense users find a set of good algorithm names to use.
18 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
19 public sealed class CngAlgorithm : IEquatable<CngAlgorithm> {
20 private static volatile CngAlgorithm s_ecdhp256;
21 private static volatile CngAlgorithm s_ecdhp384;
22 private static volatile CngAlgorithm s_ecdhp521;
23 private static volatile CngAlgorithm s_ecdsap256;
24 private static volatile CngAlgorithm s_ecdsap384;
25 private static volatile CngAlgorithm s_ecdsap521;
26 private static volatile CngAlgorithm s_md5;
27 private static volatile CngAlgorithm s_sha1;
28 private static volatile CngAlgorithm s_sha256;
29 private static volatile CngAlgorithm s_sha384;
30 private static volatile CngAlgorithm s_sha512;
31 private static volatile CngAlgorithm s_rsa;
33 private string m_algorithm;
35 public CngAlgorithm(string algorithm) {
36 Contract.Ensures(!String.IsNullOrEmpty(m_algorithm));
38 if (algorithm == null) {
39 throw new ArgumentNullException("algorithm");
41 if (algorithm.Length == 0) {
42 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidAlgorithmName, algorithm), "algorithm");
45 m_algorithm = algorithm;
49 /// Name of the algorithm
51 public string Algorithm {
53 Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
58 public static bool operator==(CngAlgorithm left, CngAlgorithm right) {
59 if (Object.ReferenceEquals(left, null)) {
60 return Object.ReferenceEquals(right, null);
63 return left.Equals(right);
67 public static bool operator !=(CngAlgorithm left, CngAlgorithm right) {
68 if (Object.ReferenceEquals(left, null)) {
69 return !Object.ReferenceEquals(right, null);
72 return !left.Equals(right);
75 public override bool Equals(object obj) {
76 Contract.Assert(m_algorithm != null);
78 return Equals(obj as CngAlgorithm);
81 public bool Equals(CngAlgorithm other) {
82 if (Object.ReferenceEquals(other, null)) {
86 return m_algorithm.Equals(other.Algorithm);
89 public override int GetHashCode() {
90 Contract.Assert(m_algorithm != null);
91 return m_algorithm.GetHashCode();
94 public override string ToString() {
95 Contract.Assert(m_algorithm != null);
100 // Well known algorithms
103 public static CngAlgorithm Rsa {
105 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
107 s_rsa = new CngAlgorithm(BCryptNative.AlgorithmName.Rsa);
113 public static CngAlgorithm ECDiffieHellmanP256 {
115 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
117 if (s_ecdhp256 == null) {
118 s_ecdhp256 = new CngAlgorithm(BCryptNative.AlgorithmName.ECDHP256);
125 public static CngAlgorithm ECDiffieHellmanP384 {
127 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
129 if (s_ecdhp384 == null) {
130 s_ecdhp384 = new CngAlgorithm(BCryptNative.AlgorithmName.ECDHP384);
137 public static CngAlgorithm ECDiffieHellmanP521 {
139 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
141 if (s_ecdhp521 == null) {
142 s_ecdhp521 = new CngAlgorithm(BCryptNative.AlgorithmName.ECDHP521);
149 public static CngAlgorithm ECDsaP256 {
151 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
153 if (s_ecdsap256 == null) {
154 s_ecdsap256 = new CngAlgorithm(BCryptNative.AlgorithmName.ECDsaP256);
161 public static CngAlgorithm ECDsaP384 {
163 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
165 if (s_ecdsap384 == null) {
166 s_ecdsap384 = new CngAlgorithm(BCryptNative.AlgorithmName.ECDsaP384);
173 public static CngAlgorithm ECDsaP521 {
175 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
177 if (s_ecdsap521 == null) {
178 s_ecdsap521 = new CngAlgorithm(BCryptNative.AlgorithmName.ECDsaP521);
185 public static CngAlgorithm MD5 {
187 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
190 s_md5 = new CngAlgorithm(BCryptNative.AlgorithmName.MD5);
197 public static CngAlgorithm Sha1 {
199 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
201 if (s_sha1 == null) {
202 s_sha1 = new CngAlgorithm(BCryptNative.AlgorithmName.Sha1);
209 public static CngAlgorithm Sha256 {
211 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
213 if (s_sha256 == null) {
214 s_sha256 = new CngAlgorithm(BCryptNative.AlgorithmName.Sha256);
221 public static CngAlgorithm Sha384 {
223 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
225 if (s_sha384 == null) {
226 s_sha384 = new CngAlgorithm(BCryptNative.AlgorithmName.Sha384);
233 public static CngAlgorithm Sha512 {
235 Contract.Ensures(Contract.Result<CngAlgorithm>() != null);
237 if (s_sha512 == null) {
238 s_sha512 = new CngAlgorithm(BCryptNative.AlgorithmName.Sha512);
246 public static CngAlgorithm ECDiffieHellman {
248 throw new NotImplementedException ();
252 public static CngAlgorithm ECDsa {
254 throw new NotImplementedException ();