View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.pkcs;
20  
21  import java.math.BigInteger;
22  import java.util.Enumeration;
23  
24  import org.apache.geronimo.util.asn1.ASN1Encodable;
25  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
26  import org.apache.geronimo.util.asn1.ASN1Sequence;
27  import org.apache.geronimo.util.asn1.DERInteger;
28  import org.apache.geronimo.util.asn1.DERObject;
29  import org.apache.geronimo.util.asn1.DERSequence;
30  
31  public class DHParameter
32      extends ASN1Encodable
33  {
34      DERInteger      p, g, l;
35  
36      public DHParameter(
37          BigInteger  p,
38          BigInteger  g,
39          int         l)
40      {
41          this.p = new DERInteger(p);
42          this.g = new DERInteger(g);
43  
44          if (l != 0)
45          {
46              this.l = new DERInteger(l);
47          }
48          else
49          {
50              this.l = null;
51          }
52      }
53  
54      public DHParameter(
55          ASN1Sequence  seq)
56      {
57          Enumeration     e = seq.getObjects();
58  
59          p = (DERInteger)e.nextElement();
60          g = (DERInteger)e.nextElement();
61  
62          if (e.hasMoreElements())
63          {
64              l = (DERInteger)e.nextElement();
65          }
66          else
67          {
68              l = null;
69          }
70      }
71  
72      public BigInteger getP()
73      {
74          return p.getPositiveValue();
75      }
76  
77      public BigInteger getG()
78      {
79          return g.getPositiveValue();
80      }
81  
82      public BigInteger getL()
83      {
84          if (l == null)
85          {
86              return null;
87          }
88  
89          return l.getPositiveValue();
90      }
91  
92      public DERObject toASN1Object()
93      {
94          ASN1EncodableVector  v = new ASN1EncodableVector();
95  
96          v.add(p);
97          v.add(g);
98  
99          if (this.getL() != null)
100         {
101             v.add(l);
102         }
103 
104         return new DERSequence(v);
105     }
106 }