Ed25519PublicKeyParameters.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc8032;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  9. {
  10. public sealed class Ed25519PublicKeyParameters
  11. : AsymmetricKeyParameter
  12. {
  13. public static readonly int KeySize = Ed25519.PublicKeySize;
  14. private readonly byte[] data = new byte[KeySize];
  15. public Ed25519PublicKeyParameters(byte[] buf)
  16. : this(Validate(buf), 0)
  17. {
  18. }
  19. public Ed25519PublicKeyParameters(byte[] buf, int off)
  20. : base(false)
  21. {
  22. Array.Copy(buf, off, data, 0, KeySize);
  23. }
  24. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  25. public Ed25519PublicKeyParameters(ReadOnlySpan<byte> buf)
  26. : base(false)
  27. {
  28. if (buf.Length != KeySize)
  29. throw new ArgumentException("must have length " + KeySize, nameof(buf));
  30. buf.CopyTo(data);
  31. }
  32. #endif
  33. public Ed25519PublicKeyParameters(Stream input)
  34. : base(false)
  35. {
  36. if (KeySize != Streams.ReadFully(input, data))
  37. throw new EndOfStreamException("EOF encountered in middle of Ed25519 public key");
  38. }
  39. public void Encode(byte[] buf, int off)
  40. {
  41. Array.Copy(data, 0, buf, off, KeySize);
  42. }
  43. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  44. public void Encode(Span<byte> buf)
  45. {
  46. data.CopyTo(buf);
  47. }
  48. #endif
  49. public byte[] GetEncoded()
  50. {
  51. return Arrays.Clone(data);
  52. }
  53. private static byte[] Validate(byte[] buf)
  54. {
  55. if (buf.Length != KeySize)
  56. throw new ArgumentException("must have length " + KeySize, nameof(buf));
  57. return buf;
  58. }
  59. }
  60. }
  61. #pragma warning restore
  62. #endif