/*
 * Copyright (c) 2008 Bradley W. Kimmel
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package ca.eandb.util.auth;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

import ca.eandb.util.UnexpectedException;

/**
 * Implements one-way hashes for passwords.  Based on
 * <a href="http://www.javacodegeeks.com/2012/05/secure-password-storage-donts-dos-and.html">Secure
 * Password Storage – Don’ts, dos and a Java example</a>.

 * @author Brad Kimmel
 */
public final class PasswordEncryptionService {

  public boolean authenticate(String attemptedPassword,
      byte[] encryptedPassword, byte[] salt) {
    // Encrypt the clear-text password using the same salt that was used to
    // encrypt the original password
    byte[] encryptedAttemptedPassword = getEncryptedPassword(
        attemptedPassword, salt);

    // Authentication succeeds if encrypted password that the user entered
    // is equal to the stored hash
    return Arrays.equals(encryptedPassword, encryptedAttemptedPassword);
  }

  public byte[] getEncryptedPassword(String password, byte[] salt) {
    // PBKDF2 with SHA-1 as the hashing algorithm. Note that the NIST
    // specifically names SHA-1 as an acceptable hashing algorithm for
    // PBKDF2
    String algorithm = "PBKDF2WithHmacSHA1";
    // SHA-1 generates 160 bit hashes, so that's what makes sense here
    int derivedKeyLength = 160;
    // Pick an iteration count that works for you. The NIST recommends at
    // least 1,000 iterations:
    // http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
    // iOS 4.x reportedly uses 10,000:
    // http://blog.crackpassword.com/2010/09/smartphone-forensics-cracking-blackberry-backup-passwords/
    int iterations = 20000;

    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations,
        derivedKeyLength);

    try {

      SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);

      return f.generateSecret(spec).getEncoded();

    } catch (InvalidKeySpecException e) {
      throw new UnexpectedException(e);
    } catch (NoSuchAlgorithmException e) {
      throw new UnexpectedException(e);
    }
  }

  public byte[] generateSalt() {
    try {
      // VERY important to use SecureRandom instead of just Random
      SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

      // Generate a 8 byte (64 bit) salt as recommended by RSA PKCS5
      byte[] salt = new byte[8];
      random.nextBytes(salt);

      return salt;
    } catch (NoSuchAlgorithmException e) {
      throw new UnexpectedException(e);
    }
  }

}

@@ -1648,15 +1648,12 @@
 rage
- %E2%80%93
  Don
-%E2%80%99
 ts, 

Fix javadoc comment