1
0
mirror of synced 2026-08-04 01:07:02 +00:00

Use include-code for password4j docs

This follows the new convention of using include-code going forward to
ensure that the documentation compiles and is tested. This also corrected
a few errors in custom params for Ballooning and PBKDF2 examples.

Issue gh-17706
This commit is contained in:
Rob Winch
2025-09-15 11:03:44 -05:00
parent 9f839384e9
commit d0372efadd
13 changed files with 487 additions and 141 deletions
@@ -483,37 +483,15 @@ This provides an alternative to Spring Security's built-in `Argon2PasswordEncode
Argon2 is the winner of the https://en.wikipedia.org/wiki/Password_Hashing_Competition[Password Hashing Competition] and is recommended for new applications.
This implementation leverages Password4j's Argon2 support which properly includes the salt in the output hash.
Create an encoder with default settings:
.Argon2Password4jPasswordEncoder
[tabs]
======
Java::
+
[source,java,role="primary"]
----
// Create an encoder with default settings
Argon2Password4jPasswordEncoder encoder = new Argon2Password4jPasswordEncoder();
String result = encoder.encode("myPassword");
assertTrue(encoder.matches("myPassword", result));
include-code::./Argon2UsageTests[tag=default-params,indent=0]
// Create an encoder with custom Argon2 function
Argon2Function customArgon2 = Argon2Function.getInstance(65536, 3, 4, 32, Argon2.ID);
Argon2Password4jPasswordEncoder customEncoder = new Argon2Password4jPasswordEncoder(customArgon2);
----
Create an encoder with custom Argon2 parameters:
Kotlin::
+
[source,kotlin,role="secondary"]
----
// Create an encoder with default settings
val encoder = Argon2Password4jPasswordEncoder()
val result: String = encoder.encode("myPassword")
assertTrue(encoder.matches("myPassword", result))
// Create an encoder with custom Argon2 function
val customArgon2 = Argon2Function.getInstance(65536, 3, 4, 32, Argon2.ID)
val customEncoder = Argon2Password4jPasswordEncoder(customArgon2)
----
======
.Argon2Password4jPasswordEncoder Custom
include-code::./Argon2UsageTests[tag=custom-params,indent=0]
[[password4j-bcrypt]]
=== BcryptPassword4jPasswordEncoder
@@ -524,37 +502,15 @@ This provides an alternative to Spring Security's built-in `BCryptPasswordEncode
BCrypt is a well-established password hashing algorithm that includes built-in salt generation and is resistant to rainbow table attacks.
This implementation leverages Password4j's BCrypt support which properly includes the salt in the output hash.
Create an encoder with default settings:
.BcryptPassword4jPasswordEncoder
[tabs]
======
Java::
+
[source,java,role="primary"]
----
// Create an encoder with default settings
BcryptPassword4jPasswordEncoder encoder = new BcryptPassword4jPasswordEncoder();
String result = encoder.encode("myPassword");
assertTrue(encoder.matches("myPassword", result));
include-code::./BcryptUsageTests[tag=default-params,indent=0]
// Create an encoder with custom round count
BcryptFunction customBcrypt = BcryptFunction.getInstance(12);
BcryptPassword4jPasswordEncoder customEncoder = new BcryptPassword4jPasswordEncoder(customBcrypt);
----
Create an encoder with custom bcrypt parameters:
Kotlin::
+
[source,kotlin,role="secondary"]
----
// Create an encoder with default settings
val encoder = BcryptPassword4jPasswordEncoder()
val result: String = encoder.encode("myPassword")
assertTrue(encoder.matches("myPassword", result))
// Create an encoder with custom round count
val customBcrypt = BcryptFunction.getInstance(12)
val customEncoder = BcryptPassword4jPasswordEncoder(customBcrypt)
----
======
.BcryptPassword4jPasswordEncoder Custom
include-code::./BcryptUsageTests[tag=custom-params,indent=0]
[[password4j-scrypt]]
=== ScryptPassword4jPasswordEncoder
@@ -565,37 +521,16 @@ This provides an alternative to Spring Security's built-in `SCryptPasswordEncode
SCrypt is a memory-hard password hashing algorithm designed to be resistant to hardware brute-force attacks.
This implementation leverages Password4j's SCrypt support which properly includes the salt in the output hash.
Create an encoder with default settings:
.ScryptPassword4jPasswordEncoder
[tabs]
======
Java::
+
[source,java,role="primary"]
----
// Create an encoder with default settings
ScryptPassword4jPasswordEncoder encoder = new ScryptPassword4jPasswordEncoder();
String result = encoder.encode("myPassword");
assertTrue(encoder.matches("myPassword", result));
include-code::./ScryptUsageTests[tag=default-params,indent=0]
// Create an encoder with custom SCrypt parameters
ScryptFunction customScrypt = ScryptFunction.getInstance(32768, 8, 1, 32);
ScryptPassword4jPasswordEncoder customEncoder = new ScryptPassword4jPasswordEncoder(customScrypt);
----
Create an encoder with custom scrypt parameters:
Kotlin::
+
[source,kotlin,role="secondary"]
----
// Create an encoder with default settings
val encoder = ScryptPassword4jPasswordEncoder()
val result: String = encoder.encode("myPassword")
assertTrue(encoder.matches("myPassword", result))
// Create an encoder with custom SCrypt parameters
val customScrypt = ScryptFunction.getInstance(32768, 8, 1, 32)
val customEncoder = ScryptPassword4jPasswordEncoder(customScrypt)
----
======
.ScryptPassword4jPasswordEncoder Custom
include-code::./ScryptUsageTests[tag=custom-params,indent=0]
[[password4j-pbkdf2]]
=== Pbkdf2Password4jPasswordEncoder
@@ -607,37 +542,15 @@ PBKDF2 is a key derivation function designed to be computationally expensive to
This implementation handles salt management explicitly since Password4j's PBKDF2 implementation does not include the salt in the output hash.
The encoded password format is: `+{salt}:{hash}+` where both salt and hash are Base64 encoded.
Create an encoder with default settings:
.Pbkdf2Password4jPasswordEncoder
[tabs]
======
Java::
+
[source,java,role="primary"]
----
// Create an encoder with default settings
Pbkdf2Password4jPasswordEncoder encoder = new Pbkdf2Password4jPasswordEncoder();
String result = encoder.encode("myPassword");
assertTrue(encoder.matches("myPassword", result));
include-code::./Pbkdf2UsageTests[tag=default-params,indent=0]
// Create an encoder with custom PBKDF2 function and salt length
PBKDF2Function customPbkdf2 = PBKDF2Function.getInstance(Algorithm.HMAC_SHA256, 100000, 256);
Pbkdf2Password4jPasswordEncoder customEncoder = new Pbkdf2Password4jPasswordEncoder(customPbkdf2, 32);
----
Create an encoder with custom PBKDF2 parameters:
Kotlin::
+
[source,kotlin,role="secondary"]
----
// Create an encoder with default settings
val encoder = Pbkdf2Password4jPasswordEncoder()
val result: String = encoder.encode("myPassword")
assertTrue(encoder.matches("myPassword", result))
// Create an encoder with custom PBKDF2 function and salt length
val customPbkdf2 = PBKDF2Function.getInstance(Algorithm.HMAC_SHA256, 100000, 256)
val customEncoder = Pbkdf2Password4jPasswordEncoder(customPbkdf2, 32)
----
======
.Pbkdf2Password4jPasswordEncoder Custom
include-code::./Pbkdf2UsageTests[tag=custom-params,indent=0]
[[password4j-ballooning]]
=== BalloonHashingPassword4jPasswordEncoder
@@ -648,37 +561,16 @@ Balloon hashing is a memory-hard password hashing algorithm designed to be resis
This implementation handles salt management explicitly since Password4j's Balloon hashing implementation does not include the salt in the output hash.
The encoded password format is: `+{salt}:{hash}+` where both salt and hash are Base64 encoded.
Create an encoder with default settings:
.BalloonHashingPassword4jPasswordEncoder
[tabs]
======
Java::
+
[source,java,role="primary"]
----
// Create an encoder with default settings
BalloonHashingPassword4jPasswordEncoder encoder = new BalloonHashingPassword4jPasswordEncoder();
String result = encoder.encode("myPassword");
assertTrue(encoder.matches("myPassword", result));
include-code::./BallooningHashingUsageTests[tag=default-params,indent=0]
// Create an encoder with custom Balloon hashing function and salt length
BalloonHashingFunction customBalloon = BalloonHashingFunction.getInstance(1024, 3, 4, "SHA-256");
BalloonHashingPassword4jPasswordEncoder customEncoder = new BalloonHashingPassword4jPasswordEncoder(customBalloon, 32);
----
Create an encoder with custom parameters:
Kotlin::
+
[source,kotlin,role="secondary"]
----
// Create an encoder with default settings
val encoder = BalloonHashingPassword4jPasswordEncoder()
val result: String = encoder.encode("myPassword")
assertTrue(encoder.matches("myPassword", result))
// Create an encoder with custom Balloon hashing function and salt length
val customBalloon = BalloonHashingFunction.getInstance(1024, 3, 4, "SHA-256")
val customEncoder = BalloonHashingPassword4jPasswordEncoder(customBalloon, 32)
----
======
.BalloonHashingPassword4jPasswordEncoder Custom
include-code::./BallooningHashingUsageTests[tag=custom-params,indent=0]
[[authentication-password-storage-configuration]]
== Password Storage Configuration