SRP6VerifierGenerator.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.Math;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Srp
  7. {
  8. /**
  9. * Generates new SRP verifier for user
  10. */
  11. public class Srp6VerifierGenerator
  12. {
  13. protected BigInteger N;
  14. protected BigInteger g;
  15. protected IDigest digest;
  16. public Srp6VerifierGenerator()
  17. {
  18. }
  19. /**
  20. * Initialises generator to create new verifiers
  21. * @param N The safe prime to use (see DHParametersGenerator)
  22. * @param g The group parameter to use (see DHParametersGenerator)
  23. * @param digest The digest to use. The same digest type will need to be used later for the actual authentication
  24. * attempt. Also note that the final session key size is dependent on the chosen digest.
  25. */
  26. public virtual void Init(BigInteger N, BigInteger g, IDigest digest)
  27. {
  28. this.N = N;
  29. this.g = g;
  30. this.digest = digest;
  31. }
  32. public virtual void Init(Srp6GroupParameters group, IDigest digest)
  33. {
  34. Init(group.N, group.G, digest);
  35. }
  36. /**
  37. * Creates a new SRP verifier
  38. * @param salt The salt to use, generally should be large and random
  39. * @param identity The user's identifying information (eg. username)
  40. * @param password The user's password
  41. * @return A new verifier for use in future SRP authentication
  42. */
  43. public virtual BigInteger GenerateVerifier(byte[] salt, byte[] identity, byte[] password)
  44. {
  45. BigInteger x = Srp6Utilities.CalculateX(digest, N, salt, identity, password);
  46. return g.ModPow(x, N);
  47. }
  48. }
  49. }
  50. #pragma warning restore
  51. #endif