1 /* DSSPublicKey.java --
2 Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
4 This file is a 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 of the License, or (at
9 your option) 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; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
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
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
.key
.dss
;
41 import gnu
.classpath
.SystemProperties
;
42 import gnu
.java
.security
.Registry
;
43 import gnu
.java
.security
.key
.IKeyPairCodec
;
45 import java
.math
.BigInteger
;
46 import java
.security
.PublicKey
;
47 import java
.security
.interfaces
.DSAPublicKey
;
50 * <p>An object that embodies a DSS (Digital Signature Standard) public key.</p>
54 public class DSSPublicKey
extends DSSKey
implements PublicKey
, DSAPublicKey
56 // Constants and variables
57 // -------------------------------------------------------------------------
60 * <code>y = g<sup>x</sup> mod p</code> where <code>x</code> is the private
61 * part of the DSA key.
63 private final BigInteger y
;
65 /** String representation of this key. Cached for speed. */
66 private transient String str
;
69 // -------------------------------------------------------------------------
72 * Conveience constructor. Calls the constructor with 5 arguments passing
73 * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred
76 * @param p the public modulus.
77 * @param q the public prime divisor of <code>p-1</code>.
78 * @param g a generator of the unique cyclic group <code>Z<sup>*</sup>
79 * <sub>p</sub></code>.
80 * @param y the public key part.
82 public DSSPublicKey(BigInteger p
, BigInteger q
, BigInteger g
, BigInteger y
)
84 this(Registry
.RAW_ENCODING_ID
, p
, q
, g
, y
);
88 * Constructs a new instance of <code>DSSPublicKey</code> given the designated
91 * @param preferredFormat the identifier of the preferred encoding format to
92 * use when externalizing this key.
93 * @param p the public modulus.
94 * @param q the public prime divisor of <code>p-1</code>.
95 * @param g a generator of the unique cyclic group <code>Z<sup>*</sup>
96 * <sub>p</sub></code>.
97 * @param y the public key part.
99 public DSSPublicKey(int preferredFormat
, BigInteger p
, BigInteger q
,
100 BigInteger g
, BigInteger y
)
102 super(preferredFormat
== Registry
.ASN1_ENCODING_ID ? Registry
.X509_ENCODING_ID
110 // -------------------------------------------------------------------------
113 * A class method that takes the output of the <code>encodePublicKey()</code>
114 * method of a DSS keypair codec object (an instance implementing
115 * {@link gnu.java.security.key.IKeyPairCodec} for DSS keys, and re-constructs
116 * an instance of this object.
118 * @param k the contents of a previously encoded instance of this object.
119 * @exception ArrayIndexOutOfBoundsException if there is not enough bytes, in
120 * <code>k</code>, to represent a valid encoding of an
121 * instance of this object.
122 * @exception IllegalArgumentException if the byte sequence does not represent
123 * a valid encoding of an instance of this object.
125 public static DSSPublicKey
valueOf(byte[] k
)
128 if (k
[0] == Registry
.MAGIC_RAW_DSS_PUBLIC_KEY
[0])
131 return (DSSPublicKey
) new DSSKeyPairRawCodec().decodePublicKey(k
);
133 catch (IllegalArgumentException ignored
)
138 return (DSSPublicKey
) new DSSKeyPairX509Codec().decodePublicKey(k
);
142 // -------------------------------------------------------------------------
144 // java.security.interfaces.DSAPublicKey interface implementation ----------
146 public BigInteger
getY()
151 // Other instance methods --------------------------------------------------
154 * <p>Returns the encoded form of this public key according to the designated
157 * @param format the desired format identifier of the resulting encoding.
158 * @return the byte sequence encoding this key according to the designated
160 * @exception IllegalArgumentException if the format is not supported.
161 * @see DSSKeyPairRawCodec
163 public byte[] getEncoded(int format
)
168 case IKeyPairCodec
.RAW_FORMAT
:
169 result
= new DSSKeyPairRawCodec().encodePublicKey(this);
171 case IKeyPairCodec
.X509_FORMAT
:
172 result
= new DSSKeyPairX509Codec().encodePublicKey(this);
175 throw new IllegalArgumentException("Unsupported encoding format: "
182 * <p>Returns <code>true</code> if the designated object is an instance of
183 * {@link DSAPublicKey} and has the same DSS (Digital Signature Standard)
184 * parameter values as this one.</p>
186 * @param obj the other non-null DSS key to compare to.
187 * @return <code>true</code> if the designated object is of the same type and
190 public boolean equals(Object obj
)
196 if (!(obj
instanceof DSAPublicKey
))
200 DSAPublicKey that
= (DSAPublicKey
) obj
;
201 return super.equals(that
) && y
.equals(that
.getY());
204 public String
toString()
208 String ls
= SystemProperties
.getProperty("line.separator");
209 str
= new StringBuilder(this.getClass().getName()).append("(")
210 .append(super.toString()).append(",").append(ls
)
211 .append("y=0x").append(y
.toString(16)).append(ls
)
212 .append(")").toString();