DsaKey.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Security.Cryptography;
  7. namespace Renci.SshNet.Security
  8. {
  9. /// <summary>
  10. /// Contains DSA private and public key
  11. /// </summary>
  12. public class DsaKey : Key, IDisposable
  13. {
  14. /// <summary>
  15. /// Gets the P.
  16. /// </summary>
  17. public BigInteger P
  18. {
  19. get
  20. {
  21. return this._privateKey[0];
  22. }
  23. }
  24. /// <summary>
  25. /// Gets the Q.
  26. /// </summary>
  27. public BigInteger Q
  28. {
  29. get
  30. {
  31. return this._privateKey[1];
  32. }
  33. }
  34. /// <summary>
  35. /// Gets the G.
  36. /// </summary>
  37. public BigInteger G
  38. {
  39. get
  40. {
  41. return this._privateKey[2];
  42. }
  43. }
  44. /// <summary>
  45. /// Gets public key Y.
  46. /// </summary>
  47. public BigInteger Y
  48. {
  49. get
  50. {
  51. return this._privateKey[3];
  52. }
  53. }
  54. /// <summary>
  55. /// Gets private key X.
  56. /// </summary>
  57. public BigInteger X
  58. {
  59. get
  60. {
  61. return this._privateKey[4];
  62. }
  63. }
  64. private DsaDigitalSignature _digitalSignature;
  65. /// <summary>
  66. /// Gets the digital signature.
  67. /// </summary>
  68. protected override DigitalSignature DigitalSignature
  69. {
  70. get
  71. {
  72. if (this._digitalSignature == null)
  73. {
  74. this._digitalSignature = new DsaDigitalSignature(this);
  75. }
  76. return this._digitalSignature;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets or sets the public.
  81. /// </summary>
  82. /// <value>
  83. /// The public.
  84. /// </value>
  85. public override BigInteger[] Public
  86. {
  87. get
  88. {
  89. return new BigInteger[] { this.P, this.Q, this.G, this.Y };
  90. }
  91. set
  92. {
  93. if (value.Length != 4)
  94. throw new InvalidOperationException("Invalid public key.");
  95. this._privateKey = value;
  96. }
  97. }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="DsaKey"/> class.
  100. /// </summary>
  101. public DsaKey()
  102. : base()
  103. {
  104. this._privateKey = new BigInteger[5];
  105. }
  106. /// <summary>
  107. /// Initializes a new instance of the <see cref="DsaKey"/> class.
  108. /// </summary>
  109. /// <param name="data">DER encoded private key data.</param>
  110. public DsaKey(byte[] data)
  111. : base(data)
  112. {
  113. if (this._privateKey.Length != 5)
  114. throw new InvalidOperationException("Invalid private key.");
  115. }
  116. #region IDisposable Members
  117. private bool _isDisposed = false;
  118. /// <summary>
  119. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  120. /// </summary>
  121. public void Dispose()
  122. {
  123. Dispose(true);
  124. GC.SuppressFinalize(this);
  125. }
  126. /// <summary>
  127. /// Releases unmanaged and - optionally - managed resources
  128. /// </summary>
  129. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  130. protected virtual void Dispose(bool disposing)
  131. {
  132. // Check to see if Dispose has already been called.
  133. if (!this._isDisposed)
  134. {
  135. // If disposing equals true, dispose all managed
  136. // and unmanaged ResourceMessages.
  137. if (disposing)
  138. {
  139. // Dispose managed ResourceMessages.
  140. if (this._digitalSignature != null)
  141. {
  142. this._digitalSignature.Dispose();
  143. this._digitalSignature = null;
  144. }
  145. }
  146. // Note disposing has been done.
  147. this._isDisposed = true;
  148. }
  149. }
  150. /// <summary>
  151. /// Releases unmanaged resources and performs other cleanup operations before the
  152. /// <see cref="SshCommand"/> is reclaimed by garbage collection.
  153. /// </summary>
  154. ~DsaKey()
  155. {
  156. // Do not re-create Dispose clean-up code here.
  157. // Calling Dispose(false) is optimal in terms of
  158. // readability and maintainability.
  159. Dispose(false);
  160. }
  161. #endregion
  162. }
  163. }