mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-27 14:22:47 +00:00
SEC-2521: Improve StandardPasswordEncoder performance
This commit is contained in:
parent
9c39a0e83e
commit
d648a56e16
@ -17,7 +17,6 @@ package org.springframework.security.crypto.password;
|
|||||||
|
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.NoSuchProviderException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper for working with the MessageDigest API.
|
* Helper for working with the MessageDigest API.
|
||||||
@ -30,7 +29,7 @@ import java.security.NoSuchProviderException;
|
|||||||
*/
|
*/
|
||||||
final class Digester {
|
final class Digester {
|
||||||
|
|
||||||
private final MessageDigest messageDigest;
|
private final String algorithm;
|
||||||
|
|
||||||
private final int iterations;
|
private final int iterations;
|
||||||
|
|
||||||
@ -40,22 +39,26 @@ final class Digester {
|
|||||||
* @param iterations the number of times to apply the digest algorithm to the input
|
* @param iterations the number of times to apply the digest algorithm to the input
|
||||||
*/
|
*/
|
||||||
public Digester(String algorithm, int iterations) {
|
public Digester(String algorithm, int iterations) {
|
||||||
try {
|
// eagerly validate the algorithm
|
||||||
messageDigest = MessageDigest.getInstance(algorithm);
|
createDigest(algorithm);
|
||||||
}
|
this.algorithm = algorithm;
|
||||||
catch (NoSuchAlgorithmException e) {
|
|
||||||
throw new IllegalStateException("No such hashing algorithm", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.iterations = iterations;
|
this.iterations = iterations;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] digest(byte[] value) {
|
public byte[] digest(byte[] value) {
|
||||||
synchronized (messageDigest) {
|
MessageDigest messageDigest = createDigest(algorithm);
|
||||||
for (int i = 0; i < iterations; i++) {
|
for (int i = 0; i < iterations; i++) {
|
||||||
value = messageDigest.digest(value);
|
value = messageDigest.digest(value);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MessageDigest createDigest(String algorithm) {
|
||||||
|
try {
|
||||||
|
return MessageDigest.getInstance(algorithm);
|
||||||
|
}
|
||||||
|
catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new IllegalStateException("No such hashing algorithm", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user