X9FieldElement.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Math;
  4. using Org.BouncyCastle.Math.EC;
  5. namespace Org.BouncyCastle.Asn1.X9
  6. {
  7. /**
  8. * Class for processing an ECFieldElement as a DER object.
  9. */
  10. public class X9FieldElement
  11. : Asn1Encodable
  12. {
  13. private ECFieldElement f;
  14. public X9FieldElement(
  15. ECFieldElement f)
  16. {
  17. this.f = f;
  18. }
  19. public X9FieldElement(
  20. BigInteger p,
  21. Asn1OctetString s)
  22. : this(new FpFieldElement(p, new BigInteger(1, s.GetOctets())))
  23. {
  24. }
  25. public X9FieldElement(
  26. int m,
  27. int k1,
  28. int k2,
  29. int k3,
  30. Asn1OctetString s)
  31. : this(new F2mFieldElement(m, k1, k2, k3, new BigInteger(1, s.GetOctets())))
  32. {
  33. }
  34. public ECFieldElement Value
  35. {
  36. get { return f; }
  37. }
  38. /**
  39. * Produce an object suitable for an Asn1OutputStream.
  40. * <pre>
  41. * FieldElement ::= OCTET STRING
  42. * </pre>
  43. * <p>
  44. * <ol>
  45. * <li> if <i>q</i> is an odd prime then the field element is
  46. * processed as an Integer and converted to an octet string
  47. * according to x 9.62 4.3.1.</li>
  48. * <li> if <i>q</i> is 2<sup>m</sup> then the bit string
  49. * contained in the field element is converted into an octet
  50. * string with the same ordering padded at the front if necessary.
  51. * </li>
  52. * </ol>
  53. * </p>
  54. */
  55. public override Asn1Object ToAsn1Object()
  56. {
  57. int byteCount = X9IntegerConverter.GetByteLength(f);
  58. byte[] paddedBigInteger = X9IntegerConverter.IntegerToBytes(f.ToBigInteger(), byteCount);
  59. return new DerOctetString(paddedBigInteger);
  60. }
  61. }
  62. }
  63. #endif