X9FieldElement.cs 2.0 KB

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