Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]
    1   /*
    2    * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package com.sun.crypto.provider;
   27   
   28   import java.security.KeyRep;
   29   import java.security.InvalidKeyException;
   30   import javax.crypto.SecretKey;
   31   import javax.crypto.spec.DESedeKeySpec;
   32   
   33   /**
   34    * This class represents a DES-EDE key.
   35    *
   36    * @author Jan Luehe
   37    *
   38    */
   39   
   40   final class DESedeKey implements SecretKey {
   41   
   42       static final long serialVersionUID = 2463986565756745178L;
   43   
   44       private byte[] key;
   45   
   46       /**
   47        * Creates a DES-EDE key from a given key.
   48        *
   49        * @param key the given key
   50        *
   51        * @exception InvalidKeyException if the given key has a wrong size
   52        */
   53       DESedeKey(byte[] key) throws InvalidKeyException {
   54           this(key, 0);
   55       }
   56   
   57       /**
   58        * Uses the first 24 bytes in <code>key</code>, beginning at
   59        * <code>offset</code>, as the DES-EDE key
   60        *
   61        * @param key the buffer with the DES-EDE key
   62        * @param offset the offset in <code>key</code>, where the DES-EDE key
   63        * starts
   64        *
   65        * @exception InvalidKeyException if the given key has a wrong size
   66        */
   67       DESedeKey(byte[] key, int offset) throws InvalidKeyException {
   68   
   69           if (key==null || ((key.length-offset)<DESedeKeySpec.DES_EDE_KEY_LEN)) {
   70               throw new InvalidKeyException("Wrong key size");
   71           }
   72           this.key = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
   73           System.arraycopy(key, offset, this.key, 0,
   74                            DESedeKeySpec.DES_EDE_KEY_LEN);
   75           DESKeyGenerator.setParityBit(this.key, 0);
   76           DESKeyGenerator.setParityBit(this.key, 8);
   77           DESKeyGenerator.setParityBit(this.key, 16);
   78       }
   79   
   80       public byte[] getEncoded() {
   81           return (byte[])this.key.clone();
   82       }
   83   
   84       public String getAlgorithm() {
   85           return "DESede";
   86       }
   87   
   88       public String getFormat() {
   89           return "RAW";
   90       }
   91   
   92       /**
   93        * Calculates a hash code value for the object.
   94        * Objects that are equal will also have the same hashcode.
   95        */
   96       public int hashCode() {
   97           int retval = 0;
   98           for (int i = 1; i < this.key.length; i++) {
   99               retval += this.key[i] * i;
  100           }
  101           return(retval ^= "desede".hashCode());
  102       }
  103   
  104       public boolean equals(Object obj) {
  105           if (this == obj)
  106               return true;
  107   
  108           if (!(obj instanceof SecretKey))
  109               return false;
  110   
  111           String thatAlg = ((SecretKey)obj).getAlgorithm();
  112           if (!(thatAlg.equalsIgnoreCase("DESede"))
  113               && !(thatAlg.equalsIgnoreCase("TripleDES")))
  114               return false;
  115   
  116           byte[] thatKey = ((SecretKey)obj).getEncoded();
  117           boolean ret = java.util.Arrays.equals(this.key, thatKey);
  118           java.util.Arrays.fill(thatKey, (byte)0x00);
  119           return ret;
  120       }
  121   
  122       /**
  123        * readObject is called to restore the state of this key from
  124        * a stream.
  125        */
  126       private void readObject(java.io.ObjectInputStream s)
  127            throws java.io.IOException, ClassNotFoundException
  128       {
  129           s.defaultReadObject();
  130           key = (byte[])key.clone();
  131       }
  132   
  133       /**
  134        * Replace the DESede key to be serialized.
  135        *
  136        * @return the standard KeyRep object to be serialized
  137        *
  138        * @throws java.io.ObjectStreamException if a new object representing
  139        * this DESede key could not be created
  140        */
  141       private Object writeReplace() throws java.io.ObjectStreamException {
  142           return new KeyRep(KeyRep.Type.SECRET,
  143                           getAlgorithm(),
  144                           getFormat(),
  145                           getEncoded());
  146       }
  147   
  148       /**
  149        * Ensures that the bytes of this key are
  150        * set to zero when there are no more references to it.
  151        */
  152       protected void finalize() throws Throwable {
  153           try {
  154               if (this.key != null) {
  155                   java.util.Arrays.fill(this.key, (byte)0x00);
  156                   this.key = null;
  157               }
  158           } finally {
  159               super.finalize();
  160           }
  161       }
  162   }

Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]