use of javax.crypto.spec.PBEKeySpec in project hbase by apache.
the class Encryption method pbkdf128.
/**
* Return a 128 bit key derived from the concatenation of the supplied
* arguments using PBKDF2WithHmacSHA1 at 10,000 iterations.
*
*/
public static byte[] pbkdf128(byte[]... args) {
byte[] salt = new byte[128];
Bytes.random(salt);
StringBuilder sb = new StringBuilder();
for (byte[] b : args) {
sb.append(Arrays.toString(b));
}
PBEKeySpec spec = new PBEKeySpec(sb.toString().toCharArray(), salt, 10000, 128);
try {
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(spec).getEncoded();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException(e);
}
}
use of javax.crypto.spec.PBEKeySpec in project elasticsearch by elastic.
the class KeyStoreWrapper method setString.
/**
* Set a string setting.
*
* @throws IllegalArgumentException if the value is not ASCII
*/
void setString(String setting, char[] value) throws GeneralSecurityException {
if (ASCII_ENCODER.canEncode(CharBuffer.wrap(value)) == false) {
throw new IllegalArgumentException("Value must be ascii");
}
SecretKey secretKey = secretFactory.generateSecret(new PBEKeySpec(value));
keystore.get().setEntry(setting, new KeyStore.SecretKeyEntry(secretKey), keystorePassword.get());
settingNames.add(setting);
}
use of javax.crypto.spec.PBEKeySpec in project orientdb by orientechnologies.
the class OSymmetricKey method create.
protected void create() {
try {
SecureRandom secureRandom = new SecureRandom();
byte[] salt = secureRandom.generateSeed(saltLength);
KeySpec keySpec = new PBEKeySpec(seedPhrase.toCharArray(), salt, iteration, keySize);
SecretKeyFactory factory = SecretKeyFactory.getInstance(seedAlgorithm);
SecretKey tempKey = factory.generateSecret(keySpec);
secretKey = new SecretKeySpec(tempKey.getEncoded(), secretKeyAlgorithm);
} catch (Exception ex) {
throw new OSecurityException("OSymmetricKey.create() Exception: " + ex);
}
}
use of javax.crypto.spec.PBEKeySpec in project jodd by oblac.
the class PBKDF2Hash method pbkdf2.
/**
* Computes the PBKDF2 hash of a password.
*
* @param password the password to hash.
* @param salt the salt
* @param iterations the iteration count (slowness factor)
* @param bytes the length of the hash to compute in bytes
* @return the PBDKF2 hash of the password
*/
private static byte[] pbkdf2(char[] password, byte[] salt, int iterations, int bytes) {
PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
SecretKeyFactory skf = null;
try {
skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
return skf.generateSecret(spec).getEncoded();
} catch (NoSuchAlgorithmException ignore) {
return null;
} catch (InvalidKeySpecException e) {
throw new IllegalArgumentException(e);
}
}
use of javax.crypto.spec.PBEKeySpec in project platform_frameworks_base by android.
the class BackupManagerService method buildCharArrayKey.
private SecretKey buildCharArrayKey(String algorithm, char[] pwArray, byte[] salt, int rounds) {
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE);
return keyFactory.generateSecret(ks);
} catch (InvalidKeySpecException e) {
Slog.e(TAG, "Invalid key spec for PBKDF2!");
} catch (NoSuchAlgorithmException e) {
Slog.e(TAG, "PBKDF2 unavailable!");
}
return null;
}
Aggregations