ether-crypto provee hashing y verificación de contraseñas con PBKDF2 sobre javax.crypto, sin dependencias externas.
Instalación
<dependency>
<groupId>dev.rafex.ether.crypto</groupId>
<artifactId>ether-crypto</artifactId>
<version>8.0.0-SNAPSHOT</version>
</dependency>
PasswordHasher — interfaz principal
public interface PasswordHasher {
PasswordHash hash(char[] password, byte[] salt, int iterations);
boolean verify(char[] password, byte[] salt, int iterations, byte[] expectedHash);
}
Uso básico con PBKDF2
PasswordHasher hasher = new PasswordHasherPBKDF2();
byte[] salt = new byte[16];
new SecureRandom().nextBytes(salt);
char[] password = "mi-contraseña-segura".toCharArray();
PasswordHash hash = hasher.hash(password, salt, 310_000);
Arrays.fill(password, '\0');
byte[] storedHash = hash.value();
Verificación
char[] inputPassword = request.password().toCharArray();
boolean valid = hasher.verify(inputPassword, storedSalt, 310_000, storedHash);
Arrays.fill(inputPassword, '\0');
if (!valid) {
throw new UnauthorizedException("Contraseña incorrecta");
}
Integración con ether-di
public class SecurityContainer {
private final Lazy<PasswordHasher> hasher = new Lazy<>(PasswordHasherPBKDF2::new);
public PasswordHasher passwordHasher() { return hasher.get(); }
}
Buenas prácticas
- Usa al menos 310 000 iteraciones (recomendación OWASP 2023 para PBKDF2-HMAC-SHA256)
- Genera sal con SecureRandom, nunca con Random
- Limpia los arrays de contraseña con Arrays.fill(password, '\0') inmediatamente tras usar
- Guarda sal y hash por separado (o codificados juntos en Base64)
Más información