|
16 | 16 |
|
17 | 17 | package org.springframework.security.crypto.password4j;
|
18 | 18 |
|
19 |
| -import com.password4j.AlgorithmFinder; |
20 | 19 | import com.password4j.Hash;
|
21 | 20 | import com.password4j.HashingFunction;
|
22 | 21 | import com.password4j.Password;
|
23 |
| -import org.apache.commons.logging.Log; |
24 |
| -import org.apache.commons.logging.LogFactory; |
25 | 22 |
|
26 | 23 | import org.springframework.security.crypto.password.AbstractValidatingPasswordEncoder;
|
27 | 24 | import org.springframework.util.Assert;
|
28 | 25 |
|
29 | 26 | /**
|
30 |
| - * Implementation of {@link org.springframework.security.crypto.password.PasswordEncoder} |
31 |
| - * that uses the Password4j library. This encoder supports multiple password hashing |
32 |
| - * algorithms including BCrypt, SCrypt, Argon2, and PBKDF2. |
| 27 | + * Abstract base class for Password4j-based password encoders. This class provides the |
| 28 | + * common functionality for password encoding and verification using the Password4j |
| 29 | + * library. |
33 | 30 | *
|
34 | 31 | * <p>
|
35 |
| - * The encoder uses the provided {@link HashingFunction} for both encoding and |
36 |
| - * verification. Password4j can automatically detect the algorithm used in existing hashes |
37 |
| - * during verification. |
| 32 | + * This class is package-private and should not be used directly. Instead, use the |
| 33 | + * specific public subclasses that support verified hashing algorithms such as BCrypt, |
| 34 | + * Argon2, and SCrypt implementations. |
38 | 35 | * </p>
|
39 | 36 | *
|
40 | 37 | * <p>
|
41 | 38 | * This implementation is thread-safe and can be shared across multiple threads.
|
42 | 39 | * </p>
|
43 | 40 | *
|
44 |
| - * <p> |
45 |
| - * <strong>Usage Examples:</strong> |
46 |
| - * </p> |
47 |
| - * <pre>{@code |
48 |
| - * // Using default algorithms from AlgorithmFinder (recommended approach) |
49 |
| - * PasswordEncoder bcryptEncoder = new Password4jPasswordEncoder(AlgorithmFinder.getBcryptInstance()); |
50 |
| - * PasswordEncoder argon2Encoder = new Password4jPasswordEncoder(AlgorithmFinder.getArgon2Instance()); |
51 |
| - * PasswordEncoder scryptEncoder = new Password4jPasswordEncoder(AlgorithmFinder.getScryptInstance()); |
52 |
| - * PasswordEncoder pbkdf2Encoder = new Password4jPasswordEncoder(AlgorithmFinder.getPBKDF2Instance()); |
53 |
| - * |
54 |
| - * // Using customized algorithm parameters |
55 |
| - * PasswordEncoder customBcrypt = new Password4jPasswordEncoder(BcryptFunction.getInstance(12)); |
56 |
| - * PasswordEncoder customArgon2 = new Password4jPasswordEncoder( |
57 |
| - * Argon2Function.getInstance(65536, 3, 4, 32, Argon2.ID)); |
58 |
| - * PasswordEncoder customScrypt = new Password4jPasswordEncoder( |
59 |
| - * ScryptFunction.getInstance(32768, 8, 1, 32)); |
60 |
| - * PasswordEncoder customPbkdf2 = new Password4jPasswordEncoder( |
61 |
| - * CompressedPBKDF2Function.getInstance("SHA256", 310000, 32)); |
62 |
| - * }</pre> |
63 |
| - * |
64 | 41 | * @author Mehrdad Bozorgmehr
|
65 | 42 | * @since 7.0
|
66 |
| - * @see AlgorithmFinder |
67 | 43 | */
|
68 |
| -public class Password4jPasswordEncoder extends AbstractValidatingPasswordEncoder { |
69 |
| - |
70 |
| - private final Log logger = LogFactory.getLog(getClass()); |
| 44 | +abstract class Password4jPasswordEncoder extends AbstractValidatingPasswordEncoder { |
71 | 45 |
|
72 | 46 | private final HashingFunction hashingFunction;
|
73 | 47 |
|
74 | 48 | /**
|
75 |
| - * Constructs a Password4j password encoder with the specified hashing function. |
76 |
| - * |
77 |
| - * <p> |
78 |
| - * It is recommended to use password4j's {@link AlgorithmFinder} to obtain default |
79 |
| - * instances with secure configurations: |
80 |
| - * </p> |
81 |
| - * <ul> |
82 |
| - * <li>{@code AlgorithmFinder.getBcryptInstance()} - BCrypt with default settings</li> |
83 |
| - * <li>{@code AlgorithmFinder.getArgon2Instance()} - Argon2 with default settings</li> |
84 |
| - * <li>{@code AlgorithmFinder.getScryptInstance()} - SCrypt with default settings</li> |
85 |
| - * <li>{@code AlgorithmFinder.getPBKDF2Instance()} - PBKDF2 with default settings</li> |
86 |
| - * </ul> |
87 |
| - * |
88 |
| - * <p> |
89 |
| - * For custom configurations, you can create specific function instances: |
90 |
| - * </p> |
91 |
| - * <ul> |
92 |
| - * <li>{@code BcryptFunction.getInstance(12)} - BCrypt with 12 rounds</li> |
93 |
| - * <li>{@code Argon2Function.getInstance(65536, 3, 4, 32, Argon2.ID)} - Custom |
94 |
| - * Argon2</li> |
95 |
| - * <li>{@code ScryptFunction.getInstance(16384, 8, 1, 32)} - Custom SCrypt</li> |
96 |
| - * <li>{@code CompressedPBKDF2Function.getInstance("SHA256", 310000, 32)} - Custom |
97 |
| - * PBKDF2</li> |
98 |
| - * </ul> |
| 49 | + * Constructs a Password4j password encoder with the specified hashing function. This |
| 50 | + * constructor is package-private and intended for use by subclasses only. |
99 | 51 | * @param hashingFunction the hashing function to use for encoding passwords, must not
|
100 | 52 | * be null
|
101 | 53 | * @throws IllegalArgumentException if hashingFunction is null
|
102 | 54 | */
|
103 |
| - public Password4jPasswordEncoder(HashingFunction hashingFunction) { |
| 55 | + Password4jPasswordEncoder(HashingFunction hashingFunction) { |
104 | 56 | Assert.notNull(hashingFunction, "hashingFunction cannot be null");
|
105 | 57 | this.hashingFunction = hashingFunction;
|
106 | 58 | }
|
107 | 59 |
|
108 | 60 | @Override
|
109 | 61 | protected String encodeNonNullPassword(String rawPassword) {
|
110 |
| - try { |
111 |
| - Hash hash = Password.hash(rawPassword).with(this.hashingFunction); |
112 |
| - return hash.getResult(); |
113 |
| - } |
114 |
| - catch (Exception ex) { |
115 |
| - throw new IllegalStateException("Failed to encode password using Password4j", ex); |
116 |
| - } |
| 62 | + Hash hash = Password.hash(rawPassword).with(this.hashingFunction); |
| 63 | + return hash.getResult(); |
117 | 64 | }
|
118 | 65 |
|
119 | 66 | @Override
|
120 | 67 | protected boolean matchesNonNull(String rawPassword, String encodedPassword) {
|
121 |
| - try { |
122 |
| - // Use the specific hashing function for verification |
123 |
| - return Password.check(rawPassword, encodedPassword).with(this.hashingFunction); |
124 |
| - } |
125 |
| - catch (Exception ex) { |
126 |
| - this.logger.warn("Password verification failed for encoded password: " + encodedPassword, ex); |
127 |
| - return false; |
128 |
| - } |
| 68 | + return Password.check(rawPassword, encodedPassword).with(this.hashingFunction); |
129 | 69 | }
|
130 | 70 |
|
131 | 71 | @Override
|
|
0 commit comments