jdk/src/java.base/share/classes/com/sun/crypto/provider/PBEKey.java
changeset 25859 3317bb8137f4
parent 25402 0c24d9aa8fb9
child 31695 4d10942c9a7b
equal deleted inserted replaced
25858:836adbf7a2cd 25859:3317bb8137f4
       
     1 /*
       
     2  * Copyright (c) 1997, 2014, 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.spec.InvalidKeySpecException;
       
    30 import java.util.Locale;
       
    31 import javax.crypto.SecretKey;
       
    32 import javax.crypto.spec.PBEKeySpec;
       
    33 
       
    34 /**
       
    35  * This class represents a PBE key.
       
    36  *
       
    37  * @author Jan Luehe
       
    38  *
       
    39  */
       
    40 final class PBEKey implements SecretKey {
       
    41 
       
    42     static final long serialVersionUID = -2234768909660948176L;
       
    43 
       
    44     private byte[] key;
       
    45 
       
    46     private String type;
       
    47 
       
    48     /**
       
    49      * Creates a PBE key from a given PBE key specification.
       
    50      *
       
    51      * @param key the given PBE key specification
       
    52      */
       
    53     PBEKey(PBEKeySpec keySpec, String keytype) throws InvalidKeySpecException {
       
    54         char[] passwd = keySpec.getPassword();
       
    55         if (passwd == null) {
       
    56             // Should allow an empty password.
       
    57             passwd = new char[0];
       
    58         }
       
    59         // Accept "\0" to signify "zero-length password with no terminator".
       
    60         if (!(passwd.length == 1 && passwd[0] == 0)) {
       
    61             for (int i=0; i<passwd.length; i++) {
       
    62                 if ((passwd[i] < '\u0020') || (passwd[i] > '\u007E')) {
       
    63                     throw new InvalidKeySpecException("Password is not ASCII");
       
    64                 }
       
    65             }
       
    66         }
       
    67         this.key = new byte[passwd.length];
       
    68         for (int i=0; i<passwd.length; i++)
       
    69             this.key[i] = (byte) (passwd[i] & 0x7f);
       
    70         java.util.Arrays.fill(passwd, ' ');
       
    71         type = keytype;
       
    72     }
       
    73 
       
    74     public byte[] getEncoded() {
       
    75         return this.key.clone();
       
    76     }
       
    77 
       
    78     public String getAlgorithm() {
       
    79         return type;
       
    80     }
       
    81 
       
    82     public String getFormat() {
       
    83         return "RAW";
       
    84     }
       
    85 
       
    86     /**
       
    87      * Calculates a hash code value for the object.
       
    88      * Objects that are equal will also have the same hashcode.
       
    89      */
       
    90     public int hashCode() {
       
    91         int retval = 0;
       
    92         for (int i = 1; i < this.key.length; i++) {
       
    93             retval += this.key[i] * i;
       
    94         }
       
    95         return(retval ^= getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode());
       
    96     }
       
    97 
       
    98     public boolean equals(Object obj) {
       
    99         if (obj == this)
       
   100             return true;
       
   101 
       
   102         if (!(obj instanceof SecretKey))
       
   103             return false;
       
   104 
       
   105         SecretKey that = (SecretKey)obj;
       
   106 
       
   107         if (!(that.getAlgorithm().equalsIgnoreCase(type)))
       
   108             return false;
       
   109 
       
   110         byte[] thatEncoded = that.getEncoded();
       
   111         boolean ret = java.util.Arrays.equals(this.key, thatEncoded);
       
   112         java.util.Arrays.fill(thatEncoded, (byte)0x00);
       
   113         return ret;
       
   114     }
       
   115 
       
   116     /**
       
   117      * readObject is called to restore the state of this key from
       
   118      * a stream.
       
   119      */
       
   120     private void readObject(java.io.ObjectInputStream s)
       
   121          throws java.io.IOException, ClassNotFoundException
       
   122     {
       
   123         s.defaultReadObject();
       
   124         key = key.clone();
       
   125     }
       
   126 
       
   127 
       
   128     /**
       
   129      * Replace the PBE key to be serialized.
       
   130      *
       
   131      * @return the standard KeyRep object to be serialized
       
   132      *
       
   133      * @throws java.io.ObjectStreamException if a new object representing
       
   134      * this PBE key could not be created
       
   135      */
       
   136     private Object writeReplace() throws java.io.ObjectStreamException {
       
   137         return new KeyRep(KeyRep.Type.SECRET,
       
   138                         getAlgorithm(),
       
   139                         getFormat(),
       
   140                         getEncoded());
       
   141     }
       
   142 
       
   143     /**
       
   144      * Ensures that the password bytes of this key are
       
   145      * set to zero when there are no more references to it.
       
   146      */
       
   147     protected void finalize() throws Throwable {
       
   148         try {
       
   149             if (this.key != null) {
       
   150                 java.util.Arrays.fill(this.key, (byte)0x00);
       
   151                 this.key = null;
       
   152             }
       
   153         } finally {
       
   154             super.finalize();
       
   155         }
       
   156     }
       
   157 }