X25519PublicKeyParameters.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc7748;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  9. {
  10. public sealed class X25519PublicKeyParameters
  11. : AsymmetricKeyParameter
  12. {
  13. public static readonly int KeySize = X25519.PointSize;
  14. private readonly byte[] data = new byte[KeySize];
  15. public X25519PublicKeyParameters(byte[] buf)
  16. : this(Validate(buf), 0)
  17. {
  18. }
  19. public X25519PublicKeyParameters(byte[] buf, int off)
  20. : base(false)
  21. {
  22. Array.Copy(buf, off, data, 0, KeySize);
  23. }
  24. public X25519PublicKeyParameters(Stream input)
  25. : base(false)
  26. {
  27. if (KeySize != Streams.ReadFully(input, data))
  28. throw new EndOfStreamException("EOF encountered in middle of X25519 public key");
  29. }
  30. public void Encode(byte[] buf, int off)
  31. {
  32. Array.Copy(data, 0, buf, off, KeySize);
  33. }
  34. public byte[] GetEncoded()
  35. {
  36. return Arrays.Clone(data);
  37. }
  38. private static byte[] Validate(byte[] buf)
  39. {
  40. if (buf.Length != KeySize)
  41. throw new ArgumentException("must have length " + KeySize, "buf");
  42. return buf;
  43. }
  44. }
  45. }
  46. #pragma warning restore
  47. #endif