01: package org.bouncycastle.asn1.x9;
02:
03: import java.math.BigInteger;
04:
05: import org.bouncycastle.asn1.ASN1Encodable;
06: import org.bouncycastle.asn1.ASN1OctetString;
07: import org.bouncycastle.asn1.DERObject;
08: import org.bouncycastle.asn1.DEROctetString;
09: import org.bouncycastle.math.ec.ECFieldElement;
10:
11: /**
12: * class for processing an FieldElement as a DER object.
13: */
14: public class X9FieldElement extends ASN1Encodable {
15: protected ECFieldElement f;
16:
17: private static X9IntegerConverter converter = new X9IntegerConverter();
18:
19: public X9FieldElement(ECFieldElement f) {
20: this .f = f;
21: }
22:
23: public X9FieldElement(BigInteger p, ASN1OctetString s) {
24: this (new ECFieldElement.Fp(p, new BigInteger(1, s.getOctets())));
25: }
26:
27: public X9FieldElement(int m, int k1, int k2, int k3,
28: ASN1OctetString s) {
29: this (new ECFieldElement.F2m(m, k1, k2, k3, new BigInteger(1, s
30: .getOctets())));
31: }
32:
33: public ECFieldElement getValue() {
34: return f;
35: }
36:
37: /**
38: * Produce an object suitable for an ASN1OutputStream.
39: * <pre>
40: * FieldElement ::= OCTET STRING
41: * </pre>
42: * <p>
43: * <ol>
44: * <li> if <i>q</i> is an odd prime then the field element is
45: * processed as an Integer and converted to an octet string
46: * according to x 9.62 4.3.1.</li>
47: * <li> if <i>q</i> is 2<sup>m</sup> then the bit string
48: * contained in the field element is converted into an octet
49: * string with the same ordering padded at the front if necessary.
50: * </li>
51: * </ol>
52: */
53: public DERObject toASN1Object() {
54: int byteCount = converter.getByteLength(f);
55: byte[] paddedBigInteger = converter.integerToBytes(f
56: .toBigInteger(), byteCount);
57:
58: return new DEROctetString(paddedBigInteger);
59: }
60: }
|