Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/DESedeKey.java
38922 views
/*1* Copyright (c) 1997, 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 com.sun.crypto.provider;2627import java.security.MessageDigest;28import java.security.KeyRep;29import java.security.InvalidKeyException;30import javax.crypto.SecretKey;31import javax.crypto.spec.DESedeKeySpec;3233/**34* This class represents a DES-EDE key.35*36* @author Jan Luehe37*38*/3940final class DESedeKey implements SecretKey {4142static final long serialVersionUID = 2463986565756745178L;4344private byte[] key;4546/**47* Creates a DES-EDE key from a given key.48*49* @param key the given key50*51* @exception InvalidKeyException if the given key has a wrong size52*/53DESedeKey(byte[] key) throws InvalidKeyException {54this(key, 0);55}5657/**58* Uses the first 24 bytes in <code>key</code>, beginning at59* <code>offset</code>, as the DES-EDE key60*61* @param key the buffer with the DES-EDE key62* @param offset the offset in <code>key</code>, where the DES-EDE key63* starts64*65* @exception InvalidKeyException if the given key has a wrong size66*/67DESedeKey(byte[] key, int offset) throws InvalidKeyException {6869if (key==null || ((key.length-offset)<DESedeKeySpec.DES_EDE_KEY_LEN)) {70throw new InvalidKeyException("Wrong key size");71}72this.key = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];73System.arraycopy(key, offset, this.key, 0,74DESedeKeySpec.DES_EDE_KEY_LEN);75DESKeyGenerator.setParityBit(this.key, 0);76DESKeyGenerator.setParityBit(this.key, 8);77DESKeyGenerator.setParityBit(this.key, 16);78}7980public synchronized byte[] getEncoded() {81return this.key.clone();82}8384public String getAlgorithm() {85return "DESede";86}8788public String getFormat() {89return "RAW";90}9192/**93* Calculates a hash code value for the object.94* Objects that are equal will also have the same hashcode.95*/96public int hashCode() {97int retval = 0;98for (int i = 1; i < this.key.length; i++) {99retval += this.key[i] * i;100}101return(retval ^= "desede".hashCode());102}103104public boolean equals(Object obj) {105if (this == obj)106return true;107108if (!(obj instanceof SecretKey))109return false;110111String thatAlg = ((SecretKey)obj).getAlgorithm();112if (!(thatAlg.equalsIgnoreCase("DESede"))113&& !(thatAlg.equalsIgnoreCase("TripleDES")))114return false;115116byte[] thatKey = ((SecretKey)obj).getEncoded();117boolean ret = MessageDigest.isEqual(this.key, thatKey);118java.util.Arrays.fill(thatKey, (byte)0x00);119return ret;120}121122/**123* readObject is called to restore the state of this key from124* a stream.125*/126private void readObject(java.io.ObjectInputStream s)127throws java.io.IOException, ClassNotFoundException128{129s.defaultReadObject();130key = key.clone();131}132133/**134* Replace the DESede key to be serialized.135*136* @return the standard KeyRep object to be serialized137*138* @throws java.io.ObjectStreamException if a new object representing139* this DESede key could not be created140*/141private Object writeReplace() throws java.io.ObjectStreamException {142return new KeyRep(KeyRep.Type.SECRET,143getAlgorithm(),144getFormat(),145getEncoded());146}147148/**149* Ensures that the bytes of this key are150* set to zero when there are no more references to it.151*/152protected void finalize() throws Throwable {153try {154synchronized (this) {155if (this.key != null) {156java.util.Arrays.fill(this.key, (byte)0x00);157this.key = null;158}159}160} finally {161super.finalize();162}163}164}165166167