DHParametersGenerator.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  8. {
  9. public class DHParametersGenerator
  10. {
  11. private int size;
  12. private int certainty;
  13. private SecureRandom random;
  14. public virtual void Init(
  15. int size,
  16. int certainty,
  17. SecureRandom random)
  18. {
  19. this.size = size;
  20. this.certainty = certainty;
  21. this.random = random;
  22. }
  23. /**
  24. * which Generates the p and g values from the given parameters,
  25. * returning the DHParameters object.
  26. * <p>
  27. * Note: can take a while...</p>
  28. */
  29. public virtual DHParameters GenerateParameters()
  30. {
  31. //
  32. // find a safe prime p where p = 2*q + 1, where p and q are prime.
  33. //
  34. BigInteger[] safePrimes = DHParametersHelper.GenerateSafePrimes(size, certainty, random);
  35. BigInteger p = safePrimes[0];
  36. BigInteger q = safePrimes[1];
  37. BigInteger g = DHParametersHelper.SelectGenerator(p, q, random);
  38. return new DHParameters(p, g, q, BigInteger.Two, null);
  39. }
  40. }
  41. }
  42. #pragma warning restore
  43. #endif