use of org.wildfly.security.password.spec.EncryptablePasswordSpec in project wildfly-swarm by wildfly-swarm.
the class AuthCallbackHandler method handle.
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback current : callbacks) {
if (current instanceof NameCallback) {
NameCallback ncb = (NameCallback) current;
ncb.setName(this.userName);
} else if (current instanceof RealmCallback) {
RealmCallback rcb = (RealmCallback) current;
rcb.setText(rcb.getDefaultText());
} else if (current instanceof CredentialCallback) {
CredentialCallback ccb = (CredentialCallback) current;
try {
DigestPasswordAlgorithmSpec algoSpec = new DigestPasswordAlgorithmSpec(this.userName, this.realm);
EncryptablePasswordSpec passwordSpec = new EncryptablePasswordSpec(this.password.toCharArray(), algoSpec);
Password passwd = PasswordFactory.getInstance(ALGORITHM_DIGEST_MD5).generatePassword(passwordSpec);
Credential creds = new PasswordCredential(passwd);
ccb.setCredential(creds);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
} else if (current instanceof PasswordCallback) {
PasswordCallback pcb = (PasswordCallback) current;
pcb.setPassword(this.password.toCharArray());
} else {
throw new UnsupportedCallbackException(current);
}
}
}
use of org.wildfly.security.password.spec.EncryptablePasswordSpec 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;
}
Aggregations