Example usage for org.bouncycastle.crypto.generators BCrypt passwordToByteArray

List of usage examples for org.bouncycastle.crypto.generators BCrypt passwordToByteArray

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.generators BCrypt passwordToByteArray.

Prototype

public static byte[] passwordToByteArray(char[] password) 

Source Link

Document

Converts a character password to bytes incorporating the required trailing zero byte.

Usage

From source file:com.distrimind.util.crypto.PasswordHashType.java

License:Open Source License

byte[] hash(char[] password, byte[] salt, byte cost, byte hashLength)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    CodeProvider.encureProviderLoaded(codeProvider);
    if (cost < 4 || cost > 31)
        throw new IllegalArgumentException("cost must be greater or equals than 4 and lower or equals than 31");

    if (defaultOf != null)
        return defaultOf.hash(password, salt, cost, hashLength);
    if (OSVersion.getCurrentOSVersion() != null && OSVersion.getCurrentOSVersion().getOS() == OS.MAC_OS_X) {
        if (this == PBKDF2WithHMacSHA2_256)
            return PasswordHashType.BC_FIPS_PBKFD2WithHMacSHA2_256.hash(password, salt, cost, hashLength);
        if (this == PBKDF2WithHMacSHA2_384)
            return PasswordHashType.BC_FIPS_PBKFD2WithHMacSHA2_384.hash(password, salt, cost, hashLength);
        if (this == PBKDF2WithHMacSHA2_512)
            return PasswordHashType.BC_FIPS_PBKFD2WithHMacSHA2_512.hash(password, salt, cost, hashLength);
    }//ww w  . jav  a2s .  c o m
    int iterations = 1 << (cost - 1);
    int scryptN = 1 << 18;
    switch (this) {
    case DEFAULT:
    case PBKDF2WithHmacSHA1:
    case PBKDF2WithHMacSHA2_256:
    case PBKDF2WithHMacSHA2_384:
    case PBKDF2WithHMacSHA2_512: {
        PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, (hashLength) * 8);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithmName,
                codeProvider.checkProviderWithCurrentOS().name());
        return skf.generateSecret(spec).getEncoded();
    }
    case GNU_PBKDF2WithHMacSHA2_256:
    case GNU_PBKDF2WithHMacSHA2_384:
    case GNU_PBKDF2WithHMacSHA2_512:
    case GNU_PBKDF2WithHMacWhirlpool:
    case GNU_PBKDF2WithHmacSHA1: {
        Object spec = GnuFunctions.PBEKeySpecGetInstance(password, salt, iterations, hashLength);
        Object skf = GnuFunctions.secretKeyFactoryGetInstance(algorithmName);
        return GnuFunctions.keyGetEncoded(GnuFunctions.secretKeyFactoryGenerateSecret(skf, spec));
    }
    case BC_BCRYPT: {

        salt = uniformizeSaltLength(salt, 16);
        return BCrypt.generate(BCrypt.passwordToByteArray(password), salt, cost);
    }
    case BC_FIPS_PBKFD2WithHMacSHA2_256:
    case BC_FIPS_PBKFD2WithHMacSHA2_384:
    case BC_FIPS_PBKFD2WithHMacSHA2_512: {
        PasswordBasedDeriver<org.bouncycastle.crypto.fips.FipsPBKD.Parameters> deriver = new FipsPBKD.DeriverFactory()
                .createDeriver(
                        FipsPBKD.PBKDF2.using(fipsDigestAlgorithm, PasswordConverter.UTF8.convert(password))
                                .withIterationCount(iterations).withSalt(salt));
        return deriver.deriveKey(PasswordBasedDeriver.KeyType.CIPHER, ((hashLength * 8) + 7) / 8);
    }
    case BC_SCRYPT_FOR_LOGIN:
        scryptN = 1 << 13;

    case BC_SCRYPT_FOR_DATAENCRYPTION:
        byte[] passwordb = new byte[password.length * 2];
        for (int i = 0; i < password.length; i++) {
            passwordb[i * 2] = (byte) (password[i] & 0xFF);
            passwordb[i * 2 + 1] = (byte) ((password[i] >> 8 & 0xFFFF) & 0xFF);
        }

        return SCrypt.generate(passwordb, salt, scryptN, 8, 1, hashLength);
    }
    throw new InternalError();
}