AsymmetricAlgorithm.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // System.Security.Cryptography AsymmetricAlgorithm Class implementation
  3. //
  4. // Authors:
  5. // Thomas Neidhart ([email protected])
  6. //
  7. using System;
  8. namespace System.Security.Cryptography {
  9. /// <summary>
  10. /// Abstract base class for all cryptographic asymmetric algorithms.
  11. /// Available algorithms include:
  12. /// RSA, DSA
  13. /// </summary>
  14. public abstract class AsymmetricAlgorithm {
  15. protected int KeySizeValue; // The size of the secret key used by the symmetric algorithm in bits.
  16. protected KeySizes[] LegalKeySizesValue; // Specifies the key sizes that are supported by the symmetric algorithm.
  17. /// <summary>
  18. /// Called from constructor of derived class.
  19. /// </summary>
  20. protected AsymmetricAlgorithm () {
  21. throw new CryptographicException();
  22. }
  23. /// <summary>
  24. /// Gets the key exchange algorithm
  25. /// </summary>
  26. public abstract string KeyExchangeAlgorithm {get;}
  27. /// <summary>
  28. /// Gets or sets the actual key size
  29. /// </summary>
  30. public virtual int KeySize {
  31. get {
  32. return this.KeySizeValue;
  33. }
  34. set {
  35. if (!IsLegalKeySize(this.LegalKeySizesValue, value))
  36. throw new CryptographicException("key size not supported by algorithm");
  37. this.KeySizeValue = value;
  38. }
  39. }
  40. /// <summary>
  41. /// Gets all legal key sizes
  42. /// </summary>
  43. public virtual KeySizes[] LegalKeySizes {
  44. get {
  45. return this.LegalKeySizesValue;
  46. }
  47. }
  48. /// <summary>
  49. /// Gets the signature algorithm
  50. /// </summary>
  51. public abstract string SignatureAlgorithm {get;}
  52. /// <summary>
  53. /// Reconstructs the AsymmetricAlgorithm Object from an XML-string
  54. /// </summary>
  55. public abstract void FromXmlString(string xmlString);
  56. /// <summary>
  57. /// Returns an XML string representation the current AsymmetricAlgorithm object
  58. /// </summary>
  59. public abstract string ToXmlString(bool includePrivateParameters);
  60. private bool IsLegalKeySize(KeySizes[] LegalKeys, int Size) {
  61. foreach (KeySizes LegalKeySize in LegalKeys) {
  62. for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {
  63. if (i == Size)
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. /// <summary>
  70. /// Checks wether the given keyLength is valid for the current algorithm
  71. /// </summary>
  72. /// <param name="bitLength">the given keyLength</param>
  73. public bool ValidKeySize(int bitLength) {
  74. return IsLegalKeySize(LegalKeySizesValue, bitLength);
  75. }
  76. /// <summary>
  77. /// Creates the default implementation of the default asymmetric algorithm (RSA).
  78. /// </summary>
  79. public static AsymmetricAlgorithm Create () {
  80. return RSA.Create();;
  81. }
  82. /// <summary>
  83. /// Creates a specific implementation of the given asymmetric algorithm.
  84. /// </summary>
  85. /// <param name="algName">the given algorithm</param>
  86. [MonoTODO]
  87. public static AsymmetricAlgorithm Create (string algName) {
  88. // TODO: use reflection to create a new instance of the given algorithm
  89. return null;
  90. }
  91. }
  92. }