Source for javax.crypto.spec.PBEKeySpec

   1: /* PBEKeySpec.java -- Wrapper for password-based keys.
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.crypto.spec;
  40: 
  41: import java.security.spec.KeySpec;
  42: 
  43: /**
  44:  * A wrapper for a password-based key, used for password-based
  45:  * encryption (PBE).
  46:  *
  47:  * <p>Examples of password-based encryption algorithms include:
  48:  *
  49:  * <ul>
  50:  * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/">PKCS #5
  51:  * - Password-Based Cryptography Standard</a></li>
  52:  * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-12/">PKCS
  53:  * #12 - Personal Information Exchange Syntax Standard</a></li>
  54:  * </ul>
  55:  *
  56:  * @author Casey Marshall (csm@gnu.org)
  57:  * @since 1.4
  58:  * @see javax.crypto.SecretKeyFactory
  59:  * @see PBEParameterSpec
  60:  */
  61: public class PBEKeySpec implements KeySpec
  62: {
  63: 
  64:   // Fields.
  65:   // ------------------------------------------------------------------------
  66: 
  67:   /** The iteration count. */
  68:   private int iterationCount;
  69: 
  70:   /** The generated key length. */
  71:   private int keyLength;
  72: 
  73:   /** The password. */
  74:   private char[] password;
  75: 
  76:   /** The salt. */
  77:   private byte[] salt;
  78: 
  79:   // Constructors.
  80:   // ------------------------------------------------------------------------
  81: 
  82:   /**
  83:    * Create a new PBE key spec with just a password.
  84:    *
  85:    * @param password The password char array.
  86:    */
  87:   public PBEKeySpec(char[] password)
  88:   {
  89:     this(password, null, 0, 0);
  90:   }
  91: 
  92:   /**
  93:    * Create a PBE key spec with a password, salt, and iteration count.
  94:    *
  95:    * @param password       The password char array.
  96:    * @param salt           The salt bytes.
  97:    * @param iterationCount The iteration count.
  98:    */
  99:   public PBEKeySpec(char[] password, byte[] salt, int iterationCount)
 100:   {
 101:     this(password, salt, iterationCount, 0);
 102:   }
 103: 
 104:   /**
 105:    * Create a PBE key spec with a password, salt, iteration count, and
 106:    * key length.
 107:    *
 108:    * @param password       The password char array.
 109:    * @param salt           The salt bytes.
 110:    * @param iterationCount The iteration count.
 111:    * @param keyLength      The generated key length.
 112:    */
 113:   public PBEKeySpec(char[] password, byte[] salt, int iterationCount,
 114:                     int keyLength)
 115:   {
 116:     this.password = password;
 117:     this.salt = salt;
 118:     this.iterationCount = iterationCount;
 119:     this.keyLength = keyLength;
 120:   }
 121: 
 122:   // Instance methods.
 123:   // ------------------------------------------------------------------------
 124: 
 125:   /**
 126:    * Clear the password array by filling it with null characters.
 127:    */
 128:   public final void clearPassword()
 129:   {
 130:     if (password == null) return;
 131:     for (int i = 0; i < password.length; i++)
 132:       {
 133:         password[i] = '\u0000';
 134:       }
 135:   }
 136: 
 137:   /**
 138:    * Get the iteration count, or 0 if it has not been specified.
 139:    *
 140:    * @return The iteration count, or 0 if it has not been specified.
 141:    */
 142:   public final int getIterationCount()
 143:   {
 144:     return iterationCount;
 145:   }
 146: 
 147:   /**
 148:    * Get the generated key length, or 0 if it has not been specified.
 149:    *
 150:    * @return The key length, or 0 if it has not been specified.
 151:    */
 152:   public final int getKeyLength()
 153:   {
 154:     return keyLength;
 155:   }
 156: 
 157:   /**
 158:    * Get the password character array.
 159:    *
 160:    * @return The password.
 161:    */
 162:   public final char[] getPassword()
 163:   {
 164:     return password;
 165:   }
 166: 
 167:   /**
 168:    * Get the salt bytes.
 169:    *
 170:    * @return The salt.
 171:    */
 172:   public final byte[] getSalt()
 173:   {
 174:     return salt;
 175:   }
 176: }