Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / key / dh / GnuDHPublicKey.java
blob56516c9d0ea7452b46eb65cb8c21035ae69cedc3
1 /* GnuDHPublicKey.java --
2 Copyright (C) 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
19 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.javax.crypto.key.dh;
41 import gnu.java.security.Registry;
42 import gnu.java.security.key.IKeyPairCodec;
44 import java.math.BigInteger;
46 import javax.crypto.interfaces.DHPublicKey;
48 /**
49 * <p>An implementation of the Diffie-Hellman public key.</p>
51 * <p>Reference:</p>
52 * <ol>
53 * <li><a href="http://www.ietf.org/rfc/rfc2631.txt">Diffie-Hellman Key
54 * Agreement Method</a><br>
55 * Eric Rescorla.</li>
56 * </ol>
58 public class GnuDHPublicKey extends GnuDHKey implements DHPublicKey
61 // Constants and variables
62 // -------------------------------------------------------------------------
64 private BigInteger y;
66 // Constructor(s)
67 // -------------------------------------------------------------------------
69 /**
70 * Convenience constructor. Calls the constructor with five arguments passing
71 * {@link Registry#RAW_ENCODING_ID} as the value of its first argument.
73 * @param q a prime divisor of p-1.
74 * @param p the public prime.
75 * @param g the generator of the group.
76 * @param y the public value y.
78 public GnuDHPublicKey(BigInteger q, BigInteger p, BigInteger g, BigInteger y)
80 this(Registry.RAW_ENCODING_ID, q, p, g, y);
83 /**
84 * Constructs a new instance of <code>GnuDHPublicKey</code> given the
85 * designated parameters.
87 * @param preferredFormat the identifier of the encoding format to use by
88 * default when externalizing the key.
89 * @param q a prime divisor of p-1.
90 * @param p the public prime.
91 * @param g the generator of the group.
92 * @param y the public value y.
94 public GnuDHPublicKey(int preferredFormat,
95 BigInteger q, BigInteger p, BigInteger g, BigInteger y)
97 super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.X509_ENCODING_ID
98 : preferredFormat,
99 q, p, g);
101 this.y = y;
104 // Class methods
105 // -------------------------------------------------------------------------
108 * <p>A class method that takes the output of the <code>encodePublicKey()</code>
109 * method of a DH keypair codec object (an instance implementing
110 * {@link IKeyPairCodec} for DSS keys, and re-constructs an instance of this
111 * object.</p>
113 * @param k the contents of a previously encoded instance of this object.
114 * @exception ArrayIndexOutOfBoundsException if there is not enough bytes,
115 * in <code>k</code>, to represent a valid encoding of an instance of this
116 * object.
117 * @exception IllegalArgumentException if the byte sequence does not
118 * represent a valid encoding of an instance of this object.
120 public static GnuDHPublicKey valueOf(byte[] k)
122 // try RAW codec
123 if (k[0] == Registry.MAGIC_RAW_DH_PUBLIC_KEY[0])
126 return (GnuDHPublicKey) new DHKeyPairRawCodec().decodePublicKey(k);
128 catch (IllegalArgumentException ignored)
132 // try X.509 codec
133 return (GnuDHPublicKey) new DHKeyPairX509Codec().decodePublicKey(k);
136 // Instance methods
137 // -------------------------------------------------------------------------
139 // javax.crypto.interfaces.DHPublicKey interface implementation ------------
141 public BigInteger getY()
143 return y;
146 // other methods -----------------------------------------------------------
149 * <p>Returns the encoded form of this public key according to the designated
150 * format.</p>
152 * @param format the desired format identifier of the resulting encoding.
153 * @return the byte sequence encoding this key according to the designated
154 * format.
155 * @exception IllegalArgumentException if the format is not supported.
157 public byte[] getEncoded(int format)
159 byte[] result;
160 switch (format)
162 case IKeyPairCodec.RAW_FORMAT:
163 result = new DHKeyPairRawCodec().encodePublicKey(this);
164 break;
165 case IKeyPairCodec.X509_FORMAT:
166 result = new DHKeyPairX509Codec().encodePublicKey(this);
167 break;
168 default:
169 throw new IllegalArgumentException("Unsupported encoding format: "
170 + format);
172 return result;
176 * Returns <code>true</code> if the designated object is an instance of
177 * {@link DHPublicKey} and has the same parameter values as this one.
179 * @param obj the other non-null DH key to compare to.
180 * @return <code>true</code> if the designated object is of the same type
181 * and value as this one.
183 public boolean equals(Object obj)
185 if (obj == null)
186 return false;
188 if (! (obj instanceof DHPublicKey))
189 return false;
191 DHPublicKey that = (DHPublicKey) obj;
192 return super.equals(that) && y.equals(that.getY());