Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/crypto/spec/SecretKeySpec.java
38918 views
/*1* Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto.spec;2627import java.security.MessageDigest;28import java.security.spec.KeySpec;29import java.util.Locale;30import javax.crypto.SecretKey;3132/**33* This class specifies a secret key in a provider-independent fashion.34*35* <p>It can be used to construct a <code>SecretKey</code> from a byte array,36* without having to go through a (provider-based)37* <code>SecretKeyFactory</code>.38*39* <p>This class is only useful for raw secret keys that can be represented as40* a byte array and have no key parameters associated with them, e.g., DES or41* Triple DES keys.42*43* @author Jan Luehe44*45* @see javax.crypto.SecretKey46* @see javax.crypto.SecretKeyFactory47* @since 1.448*/49public class SecretKeySpec implements KeySpec, SecretKey {5051private static final long serialVersionUID = 6577238317307289933L;5253/**54* The secret key.55*56* @serial57*/58private byte[] key;5960/**61* The name of the algorithm associated with this key.62*63* @serial64*/65private String algorithm;6667/**68* Constructs a secret key from the given byte array.69*70* <p>This constructor does not check if the given bytes indeed specify a71* secret key of the specified algorithm. For example, if the algorithm is72* DES, this constructor does not check if <code>key</code> is 8 bytes73* long, and also does not check for weak or semi-weak keys.74* In order for those checks to be performed, an algorithm-specific75* <i>key specification</i> class (in this case:76* {@link DESKeySpec DESKeySpec})77* should be used.78*79* @param key the key material of the secret key. The contents of80* the array are copied to protect against subsequent modification.81* @param algorithm the name of the secret-key algorithm to be associated82* with the given key material.83* See Appendix A in the <a href=84* "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">85* Java Cryptography Architecture Reference Guide</a>86* for information about standard algorithm names.87* @exception IllegalArgumentException if <code>algorithm</code>88* is null or <code>key</code> is null or empty.89*/90public SecretKeySpec(byte[] key, String algorithm) {91if (key == null || algorithm == null) {92throw new IllegalArgumentException("Missing argument");93}94if (key.length == 0) {95throw new IllegalArgumentException("Empty key");96}97this.key = key.clone();98this.algorithm = algorithm;99}100101/**102* Constructs a secret key from the given byte array, using the first103* <code>len</code> bytes of <code>key</code>, starting at104* <code>offset</code> inclusive.105*106* <p> The bytes that constitute the secret key are107* those between <code>key[offset]</code> and108* <code>key[offset+len-1]</code> inclusive.109*110* <p>This constructor does not check if the given bytes indeed specify a111* secret key of the specified algorithm. For example, if the algorithm is112* DES, this constructor does not check if <code>key</code> is 8 bytes113* long, and also does not check for weak or semi-weak keys.114* In order for those checks to be performed, an algorithm-specific key115* specification class (in this case:116* {@link DESKeySpec DESKeySpec})117* must be used.118*119* @param key the key material of the secret key. The first120* <code>len</code> bytes of the array beginning at121* <code>offset</code> inclusive are copied to protect122* against subsequent modification.123* @param offset the offset in <code>key</code> where the key material124* starts.125* @param len the length of the key material.126* @param algorithm the name of the secret-key algorithm to be associated127* with the given key material.128* See Appendix A in the <a href=129* "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">130* Java Cryptography Architecture Reference Guide</a>131* for information about standard algorithm names.132* @exception IllegalArgumentException if <code>algorithm</code>133* is null or <code>key</code> is null, empty, or too short,134* i.e. {@code key.length-offset<len}.135* @exception ArrayIndexOutOfBoundsException is thrown if136* <code>offset</code> or <code>len</code> index bytes outside the137* <code>key</code>.138*/139public SecretKeySpec(byte[] key, int offset, int len, String algorithm) {140if (key == null || algorithm == null) {141throw new IllegalArgumentException("Missing argument");142}143if (key.length == 0) {144throw new IllegalArgumentException("Empty key");145}146if (key.length-offset < len) {147throw new IllegalArgumentException148("Invalid offset/length combination");149}150if (len < 0) {151throw new ArrayIndexOutOfBoundsException("len is negative");152}153this.key = new byte[len];154System.arraycopy(key, offset, this.key, 0, len);155this.algorithm = algorithm;156}157158/**159* Returns the name of the algorithm associated with this secret key.160*161* @return the secret key algorithm.162*/163public String getAlgorithm() {164return this.algorithm;165}166167/**168* Returns the name of the encoding format for this secret key.169*170* @return the string "RAW".171*/172public String getFormat() {173return "RAW";174}175176/**177* Returns the key material of this secret key.178*179* @return the key material. Returns a new array180* each time this method is called.181*/182public byte[] getEncoded() {183return this.key.clone();184}185186/**187* Calculates a hash code value for the object.188* Objects that are equal will also have the same hashcode.189*/190public int hashCode() {191int retval = 0;192for (int i = 1; i < this.key.length; i++) {193retval += this.key[i] * i;194}195if (this.algorithm.equalsIgnoreCase("TripleDES"))196return (retval ^= "desede".hashCode());197else198return (retval ^=199this.algorithm.toLowerCase(Locale.ENGLISH).hashCode());200}201202/**203* Tests for equality between the specified object and this204* object. Two SecretKeySpec objects are considered equal if205* they are both SecretKey instances which have the206* same case-insensitive algorithm name and key encoding.207*208* @param obj the object to test for equality with this object.209*210* @return true if the objects are considered equal, false if211* <code>obj</code> is null or otherwise.212*/213public boolean equals(Object obj) {214if (this == obj)215return true;216217if (!(obj instanceof SecretKey))218return false;219220String thatAlg = ((SecretKey)obj).getAlgorithm();221if (!(thatAlg.equalsIgnoreCase(this.algorithm))) {222if ((!(thatAlg.equalsIgnoreCase("DESede"))223|| !(this.algorithm.equalsIgnoreCase("TripleDES")))224&& (!(thatAlg.equalsIgnoreCase("TripleDES"))225|| !(this.algorithm.equalsIgnoreCase("DESede"))))226return false;227}228229byte[] thatKey = ((SecretKey)obj).getEncoded();230231return MessageDigest.isEqual(this.key, thatKey);232}233}234235236