Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/DESKey.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.DESKeySpec;3233/**34* This class represents a DES key.35*36* @author Jan Luehe37*38*/3940final class DESKey implements SecretKey {4142static final long serialVersionUID = 7724971015953279128L;4344private byte[] key;4546/**47* Uses the first 8 bytes of the given key as the DES key.48*49* @param key the buffer with the DES key bytes.50*51* @exception InvalidKeyException if less than 8 bytes are available for52* the key.53*/54DESKey(byte[] key) throws InvalidKeyException {55this(key, 0);56}5758/**59* Uses the first 8 bytes in <code>key</code>, beginning at60* <code>offset</code>, as the DES key61*62* @param key the buffer with the DES key bytes.63* @param offset the offset in <code>key</code>, where the DES key bytes64* start.65*66* @exception InvalidKeyException if less than 8 bytes are available for67* the key.68*/69DESKey(byte[] key, int offset) throws InvalidKeyException {70if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {71throw new InvalidKeyException("Wrong key size");72}73this.key = new byte[DESKeySpec.DES_KEY_LEN];74System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);75DESKeyGenerator.setParityBit(this.key, 0);76}7778public synchronized byte[] getEncoded() {79// Return a copy of the key, rather than a reference,80// so that the key data cannot be modified from outside81return this.key.clone();82}8384public String getAlgorithm() {85return "DES";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 ^= "des".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("DES")))113return false;114115byte[] thatKey = ((SecretKey)obj).getEncoded();116boolean ret = MessageDigest.isEqual(this.key, thatKey);117java.util.Arrays.fill(thatKey, (byte)0x00);118return ret;119}120121/**122* readObject is called to restore the state of this key from123* a stream.124*/125private void readObject(java.io.ObjectInputStream s)126throws java.io.IOException, ClassNotFoundException127{128s.defaultReadObject();129key = key.clone();130}131132/**133* Replace the DES key to be serialized.134*135* @return the standard KeyRep object to be serialized136*137* @throws java.io.ObjectStreamException if a new object representing138* this DES key could not be created139*/140private Object writeReplace() throws java.io.ObjectStreamException {141return new KeyRep(KeyRep.Type.SECRET,142getAlgorithm(),143getFormat(),144getEncoded());145}146147/**148* Ensures that the bytes of this key are149* set to zero when there are no more references to it.150*/151protected void finalize() throws Throwable {152try {153synchronized (this) {154if (this.key != null) {155java.util.Arrays.fill(this.key, (byte)0x00);156this.key = null;157}158}159} finally {160super.finalize();161}162}163}164165166