Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / gnu / java / security / provider / GnuDSAPublicKey.java
blob9ec827d41aee4bfcd9348d09c69ba0cabc4b857a
1 /* GnuDSAPublicKey.java --- Gnu DSA Public Key
2 Copyright (C) 1999,2003,2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.provider;
41 import gnu.java.security.OID;
42 import gnu.java.security.der.BitString;
43 import gnu.java.security.der.DER;
44 import gnu.java.security.der.DERValue;
45 import gnu.java.security.der.DERWriter;
47 import java.io.ByteArrayOutputStream;
48 import java.io.IOException;
50 import java.math.BigInteger;
52 import java.security.interfaces.DSAPublicKey;
53 import java.security.interfaces.DSAParams;
54 import java.security.spec.DSAParameterSpec;
56 import java.util.ArrayList;
58 public class GnuDSAPublicKey implements DSAPublicKey
60 private byte[] encodedKey;
61 BigInteger y;
62 BigInteger p;
63 BigInteger q;
64 BigInteger g;
66 public GnuDSAPublicKey(BigInteger y, BigInteger p, BigInteger q, BigInteger g )
68 this.y = y;
69 this.p = p;
70 this.q = q;
71 this.g = g;
74 public String getAlgorithm()
76 return "DSA";
79 public String getFormat()
81 return "X.509";
84 /**
85 * The encoded form of DSA public keys is:
87 * <blockquote><pre>
88 * SubjectPublicKeyInfo ::= SEQUENCE {
89 * algorithm AlgorithmIdentifier,
90 * subjectPublicKey BIT STRING }
91 * </pre></blockquote>
93 public byte[] getEncoded()
95 if (encodedKey != null)
96 return (byte[]) encodedKey.clone();
97 try
99 ByteArrayOutputStream out = new ByteArrayOutputStream();
100 ArrayList spki = new ArrayList(2);
101 ArrayList alg = new ArrayList(2);
102 alg.add(new DERValue(DER.OBJECT_IDENTIFIER,
103 new OID("1.2.840.113549.1.1.1")));
104 ArrayList params = new ArrayList(3);
105 params.add(new DERValue(DER.INTEGER, p));
106 params.add(new DERValue(DER.INTEGER, q));
107 params.add(new DERValue(DER.INTEGER, g));
108 alg.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, params));
109 spki.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, alg));
110 spki.add(new DERValue(DER.BIT_STRING, new BitString(y.toByteArray())));
111 DERWriter.write(out, new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, spki));
112 return (byte[]) (encodedKey = out.toByteArray()).clone();
114 catch (IOException ioe)
116 return null;
120 public DSAParams getParams()
122 if (p == null || q == null || g == null)
123 return null;
124 return (DSAParams)(new DSAParameterSpec(p,q,g));
127 public BigInteger getY()
129 return y;
132 public String toString()
134 return
135 "GnuDSAPublicKey: y=" + (y != null ? y.toString(16) : "(null)") +
136 " p=" + (p != null ? p.toString(16) : "(null)") +
137 " q=" + (q != null ? q.toString(16) : "(null)") +
138 " g=" + (g != null ? g.toString(16) : "(null)");