KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > jce > provider > JDKDSAPublicKey


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.jce.provider;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.math.BigInteger JavaDoc;
23 import java.security.interfaces.DSAParams JavaDoc;
24 import java.security.interfaces.DSAPublicKey JavaDoc;
25 import java.security.spec.DSAParameterSpec JavaDoc;
26 import java.security.spec.DSAPublicKeySpec JavaDoc;
27
28 import org.apache.geronimo.util.asn1.ASN1Sequence;
29 import org.apache.geronimo.util.asn1.DERInteger;
30 import org.apache.geronimo.util.asn1.DEROutputStream;
31 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
32 import org.apache.geronimo.util.asn1.x509.DSAParameter;
33 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
34 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
35 import org.apache.geronimo.util.crypto.params.DSAPublicKeyParameters;
36
37 public class JDKDSAPublicKey
38     implements DSAPublicKey JavaDoc
39 {
40     private BigInteger JavaDoc y;
41     private DSAParams JavaDoc dsaSpec;
42
43     JDKDSAPublicKey(
44         DSAPublicKeySpec JavaDoc spec)
45     {
46         this.y = spec.getY();
47         this.dsaSpec = new DSAParameterSpec JavaDoc(spec.getP(), spec.getQ(), spec.getG());
48     }
49
50     JDKDSAPublicKey(
51         DSAPublicKey JavaDoc key)
52     {
53         this.y = key.getY();
54         this.dsaSpec = key.getParams();
55     }
56
57     JDKDSAPublicKey(
58         DSAPublicKeyParameters params)
59     {
60         this.y = params.getY();
61         this.dsaSpec = new DSAParameterSpec JavaDoc(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());
62     }
63
64     JDKDSAPublicKey(
65         BigInteger JavaDoc y,
66         DSAParameterSpec JavaDoc dsaSpec)
67     {
68         this.y = y;
69         this.dsaSpec = dsaSpec;
70     }
71
72     JDKDSAPublicKey(
73         SubjectPublicKeyInfo info)
74     {
75         DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
76         DERInteger derY = null;
77
78         try
79         {
80             derY = (DERInteger)info.getPublicKey();
81         }
82         catch (IOException JavaDoc e)
83         {
84             throw new IllegalArgumentException JavaDoc("invalid info structure in DSA public key");
85         }
86
87         this.y = derY.getValue();
88         this.dsaSpec = new DSAParameterSpec JavaDoc(params.getP(), params.getQ(), params.getG());
89     }
90
91     public String JavaDoc getAlgorithm()
92     {
93         return "DSA";
94     }
95
96     public String JavaDoc getFormat()
97     {
98         return "X.509";
99     }
100
101     public byte[] getEncoded()
102     {
103         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
104         DEROutputStream dOut = new DEROutputStream(bOut);
105         SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(y));
106
107         try
108         {
109             dOut.writeObject(info);
110             dOut.close();
111         }
112         catch (IOException JavaDoc e)
113         {
114             throw new RuntimeException JavaDoc("Error encoding DSA public key");
115         }
116
117         return bOut.toByteArray();
118
119     }
120
121     public DSAParams JavaDoc getParams()
122     {
123         return dsaSpec;
124     }
125
126     public BigInteger JavaDoc getY()
127     {
128         return y;
129     }
130
131     public String JavaDoc toString()
132     {
133         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
134         String JavaDoc nl = System.getProperty("line.separator");
135
136         buf.append("DSA Public Key" + nl);
137         buf.append(" y: " + this.getY().toString(16) + nl);
138
139         return buf.toString();
140     }
141 }
142
Popular Tags