Reset branch to trunk.
[official-gcc.git] / trunk / libjava / classpath / gnu / javax / crypto / key / dh / GnuDHPublicKey.java
blob5fb31f38364d45208b2fb74a27d2f0d7d17b6984
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.action.GetPropertyAction;
43 import gnu.java.security.key.IKeyPairCodec;
45 import java.math.BigInteger;
46 import java.security.AccessController;
48 import javax.crypto.interfaces.DHPublicKey;
50 /**
51 * An implementation of the Diffie-Hellman public key.
52 * <p>
53 * Reference:
54 * <ol>
55 * <li><a href="http://www.ietf.org/rfc/rfc2631.txt">Diffie-Hellman Key
56 * Agreement Method</a><br>
57 * Eric Rescorla.</li>
58 * </ol>
60 public class GnuDHPublicKey
61 extends GnuDHKey
62 implements DHPublicKey
64 private BigInteger y;
65 /** String representation of this key. Cached for speed. */
66 private transient String str;
68 /**
69 * Convenience constructor. Calls the constructor with five arguments passing
70 * {@link Registry#RAW_ENCODING_ID} as the value of its first argument.
72 * @param q a prime divisor of p-1.
73 * @param p the public prime.
74 * @param g the generator of the group.
75 * @param y the public value y.
77 public GnuDHPublicKey(BigInteger q, BigInteger p, BigInteger g, BigInteger y)
79 this(Registry.RAW_ENCODING_ID, q, p, g, y);
82 /**
83 * Constructs a new instance of <code>GnuDHPublicKey</code> given the
84 * designated parameters.
86 * @param preferredFormat the identifier of the encoding format to use by
87 * default when externalizing the key.
88 * @param q a prime divisor of p-1.
89 * @param p the public prime.
90 * @param g the generator of the group.
91 * @param y the public value y.
93 public GnuDHPublicKey(int preferredFormat, BigInteger q, BigInteger p,
94 BigInteger g, BigInteger y)
96 super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.X509_ENCODING_ID
97 : preferredFormat,
98 q, p, g);
99 this.y = y;
103 * A class method that takes the output of the <code>encodePublicKey()</code>
104 * method of a DH keypair codec object (an instance implementing
105 * {@link IKeyPairCodec} for DSS keys, and re-constructs an instance of this
106 * object.
108 * @param k the contents of a previously encoded instance of this object.
109 * @exception ArrayIndexOutOfBoundsException if there is not enough bytes, in
110 * <code>k</code>, to represent a valid encoding of an
111 * instance of this object.
112 * @exception IllegalArgumentException if the byte sequence does not represent
113 * a valid encoding of an instance of this object.
115 public static GnuDHPublicKey valueOf(byte[] k)
117 // try RAW codec
118 if (k[0] == Registry.MAGIC_RAW_DH_PUBLIC_KEY[0])
121 return (GnuDHPublicKey) new DHKeyPairRawCodec().decodePublicKey(k);
123 catch (IllegalArgumentException ignored)
126 // try X.509 codec
127 return (GnuDHPublicKey) new DHKeyPairX509Codec().decodePublicKey(k);
130 public BigInteger getY()
132 return y;
136 * Returns the encoded form of this public key according to the designated
137 * format.
139 * @param format the desired format identifier of the resulting encoding.
140 * @return the byte sequence encoding this key according to the designated
141 * format.
142 * @exception IllegalArgumentException if the format is not supported.
144 public byte[] getEncoded(int format)
146 byte[] result;
147 switch (format)
149 case IKeyPairCodec.RAW_FORMAT:
150 result = new DHKeyPairRawCodec().encodePublicKey(this);
151 break;
152 case IKeyPairCodec.X509_FORMAT:
153 result = new DHKeyPairX509Codec().encodePublicKey(this);
154 break;
155 default:
156 throw new IllegalArgumentException("Unsupported encoding format: "
157 + format);
159 return result;
163 * Returns <code>true</code> if the designated object is an instance of
164 * {@link DHPublicKey} and has the same parameter values as this one.
166 * @param obj the other non-null DH key to compare to.
167 * @return <code>true</code> if the designated object is of the same type
168 * and value as this one.
170 public boolean equals(Object obj)
172 if (obj == null)
173 return false;
175 if (! (obj instanceof DHPublicKey))
176 return false;
178 DHPublicKey that = (DHPublicKey) obj;
179 return super.equals(that) && y.equals(that.getY());
182 public String toString()
184 if (str == null)
186 String ls = (String) AccessController.doPrivileged
187 (new GetPropertyAction("line.separator"));
188 str = new StringBuilder(this.getClass().getName()).append("(")
189 .append(super.toString()).append(",").append(ls)
190 .append("y=0x").append(y.toString(16)).append(ls)
191 .append(")")
192 .toString();
194 return str;