X9ECPoint.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using Org.BouncyCastle.Math.EC;
  3. using Org.BouncyCastle.Utilities;
  4. namespace Org.BouncyCastle.Asn1.X9
  5. {
  6. /**
  7. * class for describing an ECPoint as a Der object.
  8. */
  9. public class X9ECPoint
  10. : Asn1Encodable
  11. {
  12. private readonly Asn1OctetString encoding;
  13. private ECCurve c;
  14. private ECPoint p;
  15. public X9ECPoint(ECPoint p)
  16. : this(p, false)
  17. {
  18. }
  19. public X9ECPoint(ECPoint p, bool compressed)
  20. {
  21. this.p = p.Normalize();
  22. this.encoding = new DerOctetString(p.GetEncoded(compressed));
  23. }
  24. public X9ECPoint(ECCurve c, byte[] encoding)
  25. {
  26. this.c = c;
  27. this.encoding = new DerOctetString(Arrays.Clone(encoding));
  28. }
  29. public X9ECPoint(ECCurve c, Asn1OctetString s)
  30. : this(c, s.GetOctets())
  31. {
  32. }
  33. public byte[] GetPointEncoding()
  34. {
  35. return Arrays.Clone(encoding.GetOctets());
  36. }
  37. public ECPoint Point
  38. {
  39. get
  40. {
  41. if (p == null)
  42. {
  43. p = c.DecodePoint(encoding.GetOctets()).Normalize();
  44. }
  45. return p;
  46. }
  47. }
  48. public bool IsPointCompressed
  49. {
  50. get
  51. {
  52. byte[] octets = encoding.GetOctets();
  53. return octets != null && octets.Length > 0 && (octets[0] == 2 || octets[0] == 3);
  54. }
  55. }
  56. /**
  57. * Produce an object suitable for an Asn1OutputStream.
  58. * <pre>
  59. * ECPoint ::= OCTET STRING
  60. * </pre>
  61. * <p>
  62. * Octet string produced using ECPoint.GetEncoded().</p>
  63. */
  64. public override Asn1Object ToAsn1Object()
  65. {
  66. return encoding;
  67. }
  68. }
  69. }
  70. #endif