Home | History | Annotate | Download | only in pkcs
      1 package org.bouncycastle.asn1.pkcs;
      2 
      3 import java.math.BigInteger;
      4 import java.util.Enumeration;
      5 
      6 import org.bouncycastle.asn1.ASN1Encodable;
      7 import org.bouncycastle.asn1.ASN1EncodableVector;
      8 import org.bouncycastle.asn1.ASN1Sequence;
      9 import org.bouncycastle.asn1.DERInteger;
     10 import org.bouncycastle.asn1.DERObject;
     11 import org.bouncycastle.asn1.DERSequence;
     12 
     13 public class DHParameter
     14     extends ASN1Encodable
     15 {
     16     DERInteger      p, g, l;
     17 
     18     public DHParameter(
     19         BigInteger  p,
     20         BigInteger  g,
     21         int         l)
     22     {
     23         this.p = new DERInteger(p);
     24         this.g = new DERInteger(g);
     25 
     26         if (l != 0)
     27         {
     28             this.l = new DERInteger(l);
     29         }
     30         else
     31         {
     32             this.l = null;
     33         }
     34     }
     35 
     36     public DHParameter(
     37         ASN1Sequence  seq)
     38     {
     39         Enumeration     e = seq.getObjects();
     40 
     41         p = (DERInteger)e.nextElement();
     42         g = (DERInteger)e.nextElement();
     43 
     44         if (e.hasMoreElements())
     45         {
     46             l = (DERInteger)e.nextElement();
     47         }
     48         else
     49         {
     50             l = null;
     51         }
     52     }
     53 
     54     public BigInteger getP()
     55     {
     56         return p.getPositiveValue();
     57     }
     58 
     59     public BigInteger getG()
     60     {
     61         return g.getPositiveValue();
     62     }
     63 
     64     public BigInteger getL()
     65     {
     66         if (l == null)
     67         {
     68             return null;
     69         }
     70 
     71         return l.getPositiveValue();
     72     }
     73 
     74     public DERObject toASN1Object()
     75     {
     76         ASN1EncodableVector  v = new ASN1EncodableVector();
     77 
     78         v.add(p);
     79         v.add(g);
     80 
     81         if (this.getL() != null)
     82         {
     83             v.add(l);
     84         }
     85 
     86         return new DERSequence(v);
     87     }
     88 }
     89