Skip to content

Commit

Permalink
SEC-2521: Improve StandardPasswordEncoder performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Winch committed Oct 27, 2015
1 parent a88ac0f commit 69274d9
Showing 1 changed file with 17 additions and 14 deletions.
Expand Up @@ -17,7 +17,6 @@

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
* Helper for working with the MessageDigest API.
Expand All @@ -30,7 +29,7 @@
*/
final class Digester {

private final MessageDigest messageDigest;
private final String algorithm;

private final int iterations;

Expand All @@ -40,22 +39,26 @@ final class Digester {
* @param iterations the number of times to apply the digest algorithm to the input
*/
public Digester(String algorithm, int iterations) {
try {
messageDigest = MessageDigest.getInstance(algorithm);
}
catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("No such hashing algorithm", e);
}

// eagerly validate the algorithm
createDigest(algorithm);
this.algorithm = algorithm;
this.iterations = iterations;
}

public byte[] digest(byte[] value) {
synchronized (messageDigest) {
for (int i = 0; i < iterations; i++) {
value = messageDigest.digest(value);
}
return value;
MessageDigest messageDigest = createDigest(algorithm);
for (int i = 0; i < iterations; i++) {
value = messageDigest.digest(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);
}
}
}

0 comments on commit 69274d9

Please sign in to comment.