Key.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// Base class for asymmetric cipher algorithms
  11. /// </summary>
  12. public abstract class Key
  13. {
  14. /// <summary>
  15. /// Specifies array of big integers that represent private key
  16. /// </summary>
  17. protected BigInteger[] _privateKey;
  18. /// <summary>
  19. /// Gets the key specific digital signature.
  20. /// </summary>
  21. protected abstract DigitalSignature DigitalSignature { get; }
  22. /// <summary>
  23. /// Gets or sets the public key.
  24. /// </summary>
  25. /// <value>
  26. /// The public.
  27. /// </value>
  28. public abstract BigInteger[] Public { get; set; }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="Key"/> class.
  31. /// </summary>
  32. /// <param name="data">DER encoded private key data.</param>
  33. public Key(byte[] data)
  34. {
  35. if (data == null)
  36. throw new ArgumentNullException("data");
  37. var der = new DerData(data);
  38. var version = der.ReadBigInteger();
  39. var keys = new List<BigInteger>();
  40. while (!der.IsEndOfData)
  41. {
  42. keys.Add(der.ReadBigInteger());
  43. }
  44. this._privateKey = keys.ToArray();
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="Key"/> class.
  48. /// </summary>
  49. public Key()
  50. : base()
  51. {
  52. }
  53. /// <summary>
  54. /// Signs the specified data with the key.
  55. /// </summary>
  56. /// <param name="data">The data to sign.</param>
  57. /// <returns>Signed data.</returns>
  58. public byte[] Sign(byte[] data)
  59. {
  60. return this.DigitalSignature.Sign(data);
  61. }
  62. /// <summary>
  63. /// Verifies the signature.
  64. /// </summary>
  65. /// <param name="data">The data to verify.</param>
  66. /// <param name="signature">The signature to verify against.</param>
  67. /// <returns></returns>
  68. public bool VerifySignature(byte[] data, byte[] signature)
  69. {
  70. return this.DigitalSignature.Verify(data, signature);
  71. }
  72. }
  73. }