0N/A/*
2362N/A * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.crypto.spec;
0N/A
0N/Aimport java.io.UnsupportedEncodingException;
0N/Aimport java.security.Key;
0N/Aimport java.security.spec.KeySpec;
0N/Aimport javax.crypto.SecretKey;
0N/A
0N/A/**
0N/A * This class specifies a secret key in a provider-independent fashion.
0N/A *
0N/A * <p>It can be used to construct a <code>SecretKey</code> from a byte array,
0N/A * without having to go through a (provider-based)
0N/A * <code>SecretKeyFactory</code>.
0N/A *
0N/A * <p>This class is only useful for raw secret keys that can be represented as
0N/A * a byte array and have no key parameters associated with them, e.g., DES or
0N/A * Triple DES keys.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A * @see javax.crypto.SecretKey
0N/A * @see javax.crypto.SecretKeyFactory
0N/A * @since 1.4
0N/A */
0N/Apublic class SecretKeySpec implements KeySpec, SecretKey {
0N/A
0N/A private static final long serialVersionUID = 6577238317307289933L;
0N/A
0N/A /**
0N/A * The secret key.
0N/A *
0N/A * @serial
0N/A */
0N/A private byte[] key;
0N/A
0N/A /**
0N/A * The name of the algorithm associated with this key.
0N/A *
0N/A * @serial
0N/A */
0N/A private String algorithm;
0N/A
0N/A /**
0N/A * Constructs a secret key from the given byte array.
0N/A *
0N/A * <p>This constructor does not check if the given bytes indeed specify a
0N/A * secret key of the specified algorithm. For example, if the algorithm is
0N/A * DES, this constructor does not check if <code>key</code> is 8 bytes
0N/A * long, and also does not check for weak or semi-weak keys.
0N/A * In order for those checks to be performed, an algorithm-specific
0N/A * <i>key specification</i> class (in this case:
0N/A * {@link DESKeySpec DESKeySpec})
0N/A * should be used.
0N/A *
0N/A * @param key the key material of the secret key. The contents of
0N/A * the array are copied to protect against subsequent modification.
0N/A * @param algorithm the name of the secret-key algorithm to be associated
0N/A * with the given key material.
0N/A * See Appendix A in the <a href=
0N/A * "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
0N/A * Java Cryptography Architecture Reference Guide</a>
0N/A * for information about standard algorithm names.
0N/A * @exception IllegalArgumentException if <code>algorithm</code>
0N/A * is null or <code>key</code> is null or empty.
0N/A */
0N/A public SecretKeySpec(byte[] key, String algorithm) {
0N/A if (key == null || algorithm == null) {
0N/A throw new IllegalArgumentException("Missing argument");
0N/A }
0N/A if (key.length == 0) {
0N/A throw new IllegalArgumentException("Empty key");
0N/A }
0N/A this.key = (byte[])key.clone();
0N/A this.algorithm = algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a secret key from the given byte array, using the first
0N/A * <code>len</code> bytes of <code>key</code>, starting at
0N/A * <code>offset</code> inclusive.
0N/A *
0N/A * <p> The bytes that constitute the secret key are
0N/A * those between <code>key[offset]</code> and
0N/A * <code>key[offset+len-1]</code> inclusive.
0N/A *
0N/A * <p>This constructor does not check if the given bytes indeed specify a
0N/A * secret key of the specified algorithm. For example, if the algorithm is
0N/A * DES, this constructor does not check if <code>key</code> is 8 bytes
0N/A * long, and also does not check for weak or semi-weak keys.
0N/A * In order for those checks to be performed, an algorithm-specific key
0N/A * specification class (in this case:
0N/A * {@link DESKeySpec DESKeySpec})
0N/A * must be used.
0N/A *
0N/A * @param key the key material of the secret key. The first
0N/A * <code>len</code> bytes of the array beginning at
0N/A * <code>offset</code> inclusive are copied to protect
0N/A * against subsequent modification.
0N/A * @param offset the offset in <code>key</code> where the key material
0N/A * starts.
0N/A * @param len the length of the key material.
0N/A * @param algorithm the name of the secret-key algorithm to be associated
0N/A * with the given key material.
0N/A * See Appendix A in the <a href=
0N/A * "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
0N/A * Java Cryptography Architecture Reference Guide</a>
0N/A * for information about standard algorithm names.
0N/A * @exception IllegalArgumentException if <code>algorithm</code>
0N/A * is null or <code>key</code> is null, empty, or too short,
0N/A * i.e. <code>key.length-offset<len</code>.
0N/A * @exception ArrayIndexOutOfBoundsException is thrown if
0N/A * <code>offset</code> or <code>len</code> index bytes outside the
0N/A * <code>key</code>.
0N/A */
0N/A public SecretKeySpec(byte[] key, int offset, int len, String algorithm) {
0N/A if (key == null || algorithm == null) {
0N/A throw new IllegalArgumentException("Missing argument");
0N/A }
0N/A if (key.length == 0) {
0N/A throw new IllegalArgumentException("Empty key");
0N/A }
0N/A if (key.length-offset < len) {
0N/A throw new IllegalArgumentException
0N/A ("Invalid offset/length combination");
0N/A }
0N/A if (len < 0) {
0N/A throw new ArrayIndexOutOfBoundsException("len is negative");
0N/A }
0N/A this.key = new byte[len];
0N/A System.arraycopy(key, offset, this.key, 0, len);
0N/A this.algorithm = algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the algorithm associated with this secret key.
0N/A *
0N/A * @return the secret key algorithm.
0N/A */
0N/A public String getAlgorithm() {
0N/A return this.algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the encoding format for this secret key.
0N/A *
0N/A * @return the string "RAW".
0N/A */
0N/A public String getFormat() {
0N/A return "RAW";
0N/A }
0N/A
0N/A /**
0N/A * Returns the key material of this secret key.
0N/A *
0N/A * @return the key material. Returns a new array
0N/A * each time this method is called.
0N/A */
0N/A public byte[] getEncoded() {
0N/A return (byte[])this.key.clone();
0N/A }
0N/A
0N/A /**
0N/A * Calculates a hash code value for the object.
0N/A * Objects that are equal will also have the same hashcode.
0N/A */
0N/A public int hashCode() {
0N/A int retval = 0;
0N/A for (int i = 1; i < this.key.length; i++) {
0N/A retval += this.key[i] * i;
0N/A }
0N/A if (this.algorithm.equalsIgnoreCase("TripleDES"))
0N/A return (retval ^= "desede".hashCode());
0N/A else
0N/A return (retval ^= this.algorithm.toLowerCase().hashCode());
0N/A }
0N/A
0N/A /**
0N/A * Tests for equality between the specified object and this
0N/A * object. Two SecretKeySpec objects are considered equal if
0N/A * they are both SecretKey instances which have the
0N/A * same case-insensitive algorithm name and key encoding.
0N/A *
0N/A * @param obj the object to test for equality with this object.
0N/A *
0N/A * @return true if the objects are considered equal, false if
0N/A * <code>obj</code> is null or otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (this == obj)
0N/A return true;
0N/A
0N/A if (!(obj instanceof SecretKey))
0N/A return false;
0N/A
0N/A String thatAlg = ((SecretKey)obj).getAlgorithm();
0N/A if (!(thatAlg.equalsIgnoreCase(this.algorithm))) {
0N/A if ((!(thatAlg.equalsIgnoreCase("DESede"))
0N/A || !(this.algorithm.equalsIgnoreCase("TripleDES")))
0N/A && (!(thatAlg.equalsIgnoreCase("TripleDES"))
0N/A || !(this.algorithm.equalsIgnoreCase("DESede"))))
0N/A return false;
0N/A }
0N/A
0N/A byte[] thatKey = ((SecretKey)obj).getEncoded();
0N/A
0N/A return java.util.Arrays.equals(this.key, thatKey);
0N/A }
0N/A}