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
.java
.security
.Registry
;
42 import gnu
.java
.security
.action
.GetPropertyAction
;
43 import gnu
.java
.security
.key
.IKeyPairCodec
;
45 import java
.math
.BigInteger
;
46 import java
.security
.AccessController
;
47 import java
.security
.PublicKey
;
48 import java
.security
.interfaces
.DSAPublicKey
;
51 * An object that embodies a DSS (Digital Signature Standard) public key.
55 public class DSSPublicKey
57 implements PublicKey
, DSAPublicKey
60 * <code>y = g<sup>x</sup> mod p</code> where <code>x</code> is the
61 * private 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 * Conveience constructor. Calls the constructor with 5 arguments passing
70 * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred
73 * @param p the public modulus.
74 * @param q the public prime divisor of <code>p-1</code>.
75 * @param g a generator of the unique cyclic group <code>Z<sup>*</sup>
76 * <sub>p</sub></code>.
77 * @param y the public key part.
79 public DSSPublicKey(BigInteger p
, BigInteger q
, BigInteger g
, BigInteger y
)
81 this(Registry
.RAW_ENCODING_ID
, p
, q
, g
, y
);
85 * Constructs a new instance of <code>DSSPublicKey</code> given the
86 * designated arguments.
88 * @param preferredFormat the identifier of the preferred encoding format to
89 * use when externalizing this key.
90 * @param p the public modulus.
91 * @param q the public prime divisor of <code>p-1</code>.
92 * @param g a generator of the unique cyclic group <code>Z<sup>*</sup>
93 * <sub>p</sub></code>.
94 * @param y the public key part.
96 public DSSPublicKey(int preferredFormat
, BigInteger p
, BigInteger q
,
97 BigInteger g
, BigInteger y
)
99 super(preferredFormat
== Registry
.ASN1_ENCODING_ID ? Registry
.X509_ENCODING_ID
106 * A class method that takes the output of the <code>encodePublicKey()</code>
107 * method of a DSS keypair codec object (an instance implementing
108 * {@link gnu.java.security.key.IKeyPairCodec} for DSS keys, and re-constructs
109 * an instance of this object.
111 * @param k the contents of a previously encoded instance of this object.
112 * @exception ArrayIndexOutOfBoundsException if there is not enough bytes, in
113 * <code>k</code>, to represent a valid encoding of an
114 * instance of this object.
115 * @exception IllegalArgumentException if the byte sequence does not represent
116 * a valid encoding of an instance of this object.
118 public static DSSPublicKey
valueOf(byte[] k
)
121 if (k
[0] == Registry
.MAGIC_RAW_DSS_PUBLIC_KEY
[0])
124 return (DSSPublicKey
) new DSSKeyPairRawCodec().decodePublicKey(k
);
126 catch (IllegalArgumentException ignored
)
130 return (DSSPublicKey
) new DSSKeyPairX509Codec().decodePublicKey(k
);
133 public BigInteger
getY()
139 * Returns the encoded form of this public key according to the designated
142 * @param format the desired format identifier of the resulting encoding.
143 * @return the byte sequence encoding this key according to the designated
145 * @exception IllegalArgumentException if the format is not supported.
146 * @see DSSKeyPairRawCodec
148 public byte[] getEncoded(int format
)
153 case IKeyPairCodec
.RAW_FORMAT
:
154 result
= new DSSKeyPairRawCodec().encodePublicKey(this);
156 case IKeyPairCodec
.X509_FORMAT
:
157 result
= new DSSKeyPairX509Codec().encodePublicKey(this);
160 throw new IllegalArgumentException("Unsupported encoding format: "
167 * Returns <code>true</code> if the designated object is an instance of
168 * {@link DSAPublicKey} and has the same DSS (Digital Signature Standard)
169 * parameter values as this one.
171 * @param obj the other non-null DSS key to compare to.
172 * @return <code>true</code> if the designated object is of the same type
173 * and value as this one.
175 public boolean equals(Object obj
)
180 if (! (obj
instanceof DSAPublicKey
))
183 DSAPublicKey that
= (DSAPublicKey
) obj
;
184 return super.equals(that
) && y
.equals(that
.getY());
187 public String
toString()
191 String ls
= (String
) AccessController
.doPrivileged
192 (new GetPropertyAction("line.separator"));
193 str
= new StringBuilder(this.getClass().getName()).append("(")
194 .append(super.toString()).append(",").append(ls
)
195 .append("y=0x").append(y
.toString(16)).append(ls
)