Path: blob/master/src/java.base/share/classes/javax/crypto/spec/PBEKeySpec.java
41159 views
/*1* Copyright (c) 1997, 2018, 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 javax.crypto.spec;2627import java.security.spec.KeySpec;28import java.util.Arrays;2930/**31* A user-chosen password that can be used with password-based encryption32* (<i>PBE</i>).33*34* <p>The password can be viewed as some kind of raw key material, from which35* the encryption mechanism that uses it derives a cryptographic key.36*37* <p>Different PBE mechanisms may consume different bits of each password38* character. For example, the PBE mechanism defined in39* <a href="http://www.ietf.org/rfc/rfc2898.txt">40* PKCS #5</a> looks at only the low order 8 bits of each character, whereas41* PKCS #12 looks at all 16 bits of each character.42*43* <p>You convert the password characters to a PBE key by creating an44* instance of the appropriate secret-key factory. For example, a secret-key45* factory for PKCS #5 will construct a PBE key from only the low order 8 bits46* of each password character, whereas a secret-key factory for PKCS #12 will47* take all 16 bits of each character.48*49* <p>Also note that this class stores passwords as char arrays instead of50* <code>String</code> objects (which would seem more logical), because the51* String class is immutable and there is no way to overwrite its52* internal value when the password stored in it is no longer needed. Hence,53* this class requests the password as a char array, so it can be overwritten54* when done.55*56* @author Jan Luehe57* @author Valerie Peng58*59* @see javax.crypto.SecretKeyFactory60* @see PBEParameterSpec61* @since 1.462*/63public class PBEKeySpec implements KeySpec {6465private char[] password;66private byte[] salt = null;67private int iterationCount = 0;68private int keyLength = 0;6970/**71* Constructor that takes a password. An empty char[] is used if72* null is specified.73*74* <p> Note: <code>password</code> is cloned before it is stored in75* the new <code>PBEKeySpec</code> object.76*77* @param password the password.78*/79public PBEKeySpec(char[] password) {80if ((password == null) || (password.length == 0)) {81this.password = new char[0];82} else {83this.password = password.clone();84}85}868788/**89* Constructor that takes a password, salt, iteration count, and90* to-be-derived key length for generating PBEKey of variable-key-size91* PBE ciphers. An empty char[] is used if null is specified for92* <code>password</code>.93*94* <p> Note: the <code>password</code> and <code>salt</code>95* are cloned before they are stored in96* the new <code>PBEKeySpec</code> object.97*98* @param password the password.99* @param salt the salt.100* @param iterationCount the iteration count.101* @param keyLength the to-be-derived key length.102* @exception NullPointerException if <code>salt</code> is null.103* @exception IllegalArgumentException if <code>salt</code> is empty,104* i.e. 0-length, <code>iterationCount</code> or105* <code>keyLength</code> is not positive.106*/107public PBEKeySpec(char[] password, byte[] salt, int iterationCount,108int keyLength) {109if ((password == null) || (password.length == 0)) {110this.password = new char[0];111} else {112this.password = password.clone();113}114if (salt == null) {115throw new NullPointerException("the salt parameter " +116"must be non-null");117} else if (salt.length == 0) {118throw new IllegalArgumentException("the salt parameter " +119"must not be empty");120} else {121this.salt = salt.clone();122}123if (iterationCount<=0) {124throw new IllegalArgumentException("invalid iterationCount value");125}126if (keyLength<=0) {127throw new IllegalArgumentException("invalid keyLength value");128}129this.iterationCount = iterationCount;130this.keyLength = keyLength;131}132133134/**135* Constructor that takes a password, salt, iteration count for136* generating PBEKey of fixed-key-size PBE ciphers. An empty137* char[] is used if null is specified for <code>password</code>.138*139* <p> Note: the <code>password</code> and <code>salt</code>140* are cloned before they are stored in the new141* <code>PBEKeySpec</code> object.142*143* @param password the password.144* @param salt the salt.145* @param iterationCount the iteration count.146* @exception NullPointerException if <code>salt</code> is null.147* @exception IllegalArgumentException if <code>salt</code> is empty,148* i.e. 0-length, or <code>iterationCount</code> is not positive.149*/150public PBEKeySpec(char[] password, byte[] salt, int iterationCount) {151if ((password == null) || (password.length == 0)) {152this.password = new char[0];153} else {154this.password = password.clone();155}156if (salt == null) {157throw new NullPointerException("the salt parameter " +158"must be non-null");159} else if (salt.length == 0) {160throw new IllegalArgumentException("the salt parameter " +161"must not be empty");162} else {163this.salt = salt.clone();164}165if (iterationCount<=0) {166throw new IllegalArgumentException("invalid iterationCount value");167}168this.iterationCount = iterationCount;169}170171/**172* Clears the internal copy of the password.173*174*/175public final void clearPassword() {176if (password != null) {177Arrays.fill(password, ' ');178password = null;179}180}181182/**183* Returns a copy of the password.184*185* <p> Note: this method returns a copy of the password. It is186* the caller's responsibility to zero out the password information after187* it is no longer needed.188*189* @exception IllegalStateException if password has been cleared by190* calling <code>clearPassword</code> method.191* @return the password.192*/193public final char[] getPassword() {194if (password == null) {195throw new IllegalStateException("password has been cleared");196}197return password.clone();198}199200/**201* Returns a copy of the salt or null if not specified.202*203* <p> Note: this method should return a copy of the salt. It is204* the caller's responsibility to zero out the salt information after205* it is no longer needed.206*207* @return the salt.208*/209public final byte[] getSalt() {210if (salt != null) {211return salt.clone();212} else {213return null;214}215}216217/**218* Returns the iteration count or 0 if not specified.219*220* @return the iteration count.221*/222public final int getIterationCount() {223return iterationCount;224}225226/**227* Returns the to-be-derived key length or 0 if not specified.228*229* <p> Note: this is used to indicate the preference on key length230* for variable-key-size ciphers. The actual key size depends on231* each provider's implementation.232*233* @return the to-be-derived key length.234*/235public final int getKeyLength() {236return keyLength;237}238}239240241