GenericSigner.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  8. {
  9. public class GenericSigner
  10. : ISigner
  11. {
  12. private readonly IAsymmetricBlockCipher engine;
  13. private readonly IDigest digest;
  14. private bool forSigning;
  15. public GenericSigner(
  16. IAsymmetricBlockCipher engine,
  17. IDigest digest)
  18. {
  19. this.engine = engine;
  20. this.digest = digest;
  21. }
  22. public virtual string AlgorithmName
  23. {
  24. get { return "Generic(" + engine.AlgorithmName + "/" + digest.AlgorithmName + ")"; }
  25. }
  26. /**
  27. * initialise the signer for signing or verification.
  28. *
  29. * @param forSigning
  30. * true if for signing, false otherwise
  31. * @param parameters
  32. * necessary parameters.
  33. */
  34. public virtual void Init(bool forSigning, ICipherParameters parameters)
  35. {
  36. this.forSigning = forSigning;
  37. AsymmetricKeyParameter k;
  38. if (parameters is ParametersWithRandom)
  39. {
  40. k = (AsymmetricKeyParameter)((ParametersWithRandom)parameters).Parameters;
  41. }
  42. else
  43. {
  44. k = (AsymmetricKeyParameter)parameters;
  45. }
  46. if (forSigning && !k.IsPrivate)
  47. throw new InvalidKeyException("Signing requires private key.");
  48. if (!forSigning && k.IsPrivate)
  49. throw new InvalidKeyException("Verification requires public key.");
  50. Reset();
  51. engine.Init(forSigning, parameters);
  52. }
  53. /**
  54. * update the internal digest with the byte b
  55. */
  56. public virtual void Update(byte input)
  57. {
  58. digest.Update(input);
  59. }
  60. /**
  61. * update the internal digest with the byte array in
  62. */
  63. public virtual void BlockUpdate(byte[] input, int inOff, int length)
  64. {
  65. digest.BlockUpdate(input, inOff, length);
  66. }
  67. /**
  68. * Generate a signature for the message we've been loaded with using the key
  69. * we were initialised with.
  70. */
  71. public virtual byte[] GenerateSignature()
  72. {
  73. if (!forSigning)
  74. throw new InvalidOperationException("GenericSigner not initialised for signature generation.");
  75. byte[] hash = new byte[digest.GetDigestSize()];
  76. digest.DoFinal(hash, 0);
  77. return engine.ProcessBlock(hash, 0, hash.Length);
  78. }
  79. /**
  80. * return true if the internal state represents the signature described in
  81. * the passed in array.
  82. */
  83. public virtual bool VerifySignature(byte[] signature)
  84. {
  85. if (forSigning)
  86. throw new InvalidOperationException("GenericSigner not initialised for verification");
  87. byte[] hash = new byte[digest.GetDigestSize()];
  88. digest.DoFinal(hash, 0);
  89. try
  90. {
  91. byte[] sig = engine.ProcessBlock(signature, 0, signature.Length);
  92. // Extend with leading zeroes to match the digest size, if necessary.
  93. if (sig.Length < hash.Length)
  94. {
  95. byte[] tmp = new byte[hash.Length];
  96. Array.Copy(sig, 0, tmp, tmp.Length - sig.Length, sig.Length);
  97. sig = tmp;
  98. }
  99. return Arrays.ConstantTimeAreEqual(sig, hash);
  100. }
  101. catch (Exception)
  102. {
  103. return false;
  104. }
  105. }
  106. public virtual void Reset()
  107. {
  108. digest.Reset();
  109. }
  110. }
  111. }
  112. #pragma warning restore
  113. #endif