Search in sources :

Example 1 with IteratedSaltedPasswordAlgorithmSpec

use of org.wildfly.security.password.spec.IteratedSaltedPasswordAlgorithmSpec in project fuse-karaf by jboss-fuse.

the class MaskedPasswordHelper method createConfiguration.

@Override
public Map<String, String> createConfiguration(final Map<String, String> attributes) throws GeneralSecurityException, IOException {
    final Provider provider = ProviderHelper.provider(option(attributes, "provider", ProviderHelper.WILDFLY_PROVIDER));
    final String algorithm = option(attributes, "algorithm", DEFAULT_ALGORITHM);
    final PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm, provider);
    final String password = option(attributes, "password", null);
    final String salt = option(attributes, "salt", "");
    final String iterations = option(attributes, "iterations", "");
    final AlgorithmParameterSpec algorithmParameterSpec;
    if (salt.isEmpty() && iterations.isEmpty()) {
        algorithmParameterSpec = null;
    } else if (salt.isEmpty()) {
        algorithmParameterSpec = new IteratedPasswordAlgorithmSpec(parseInt(iterations));
    } else {
        final byte[] saltBytes = Base64.getDecoder().decode(salt);
        algorithmParameterSpec = new IteratedSaltedPasswordAlgorithmSpec(parseInt(iterations), saltBytes);
    }
    final EncryptablePasswordSpec keySpec = new EncryptablePasswordSpec(password.toCharArray(), algorithmParameterSpec);
    final MaskedPassword maskedPassword = passwordFactory.generatePassword(keySpec).castAs(MaskedPassword.class);
    final MaskedPasswordAlgorithmSpec maskedPasswordAlgorithmSpec = maskedPassword.getParameterSpec();
    final Map<String, String> configuration = new HashMap<>();
    final Encoder encoder = Base64.getEncoder();
    if (!DEFAULT_ALGORITHM.equals(algorithm)) {
        configuration.put(CREDENTIAL_STORE_PROTECTION_ALGORITHM, algorithm);
    }
    configuration.put(CREDENTIAL_STORE_PROTECTION, encoder.encodeToString(maskedPassword.getMaskedPasswordBytes()));
    final AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(algorithm, provider);
    algorithmParameters.init(maskedPasswordAlgorithmSpec);
    final byte[] encoded = algorithmParameters.getEncoded();
    configuration.put(CREDENTIAL_STORE_PROTECTION_PARAMS, encoder.encodeToString(encoded));
    return configuration;
}
Also used : HashMap(java.util.HashMap) EncryptablePasswordSpec(org.wildfly.security.password.spec.EncryptablePasswordSpec) Provider(java.security.Provider) IteratedPasswordAlgorithmSpec(org.wildfly.security.password.spec.IteratedPasswordAlgorithmSpec) PasswordFactory(org.wildfly.security.password.PasswordFactory) Encoder(java.util.Base64.Encoder) IteratedSaltedPasswordAlgorithmSpec(org.wildfly.security.password.spec.IteratedSaltedPasswordAlgorithmSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) MaskedPassword(org.wildfly.security.password.interfaces.MaskedPassword) MaskedPasswordAlgorithmSpec(org.wildfly.security.password.spec.MaskedPasswordAlgorithmSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmParameters (java.security.AlgorithmParameters)1 Provider (java.security.Provider)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 Encoder (java.util.Base64.Encoder)1 HashMap (java.util.HashMap)1 PasswordFactory (org.wildfly.security.password.PasswordFactory)1 MaskedPassword (org.wildfly.security.password.interfaces.MaskedPassword)1 EncryptablePasswordSpec (org.wildfly.security.password.spec.EncryptablePasswordSpec)1 IteratedPasswordAlgorithmSpec (org.wildfly.security.password.spec.IteratedPasswordAlgorithmSpec)1 IteratedSaltedPasswordAlgorithmSpec (org.wildfly.security.password.spec.IteratedSaltedPasswordAlgorithmSpec)1 MaskedPasswordAlgorithmSpec (org.wildfly.security.password.spec.MaskedPasswordAlgorithmSpec)1