RsaPrivateCrtKeyParameters.cs 3.4 KB

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