Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.exoplatform.web.security.hash;
 
 
 
A SaltedHashService implementation using a javax.crypto.SecretKeyFactory for salted hashing.

Author(s):
Peter Palaga
 
 public class JCASaltedHashService implements SaltedHashService {
 
     public static final String PBKDF2_WITH_HMAC_SHA1 = "PBKDF2WithHmacSHA1";
     public static final int DEFAULT_SALT_BYTE_LENGTH = 9;
     public static final int DEFAULT_HASH_BYTE_LENGTH = 9;
     public static final int DEFAULT_ITERATION_COUNT = 1000;

    
The number of iterations submitted to javax.crypto.SecretKeyFactory.generateSecret(java.security.spec.KeySpec).

From http://en.wikipedia.org/wiki/PBKDF2 (retrieved 2013-01-08): When the standard was written in 2000, the recommended minimum number of iterations was 1000, but the parameter is intended to be increased over time as CPU speeds increase. [...] Apple's iOS 3 uses 2,000 iterations and iOS 4 uses 10,000.

 
     private int iterationCount;

    
Salt length in bytes. Use values divisible by 3 to get salted hash strings without Base64 padding characters.
 
     private int saltByteLength;

    
Hash length in bytes. Use values divisible by 3 to get salted hash strings without Base64 padding characters.
 
     private int hashByteLength;

    
Name of the hashing algorithm which is submitted to javax.crypto.SecretKeyFactory.getInstance(java.lang.String).
 
     private String algorithm;

    
Pluggable codec for serialization and deserialization of SaltedHash objects.
 
     private SaltedHashCodec codec;

    
 
     private final Logger log = LoggerFactory.getLogger(JCASaltedHashService.class);

    

Parameters:
algorithm
iterationCount
saltLength
hashLength
codec
 
     public JCASaltedHashService(String algorithmint iterationCountint saltLengthint hashLengthSaltedHashCodec codec) {
         super();
         this. = algorithm;
         this. = iterationCount;
        this. = saltLength;
        this. = hashLength;
        this. = codec;
    }

    
    public JCASaltedHashService() {
                .);
    }
    /*
     * (non-Javadoc)
     *
     * @see org.exoplatform.web.security.hash.SaltedHashService#getSaltedHash(java.lang.String, java.security.SecureRandom)
     */
    @Override
    public String getSaltedHash(String passwordthrows SaltedHashException {
        try {
            byte[] salt = new byte[];
            PortalContainer container = PortalContainer.getInstance();
            SecureRandom random = ((SecureRandomServicecontainer.getComponentInstanceOfType(SecureRandomService.class)).getSecureRandom();
            random.nextBytes(salt);
            SaltedHash saltedHash = new SaltedHash(salthash(this.passwordsalt,
                    ));
            return .encode(saltedHash);
        } catch (InvalidKeySpecException e) {
            throw new SaltedHashException("Could not create salted hash from password."e);
        } catch (NoSuchAlgorithmException e) {
            throw new SaltedHashException("Could not create salted hash from password."e);
        }
    }
    /*
     * (non-Javadoc)
     *
     * @see org.exoplatform.web.security.hash.SaltedHashService#validate(java.lang.String, java.lang.String)
     */
    @Override
    public boolean validate(String passwordString encodedSaltedHashthrows SaltedHashException {
        try {
            SaltedHash saltedHash = .decode(encodedSaltedHash);
            byte[] expectedHash = hash(saltedHash.getAlgorithm(), passwordsaltedHash.getSalt(),
                    saltedHash.getIterationCount(), saltedHash.getHash().length);
            if (.isDebugEnabled()) {
                .debug("About to validate submitted hash " + Arrays.toString(expectedHash) + " against stored hash "
                        + Arrays.toString(saltedHash.getHash()));
            }
            return Arrays.equals(expectedHashsaltedHash.getHash());
        } catch (NoSuchAlgorithmException e) {
            throw new SaltedHashException("Could not validate password against salted hash."e);
        } catch (InvalidKeySpecException e) {
            throw new SaltedHashException("Could not validate password against salted hash."e);
        }
    }

    
Computes the hash as a byte array.

Parameters:
algorithm
password
salt
iterationCount
hashLength
Returns:
Throws:
java.security.spec.InvalidKeySpecException
java.security.NoSuchAlgorithmException
    private static byte[] hash(String algorithmString passwordbyte[] saltint iterationCountint hashLength)
            throws InvalidKeySpecExceptionNoSuchAlgorithmException {
        SecretKey key = SecretKeyFactory.getInstance(algorithm).generateSecret(
                new PBEKeySpec(password.toCharArray(), saltiterationCounthashLength * 8));
        return key.getEncoded();
    }

    

Returns:
the iterationCount
See also:
iterationCount
    public int getIterationCount() {
        return ;
    }

    

Parameters:
iterationCount the iterationCount to set
See also:
iterationCount
    public void setIterationCount(int iterationCount) {
        this. = iterationCount;
    }

    

Returns:
the saltLength
See also:
saltByteLength
    public int getSaltByteLength() {
        return ;
    }

    

Parameters:
saltLength the saltLength to set
See also:
saltByteLength
    public void setSaltLength(int saltLength) {
        this. = saltLength;
    }

    

Returns:
the hashLength
See also:
hashByteLength
    public int getHashByteLength() {
        return ;
    }

    

Parameters:
hashLength the hashLength to set
See also:
hashByteLength
    public void setHashLength(int hashLength) {
        this. = hashLength;
    }

    

Returns:
the algorithm
See also:
algorithm
    public String getAlgorithm() {
        return ;
    }

    

Parameters:
algorithm the algorithm to set
See also:
algorithm
    public void setAlgorithm(String algorithm) {
        this. = algorithm;
    }

    

Returns:
the codec
See also:
codec
    public SaltedHashCodec getCodec() {
        return ;
    }

    

Parameters:
codec the codec to set
See also:
codec
    public void setCodec(SaltedHashCodec codec) {
        this. = codec;
    }
New to GrepCode? Check out our FAQ X