0N/A/*
2362N/A * Copyright (c) 1997, 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 com.sun.crypto.provider;
0N/A
0N/Aimport java.security.KeyRep;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport javax.crypto.SecretKey;
0N/Aimport javax.crypto.spec.DESKeySpec;
0N/A
0N/A/**
0N/A * This class represents a DES key.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A */
0N/A
0N/Afinal class DESKey implements SecretKey {
0N/A
0N/A static final long serialVersionUID = 7724971015953279128L;
0N/A
0N/A private byte[] key;
0N/A
0N/A /**
0N/A * Uses the first 8 bytes of the given key as the DES key.
0N/A *
0N/A * @param key the buffer with the DES key bytes.
0N/A *
0N/A * @exception InvalidKeyException if less than 8 bytes are available for
0N/A * the key.
0N/A */
0N/A DESKey(byte[] key) throws InvalidKeyException {
0N/A this(key, 0);
0N/A }
0N/A
0N/A /**
0N/A * Uses the first 8 bytes in <code>key</code>, beginning at
0N/A * <code>offset</code>, as the DES key
0N/A *
0N/A * @param key the buffer with the DES key bytes.
0N/A * @param offset the offset in <code>key</code>, where the DES key bytes
0N/A * start.
0N/A *
0N/A * @exception InvalidKeyException if less than 8 bytes are available for
0N/A * the key.
0N/A */
0N/A DESKey(byte[] key, int offset) throws InvalidKeyException {
0N/A if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {
0N/A throw new InvalidKeyException("Wrong key size");
0N/A }
0N/A this.key = new byte[DESKeySpec.DES_KEY_LEN];
0N/A System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);
0N/A DESKeyGenerator.setParityBit(this.key, 0);
0N/A }
0N/A
0N/A public byte[] getEncoded() {
0N/A // Return a copy of the key, rather than a reference,
0N/A // so that the key data cannot be modified from outside
0N/A return (byte[])this.key.clone();
0N/A }
0N/A
0N/A public String getAlgorithm() {
0N/A return "DES";
0N/A }
0N/A
0N/A public String getFormat() {
0N/A return "RAW";
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 return(retval ^= "des".hashCode());
0N/A }
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("DES")))
0N/A return false;
0N/A
0N/A byte[] thatKey = ((SecretKey)obj).getEncoded();
0N/A boolean ret = java.util.Arrays.equals(this.key, thatKey);
0N/A java.util.Arrays.fill(thatKey, (byte)0x00);
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * readObject is called to restore the state of this key from
0N/A * a stream.
0N/A */
0N/A private void readObject(java.io.ObjectInputStream s)
0N/A throws java.io.IOException, ClassNotFoundException
0N/A {
0N/A s.defaultReadObject();
0N/A key = (byte[])key.clone();
0N/A }
0N/A
0N/A /**
0N/A * Replace the DES key to be serialized.
0N/A *
0N/A * @return the standard KeyRep object to be serialized
0N/A *
0N/A * @throws java.io.ObjectStreamException if a new object representing
0N/A * this DES key could not be created
0N/A */
0N/A private Object writeReplace() throws java.io.ObjectStreamException {
0N/A return new KeyRep(KeyRep.Type.SECRET,
0N/A getAlgorithm(),
0N/A getFormat(),
0N/A getEncoded());
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the bytes of this key are
0N/A * set to zero when there are no more references to it.
0N/A */
0N/A protected void finalize() throws Throwable {
0N/A try {
0N/A if (this.key != null) {
0N/A java.util.Arrays.fill(this.key, (byte)0x00);
0N/A this.key = null;
0N/A }
0N/A } finally {
0N/A super.finalize();
0N/A }
0N/A }
0N/A}