RsaPrivateCrtKeyParameters.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto;
  4. using Org.BouncyCastle.Math;
  5. namespace Org.BouncyCastle.Crypto.Parameters
  6. {
  7. public class RsaPrivateCrtKeyParameters
  8. : RsaKeyParameters
  9. {
  10. private readonly BigInteger e, p, q, dP, dQ, qInv;
  11. public RsaPrivateCrtKeyParameters(
  12. BigInteger modulus,
  13. BigInteger publicExponent,
  14. BigInteger privateExponent,
  15. BigInteger p,
  16. BigInteger q,
  17. BigInteger dP,
  18. BigInteger dQ,
  19. BigInteger qInv)
  20. : base(true, modulus, privateExponent)
  21. {
  22. ValidateValue(publicExponent, "publicExponent", "exponent");
  23. ValidateValue(p, "p", "P value");
  24. ValidateValue(q, "q", "Q value");
  25. ValidateValue(dP, "dP", "DP value");
  26. ValidateValue(dQ, "dQ", "DQ value");
  27. ValidateValue(qInv, "qInv", "InverseQ value");
  28. this.e = publicExponent;
  29. this.p = p;
  30. this.q = q;
  31. this.dP = dP;
  32. this.dQ = dQ;
  33. this.qInv = qInv;
  34. }
  35. public BigInteger PublicExponent
  36. {
  37. get { return e; }
  38. }
  39. public BigInteger P
  40. {
  41. get { return p; }
  42. }
  43. public BigInteger Q
  44. {
  45. get { return q; }
  46. }
  47. public BigInteger DP
  48. {
  49. get { return dP; }
  50. }
  51. public BigInteger DQ
  52. {
  53. get { return dQ; }
  54. }
  55. public BigInteger QInv
  56. {
  57. get { return qInv; }
  58. }
  59. public override bool Equals(
  60. object obj)
  61. {
  62. if (obj == this)
  63. return true;
  64. RsaPrivateCrtKeyParameters kp = obj as RsaPrivateCrtKeyParameters;
  65. if (kp == null)
  66. return false;
  67. return kp.DP.Equals(dP)
  68. && kp.DQ.Equals(dQ)
  69. && kp.Exponent.Equals(this.Exponent)
  70. && kp.Modulus.Equals(this.Modulus)
  71. && kp.P.Equals(p)
  72. && kp.Q.Equals(q)
  73. && kp.PublicExponent.Equals(e)
  74. && kp.QInv.Equals(qInv);
  75. }
  76. public override int GetHashCode()
  77. {
  78. return DP.GetHashCode() ^ DQ.GetHashCode() ^ Exponent.GetHashCode() ^ Modulus.GetHashCode()
  79. ^ P.GetHashCode() ^ Q.GetHashCode() ^ PublicExponent.GetHashCode() ^ QInv.GetHashCode();
  80. }
  81. private static void ValidateValue(BigInteger x, string name, string desc)
  82. {
  83. if (x == null)
  84. throw new ArgumentNullException(name);
  85. if (x.SignValue <= 0)
  86. throw new ArgumentException("Not a valid RSA " + desc, name);
  87. }
  88. }
  89. }
  90. #endif