1package org.bouncycastle.asn1.pkcs;
2
3import java.math.BigInteger;
4import java.util.Enumeration;
5
6import org.bouncycastle.asn1.ASN1EncodableVector;
7import org.bouncycastle.asn1.ASN1Integer;
8import org.bouncycastle.asn1.ASN1Object;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.DERSequence;
12
13public class DHParameter
14    extends ASN1Object
15{
16    ASN1Integer      p, g, l;
17
18    public DHParameter(
19        BigInteger  p,
20        BigInteger  g,
21        int         l)
22    {
23        this.p = new ASN1Integer(p);
24        this.g = new ASN1Integer(g);
25
26        if (l != 0)
27        {
28            this.l = new ASN1Integer(l);
29        }
30        else
31        {
32            this.l = null;
33        }
34    }
35
36    public static DHParameter getInstance(
37        Object  obj)
38    {
39        if (obj instanceof DHParameter)
40        {
41            return (DHParameter)obj;
42        }
43
44        if (obj != null)
45        {
46            return new DHParameter(ASN1Sequence.getInstance(obj));
47        }
48
49        return null;
50    }
51
52    private DHParameter(
53        ASN1Sequence  seq)
54    {
55        Enumeration     e = seq.getObjects();
56
57        p = ASN1Integer.getInstance(e.nextElement());
58        g = ASN1Integer.getInstance(e.nextElement());
59
60        if (e.hasMoreElements())
61        {
62            l = (ASN1Integer)e.nextElement();
63        }
64        else
65        {
66            l = null;
67        }
68    }
69
70    public BigInteger getP()
71    {
72        return p.getPositiveValue();
73    }
74
75    public BigInteger getG()
76    {
77        return g.getPositiveValue();
78    }
79
80    public BigInteger getL()
81    {
82        if (l == null)
83        {
84            return null;
85        }
86
87        return l.getPositiveValue();
88    }
89
90    public ASN1Primitive toASN1Primitive()
91    {
92        ASN1EncodableVector  v = new ASN1EncodableVector();
93
94        v.add(p);
95        v.add(g);
96
97        if (this.getL() != null)
98        {
99            v.add(l);
100        }
101
102        return new DERSequence(v);
103    }
104}
105