KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > RSAPublicKeyStructure


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1.x509;
19
20 import java.math.BigInteger JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import org.apache.geronimo.util.asn1.ASN1Encodable;
24 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
25 import org.apache.geronimo.util.asn1.ASN1Sequence;
26 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
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 RSAPublicKeyStructure
32     extends ASN1Encodable
33 {
34     private BigInteger JavaDoc modulus;
35     private BigInteger JavaDoc publicExponent;
36
37     public static RSAPublicKeyStructure getInstance(
38         ASN1TaggedObject obj,
39         boolean explicit)
40     {
41         return getInstance(ASN1Sequence.getInstance(obj, explicit));
42     }
43
44     public static RSAPublicKeyStructure getInstance(
45         Object JavaDoc obj)
46     {
47         if(obj == null || obj instanceof RSAPublicKeyStructure)
48         {
49             return (RSAPublicKeyStructure)obj;
50         }
51
52         if(obj instanceof ASN1Sequence)
53         {
54             return new RSAPublicKeyStructure((ASN1Sequence)obj);
55         }
56
57         throw new IllegalArgumentException JavaDoc("Invalid RSAPublicKeyStructure: " + obj.getClass().getName());
58     }
59
60     public RSAPublicKeyStructure(
61         BigInteger JavaDoc modulus,
62         BigInteger JavaDoc publicExponent)
63     {
64         this.modulus = modulus;
65         this.publicExponent = publicExponent;
66     }
67
68     public RSAPublicKeyStructure(
69         ASN1Sequence seq)
70     {
71         Enumeration JavaDoc e = seq.getObjects();
72
73         modulus = ((DERInteger)e.nextElement()).getPositiveValue();
74         publicExponent = ((DERInteger)e.nextElement()).getPositiveValue();
75     }
76
77     public BigInteger JavaDoc getModulus()
78     {
79         return modulus;
80     }
81
82     public BigInteger JavaDoc getPublicExponent()
83     {
84         return publicExponent;
85     }
86
87     /**
88      * This outputs the key in PKCS1v2 format.
89      * <pre>
90      * RSAPublicKey ::= SEQUENCE {
91      * modulus INTEGER, -- n
92      * publicExponent INTEGER, -- e
93      * }
94      * </pre>
95      * <p>
96      */

97     public DERObject toASN1Object()
98     {
99         ASN1EncodableVector v = new ASN1EncodableVector();
100
101         v.add(new DERInteger(getModulus()));
102         v.add(new DERInteger(getPublicExponent()));
103
104         return new DERSequence(v);
105     }
106 }
107
Popular Tags