# | Method | PBKDF2 | bcrypt | scrypt | Argon2 |
---|---|---|---|---|---|
1 | Implementation | SecretKeyFactory(PBKDF2WithHmacSHA256) | BCryptPasswordEncoder | SCryptPasswordEncoder | Argon2PasswordEncoder |
2 | prerequisites | JDK 8 or later | Spring Security*1 | Spring Security + Bouncy Castle*2 | Spring Security + Bouncy Castle*3 |
3 | result type | byte[] | String | String | String |
4 | result content | hash value | Modular Crypt Format*4 | Original*5 | ? |
- PBKDF2 with
PBKDF2WithHmacSHA256
is the most portable and FIPS 140-2 compliant way*6*7 to hash password. - bcrypt is an easy way (don't require additional libraries) to get a stronger hashing method when you already use Spring Security, with due consideration of the 72-byte length limitation *8. The output is compliant with Modular Crypt Format, but I didn't test compatibility with
crypt(5)
or PassLib. - You can use scrypt or Argon2 if you can use Spring Security and Bouncy castle.
Note: JavaEE 8 specification has the interface javax.security.enterprise.identitystore.Pbkdf2PasswordHash
to generate a hash with PBKDF2*9. I don't have an environment to evaluate the implementation (Eclipse Soteria) so I omitted it from the list.
*1:https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoder.html
*2:https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/scrypt/SCryptPasswordEncoder.html
*3:https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/argon2/Argon2PasswordEncoder.html
*4:https://docs.spring.io/spring-security/reference/features/authentication/password-storage.html#authentication-password-storage-dpe-format
*5:https://docs.spring.io/spring-security/reference/features/authentication/password-storage.html#authentication-password-storage-dpe-format
*6:https://csrc.nist.gov/csrc/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp2261.pdf
*7:https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
*8:https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#input-limits
*9:https://tomee.apache.org/jakartaee-8.0/javadoc/javax/security/enterprise/identitystore/Pbkdf2PasswordHash.html