CngKeyBlobFormat.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // System.Security.Cryptography.CngKeyBlobFormat
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. // Copyright (C) 2011 Juho Vähä-Herttua
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. namespace System.Security.Cryptography {
  31. // note: CNG stands for "Cryptography API: Next Generation"
  32. [Serializable]
  33. public sealed class CngKeyBlobFormat : IEquatable<CngKeyBlobFormat> {
  34. private string m_format;
  35. public CngKeyBlobFormat (string format)
  36. {
  37. if (format == null)
  38. throw new ArgumentNullException ("format");
  39. if (format.Length == 0)
  40. throw new ArgumentException ("format");
  41. m_format = format;
  42. }
  43. public string Format {
  44. get { return m_format; }
  45. }
  46. public bool Equals (CngKeyBlobFormat other)
  47. {
  48. if (other == null)
  49. return false;
  50. return m_format == other.m_format;
  51. }
  52. public override bool Equals (object obj)
  53. {
  54. return Equals (obj as CngKeyBlobFormat);
  55. }
  56. public override int GetHashCode ()
  57. {
  58. return m_format.GetHashCode ();
  59. }
  60. public override string ToString ()
  61. {
  62. return m_format;
  63. }
  64. // static
  65. private static CngKeyBlobFormat opaqueTransportBlob;
  66. private static CngKeyBlobFormat genericPrivateBlob;
  67. private static CngKeyBlobFormat genericPublicBlob;
  68. private static CngKeyBlobFormat eccPrivateBlob;
  69. private static CngKeyBlobFormat eccPublicBlob;
  70. private static CngKeyBlobFormat pkcs8PrivateBlob;
  71. public static CngKeyBlobFormat OpaqueTransportBlob {
  72. get {
  73. if (opaqueTransportBlob == null)
  74. opaqueTransportBlob = new CngKeyBlobFormat ("OpaqueTransport");
  75. return opaqueTransportBlob;
  76. }
  77. }
  78. public static CngKeyBlobFormat GenericPrivateBlob {
  79. get {
  80. if (genericPrivateBlob == null)
  81. genericPrivateBlob = new CngKeyBlobFormat ("PRIVATEBLOB");
  82. return genericPrivateBlob;
  83. }
  84. }
  85. public static CngKeyBlobFormat GenericPublicBlob {
  86. get {
  87. if (genericPublicBlob == null)
  88. genericPublicBlob = new CngKeyBlobFormat ("PUBLICBLOB");
  89. return genericPublicBlob;
  90. }
  91. }
  92. public static CngKeyBlobFormat EccPrivateBlob {
  93. get {
  94. if (eccPrivateBlob == null)
  95. eccPrivateBlob = new CngKeyBlobFormat ("ECCPRIVATEBLOB");
  96. return eccPrivateBlob;
  97. }
  98. }
  99. public static CngKeyBlobFormat EccPublicBlob {
  100. get {
  101. if (eccPublicBlob == null)
  102. eccPublicBlob = new CngKeyBlobFormat ("ECCPUBLICBLOB");
  103. return eccPublicBlob;
  104. }
  105. }
  106. public static CngKeyBlobFormat Pkcs8PrivateBlob {
  107. get {
  108. if (pkcs8PrivateBlob == null)
  109. pkcs8PrivateBlob = new CngKeyBlobFormat ("PKCS8_PRIVATEKEY");
  110. return pkcs8PrivateBlob;
  111. }
  112. }
  113. public static bool operator == (CngKeyBlobFormat left, CngKeyBlobFormat right)
  114. {
  115. if ((object)left == null)
  116. return ((object)right == null);
  117. return left.Equals (right);
  118. }
  119. public static bool operator != (CngKeyBlobFormat left, CngKeyBlobFormat right)
  120. {
  121. if ((object)left == null)
  122. return ((object)right != null);
  123. return !left.Equals (right);
  124. }
  125. }
  126. }