List of usage examples for org.bouncycastle.crypto.generators PKCS5S2ParametersGenerator generateDerivedMacParameters
public CipherParameters generateDerivedMacParameters(int keySize)
From source file:com.booleanworks.kryptopterus.entities.AppUser.java
public static byte[][] hash(String stringToHash) throws UnsupportedEncodingException { // tuning parameters // these sizes are relatively arbitrary int seedBytes = AppUser.hashAndSaltSize; int hashBytes = AppUser.hashAndSaltSize; // increase iterations as high as your performance can tolerate // since this increases computational cost of password guessing // which should help security int iterations = 1000; // to save a new password: SecureRandom rng = new SecureRandom(); byte[] salt = rng.generateSeed(seedBytes); PKCS5S2ParametersGenerator kdf = new PKCS5S2ParametersGenerator(); kdf.init(stringToHash.getBytes("UTF-8"), salt, iterations); byte[] hash = ((KeyParameter) kdf.generateDerivedMacParameters(8 * hashBytes)).getKey(); // now save salt and hash return new byte[][] { hash, salt }; }
From source file:com.booleanworks.kryptopterus.entities.AppUser.java
public static byte[][] hash(String stringToHash, byte[] sourceSalt) throws UnsupportedEncodingException { // tuning parameters // these sizes are relatively arbitrary int seedBytes = AppUser.hashAndSaltSize; int hashBytes = AppUser.hashAndSaltSize; // increase iterations as high as your performance can tolerate // since this increases computational cost of password guessing // which should help security int iterations = 1000; // to save a new password: SecureRandom rng = new SecureRandom(); byte[] salt = sourceSalt; PKCS5S2ParametersGenerator kdf = new PKCS5S2ParametersGenerator(); kdf.init(stringToHash.getBytes("UTF-8"), salt, iterations); byte[] hash = ((KeyParameter) kdf.generateDerivedMacParameters(8 * hashBytes)).getKey(); // now save salt and hash return new byte[][] { hash, salt }; }
From source file:com.booleanworks.kryptopterus.entities.AppUser.java
public static boolean checkHash(String sourceString, byte[] sourceHash, byte[] sourceSalt) throws UnsupportedEncodingException { // tuning parameters // these sizes are relatimmodificationDateodificationDatevely arbitrary int seedBytes = AppUser.hashAndSaltSize; int hashBytes = AppUser.hashAndSaltSize; // increase iterations as high as your performance can tolerate // since this increases computational cost of password guessing // which should help security int iterations = 1000; // to save a new password: SecureRandom rng = new SecureRandom(); byte[] salt = sourceSalt; PKCS5S2ParametersGenerator kdf = new PKCS5S2ParametersGenerator(); kdf.init(sourceString.getBytes("UTF-8"), salt, iterations); byte[] hash = ((KeyParameter) kdf.generateDerivedMacParameters(8 * hashBytes)).getKey(); if (hash.length != sourceHash.length) { return false; }/*from ww w . j av a 2s . c o m*/ for (int cByte = 0; cByte < hash.length; cByte++) { if (hash[cByte] != sourceHash[cByte]) { return false; } } return true; }
From source file:de.gs_sys.basics.crypto.hmac.PBKDF.java
License:Open Source License
public static byte[] bytePBKDF(String pas, int iterations) { if (pas == null) return null; PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(); // uses PKCS#5 standard generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(pas.toCharArray()), SALT, iterations); // Generate Key 32 chars at 8 bit so total 256 bit KeyParameter key = (KeyParameter) generator.generateDerivedMacParameters(KeyBitSize); return key.getKey(); }
From source file:de.gs_sys.basics.crypto.hmac.PBKDF.java
License:Open Source License
/** * Performs a Password-Based Key Derivation Function *///from w ww. j av a2s .c o m public static byte[] PBKDF(byte[] pas, int iterations) { if (pas == null) return null; PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(); // uses PKCS#5 standard generator.init(pas, SALT, iterations); // Generate Key 32 chars at 8 bit so total 256 bit KeyParameter key = (KeyParameter) generator.generateDerivedMacParameters(KeyBitSize); return key.getKey(); }
From source file:de.gs_sys.basics.crypto.hmac.PBKDF.java
License:Open Source License
/** * Performs a Password-Based Key Derivation Function *///from w w w.ja v a 2s .c o m public static byte[] bigPBKDF(byte[] pas, int iterations, int KeyBitSize) { if (pas == null) return null; PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(); // uses PKCS#5 standard generator.init(pas, SALT, iterations); // Generate Key 32 chars at 8 bit so total 256 bit KeyParameter key = (KeyParameter) generator.generateDerivedMacParameters(KeyBitSize); return key.getKey(); }
From source file:de.gs_sys.basics.crypto.hmac.PBKDF.java
License:Open Source License
public static byte[] bigPBKDF(String pas, int iterations, int KeyBitSize) { if (pas == null) return null; PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(); // uses PKCS#5 standard generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(pas.toCharArray()), SALT, iterations); // Generate Key 32 chars at 8 bit so total 256 bit KeyParameter key = (KeyParameter) generator.generateDerivedMacParameters(KeyBitSize); return key.getKey(); }
From source file:edu.tamu.tcat.crypto.bouncycastle.PBKDF2Impl.java
License:Apache License
@Override public byte[] deriveKey(byte[] password, byte[] salt, int rounds, int keySizeInBytes) { PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(bouncyDigest); generator.init(password, salt, rounds); KeyParameter keyParameter = (KeyParameter) generator.generateDerivedMacParameters(keySizeInBytes * 8); return keyParameter.getKey(); }
From source file:in.gagan.common.util.CommonUtil.java
License:Apache License
public static Map<String, String> convertToHash(String passwordToSave) { // tuning parameters // these sizes are relatively arbitrary int seedBytes = 20; int hashBytes = 20; SecureRandom rng = null;// www .j a v a 2s .c o m byte[] tmpSalt = null; PKCS5S2ParametersGenerator kdf = null; byte[] tmpHash = null; String hash = null; String salt = null; // increase iterations as high as your performance can tolerate // since this increases computational cost of password guessing // which should help security int iterations = 1000; Map<String, String> hashedMap = new HashMap<String, String>(); // to save a new password: try { rng = new SecureRandom(); tmpSalt = rng.generateSeed(seedBytes); kdf = new PKCS5S2ParametersGenerator(); kdf.init(passwordToSave.getBytes("UTF-8"), tmpSalt, iterations); tmpHash = ((KeyParameter) kdf.generateDerivedMacParameters(8 * hashBytes)).getKey(); hash = new String(Base64.encode(tmpHash)); salt = new String(Base64.encode(tmpSalt)); hashedMap.put(ApplicationConstants.SALTS, salt); hashedMap.put(ApplicationConstants.HASHES, hash); return hashedMap; } catch (Exception e) { logger.error("CommonUtil.convert hashing failed " + e); } finally { makeVariablesNull( new Object[] { tmpHash, hash, seedBytes, hashBytes, rng, tmpSalt, kdf, salt, iterations }); } return null; }
From source file:in.gagan.common.util.CommonUtil.java
License:Apache License
public static String convertToHashForPasswordAuthentication(String salt, String password) { PKCS5S2ParametersGenerator kdf = null; int hashBytes = 20; int iterations = 1000; byte[] tmpHash = null; String hash = null;// www . j a va 2 s . co m byte[] tmpSalt = null; try { tmpSalt = Base64.decode(salt); kdf = new PKCS5S2ParametersGenerator(); kdf.init(password.getBytes("UTF-8"), tmpSalt, iterations); tmpHash = ((KeyParameter) kdf.generateDerivedMacParameters(8 * hashBytes)).getKey(); hash = new String(Base64.encode(tmpHash)); return hash; } catch (Exception e) { logger.error("CommonUtils.convertToHashForPasswordAuthentication error: " + e); } finally { makeVariablesNull(new Object[] { tmpHash, hash, hashBytes, tmpSalt, kdf, salt, iterations, password }); } return null; }