Reformat code using spring-javaformat
Run `./gradlew format` to reformat all java files. Issue gh-8945
This commit is contained in:
@@ -28,22 +28,24 @@ import org.bouncycastle.util.Arrays;
|
||||
* @since 5.3
|
||||
*/
|
||||
class Argon2EncodingUtils {
|
||||
|
||||
private static final Base64.Encoder b64encoder = Base64.getEncoder().withoutPadding();
|
||||
|
||||
private static final Base64.Decoder b64decoder = Base64.getDecoder();
|
||||
|
||||
/**
|
||||
* Encodes a raw Argon2-hash and its parameters into the standard Argon2-hash-string as specified in the reference
|
||||
* implementation (https://github.com/P-H-C/phc-winner-argon2/blob/master/src/encoding.c#L244):
|
||||
* Encodes a raw Argon2-hash and its parameters into the standard Argon2-hash-string
|
||||
* as specified in the reference implementation
|
||||
* (https://github.com/P-H-C/phc-winner-argon2/blob/master/src/encoding.c#L244):
|
||||
*
|
||||
* {@code $argon2<T>[$v=<num>]$m=<num>,t=<num>,p=<num>$<bin>$<bin>}
|
||||
*
|
||||
* where {@code <T>} is either 'd', 'id', or 'i', {@code <num>} is a decimal integer (positive,
|
||||
* fits in an 'unsigned long'), and {@code <bin>} is Base64-encoded data (no '=' padding
|
||||
* characters, no newline or whitespace).
|
||||
*
|
||||
* The last two binary chunks (encoded in Base64) are, in that order,
|
||||
* the salt and the output. If no salt has been used, the salt will be omitted.
|
||||
* where {@code <T>} is either 'd', 'id', or 'i', {@code <num>} is a decimal integer
|
||||
* (positive, fits in an 'unsigned long'), and {@code <bin>} is Base64-encoded data
|
||||
* (no '=' padding characters, no newline or whitespace).
|
||||
*
|
||||
* The last two binary chunks (encoded in Base64) are, in that order, the salt and the
|
||||
* output. If no salt has been used, the salt will be omitted.
|
||||
* @param hash the raw Argon2 hash in binary format
|
||||
* @param parameters the Argon2 parameters that were used to create the hash
|
||||
* @return the encoded Argon2-hash-string as described above
|
||||
@@ -53,44 +55,48 @@ class Argon2EncodingUtils {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
switch (parameters.getType()) {
|
||||
case Argon2Parameters.ARGON2_d: stringBuilder.append("$argon2d"); break;
|
||||
case Argon2Parameters.ARGON2_i: stringBuilder.append("$argon2i"); break;
|
||||
case Argon2Parameters.ARGON2_id: stringBuilder.append("$argon2id"); break;
|
||||
default: throw new IllegalArgumentException("Invalid algorithm type: "+parameters.getType());
|
||||
case Argon2Parameters.ARGON2_d:
|
||||
stringBuilder.append("$argon2d");
|
||||
break;
|
||||
case Argon2Parameters.ARGON2_i:
|
||||
stringBuilder.append("$argon2i");
|
||||
break;
|
||||
case Argon2Parameters.ARGON2_id:
|
||||
stringBuilder.append("$argon2id");
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid algorithm type: " + parameters.getType());
|
||||
}
|
||||
stringBuilder.append("$v=").append(parameters.getVersion())
|
||||
.append("$m=").append(parameters.getMemory())
|
||||
.append(",t=").append(parameters.getIterations())
|
||||
.append(",p=").append(parameters.getLanes());
|
||||
stringBuilder.append("$v=").append(parameters.getVersion()).append("$m=").append(parameters.getMemory())
|
||||
.append(",t=").append(parameters.getIterations()).append(",p=").append(parameters.getLanes());
|
||||
|
||||
if (parameters.getSalt() != null) {
|
||||
stringBuilder.append("$")
|
||||
.append(b64encoder.encodeToString(parameters.getSalt()));
|
||||
stringBuilder.append("$").append(b64encoder.encodeToString(parameters.getSalt()));
|
||||
}
|
||||
|
||||
stringBuilder.append("$")
|
||||
.append(b64encoder.encodeToString(hash));
|
||||
stringBuilder.append("$").append(b64encoder.encodeToString(hash));
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes an Argon2 hash string as specified in the reference implementation
|
||||
* (https://github.com/P-H-C/phc-winner-argon2/blob/master/src/encoding.c#L244) into the raw hash and the used
|
||||
* parameters.
|
||||
* (https://github.com/P-H-C/phc-winner-argon2/blob/master/src/encoding.c#L244) into
|
||||
* the raw hash and the used parameters.
|
||||
*
|
||||
* The hash has to be formatted as follows:
|
||||
* {@code $argon2<T>[$v=<num>]$m=<num>,t=<num>,p=<num>$<bin>$<bin>}
|
||||
*
|
||||
* where {@code <T>} is either 'd', 'id', or 'i', {@code <num>} is a decimal integer (positive,
|
||||
* fits in an 'unsigned long'), and {@code <bin>} is Base64-encoded data (no '=' padding
|
||||
* characters, no newline or whitespace).
|
||||
* where {@code <T>} is either 'd', 'id', or 'i', {@code <num>} is a decimal integer
|
||||
* (positive, fits in an 'unsigned long'), and {@code <bin>} is Base64-encoded data
|
||||
* (no '=' padding characters, no newline or whitespace).
|
||||
*
|
||||
* The last two binary chunks (encoded in Base64) are, in that order,
|
||||
* the salt and the output. Both are required. The binary salt length and the
|
||||
* output length must be in the allowed ranges defined in argon2.h.
|
||||
* The last two binary chunks (encoded in Base64) are, in that order, the salt and the
|
||||
* output. Both are required. The binary salt length and the output length must be in
|
||||
* the allowed ranges defined in argon2.h.
|
||||
* @param encodedHash the Argon2 hash string as described above
|
||||
* @return an {@link Argon2Hash} object containing the raw hash and the {@link Argon2Parameters}.
|
||||
* @return an {@link Argon2Hash} object containing the raw hash and the
|
||||
* {@link Argon2Parameters}.
|
||||
* @throws IllegalArgumentException if the encoded hash is malformed
|
||||
*/
|
||||
public static Argon2Hash decode(String encodedHash) throws IllegalArgumentException {
|
||||
@@ -105,10 +111,17 @@ class Argon2EncodingUtils {
|
||||
int currentPart = 1;
|
||||
|
||||
switch (parts[currentPart++]) {
|
||||
case "argon2d": paramsBuilder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_d); break;
|
||||
case "argon2i": paramsBuilder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_i); break;
|
||||
case "argon2id": paramsBuilder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id); break;
|
||||
default: throw new IllegalArgumentException("Invalid algorithm type: "+parts[0]);
|
||||
case "argon2d":
|
||||
paramsBuilder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_d);
|
||||
break;
|
||||
case "argon2i":
|
||||
paramsBuilder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_i);
|
||||
break;
|
||||
case "argon2id":
|
||||
paramsBuilder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid algorithm type: " + parts[0]);
|
||||
}
|
||||
|
||||
if (parts[currentPart].startsWith("v=")) {
|
||||
@@ -124,19 +137,22 @@ class Argon2EncodingUtils {
|
||||
|
||||
if (performanceParams[0].startsWith("m=")) {
|
||||
paramsBuilder.withMemoryAsKB(Integer.parseInt(performanceParams[0].substring(2)));
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Invalid memory parameter");
|
||||
}
|
||||
|
||||
if (performanceParams[1].startsWith("t=")) {
|
||||
paramsBuilder.withIterations(Integer.parseInt(performanceParams[1].substring(2)));
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Invalid iterations parameter");
|
||||
}
|
||||
|
||||
if (performanceParams[2].startsWith("p=")) {
|
||||
paramsBuilder.withParallelism(Integer.parseInt(performanceParams[2].substring(2)));
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Invalid parallelity parameter");
|
||||
}
|
||||
|
||||
@@ -148,6 +164,7 @@ class Argon2EncodingUtils {
|
||||
public static class Argon2Hash {
|
||||
|
||||
private byte[] hash;
|
||||
|
||||
private Argon2Parameters parameters;
|
||||
|
||||
Argon2Hash(byte[] hash, Argon2Parameters parameters) {
|
||||
@@ -170,5 +187,7 @@ class Argon2EncodingUtils {
|
||||
public void setParameters(Argon2Parameters parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,16 +26,19 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Implementation of PasswordEncoder that uses the Argon2 hashing function.
|
||||
* Clients can optionally supply the length of the salt to use, the length
|
||||
* of the generated hash, a cpu cost parameter, a memory cost parameter
|
||||
* and a parallelization parameter.
|
||||
* Implementation of PasswordEncoder that uses the Argon2 hashing function. Clients can
|
||||
* optionally supply the length of the salt to use, the length of the generated hash, a
|
||||
* cpu cost parameter, a memory cost parameter and a parallelization parameter.
|
||||
* </p>
|
||||
*
|
||||
* <p>Note:</p>
|
||||
* <p>The currently implementation uses Bouncy castle which does not exploit
|
||||
* parallelism/optimizations that password crackers will, so there is an
|
||||
* unnecessary asymmetry between attacker and defender.</p>
|
||||
* <p>
|
||||
* Note:
|
||||
* </p>
|
||||
* <p>
|
||||
* The currently implementation uses Bouncy castle which does not exploit
|
||||
* parallelism/optimizations that password crackers will, so there is an unnecessary
|
||||
* asymmetry between attacker and defender.
|
||||
* </p>
|
||||
*
|
||||
* @author Simeon Macke
|
||||
* @since 5.3
|
||||
@@ -43,16 +46,23 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
public class Argon2PasswordEncoder implements PasswordEncoder {
|
||||
|
||||
private static final int DEFAULT_SALT_LENGTH = 16;
|
||||
|
||||
private static final int DEFAULT_HASH_LENGTH = 32;
|
||||
|
||||
private static final int DEFAULT_PARALLELISM = 1;
|
||||
|
||||
private static final int DEFAULT_MEMORY = 1 << 12;
|
||||
|
||||
private static final int DEFAULT_ITERATIONS = 3;
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final int hashLength;
|
||||
|
||||
private final int parallelism;
|
||||
|
||||
private final int memory;
|
||||
|
||||
private final int iterations;
|
||||
|
||||
private final BytesKeyGenerator saltGenerator;
|
||||
@@ -75,12 +85,8 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
|
||||
byte[] salt = saltGenerator.generateKey();
|
||||
byte[] hash = new byte[hashLength];
|
||||
|
||||
Argon2Parameters params = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id).
|
||||
withSalt(salt).
|
||||
withParallelism(parallelism).
|
||||
withMemoryAsKB(memory).
|
||||
withIterations(iterations).
|
||||
build();
|
||||
Argon2Parameters params = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id).withSalt(salt)
|
||||
.withParallelism(parallelism).withMemoryAsKB(memory).withIterations(iterations).build();
|
||||
Argon2BytesGenerator generator = new Argon2BytesGenerator();
|
||||
generator.init(params);
|
||||
generator.generateBytes(rawPassword.toString().toCharArray(), hash);
|
||||
@@ -99,7 +105,8 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
|
||||
|
||||
try {
|
||||
decoded = Argon2EncodingUtils.decode(encodedPassword);
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
logger.warn("Malformed password hash", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -19,25 +19,23 @@ import java.util.Arrays;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* BCrypt implements OpenBSD-style Blowfish password hashing using
|
||||
* the scheme described in "A Future-Adaptable Password Scheme" by
|
||||
* Niels Provos and David Mazieres.
|
||||
* BCrypt implements OpenBSD-style Blowfish password hashing using the scheme described in
|
||||
* "A Future-Adaptable Password Scheme" by Niels Provos and David Mazieres.
|
||||
* <p>
|
||||
* This password hashing system tries to thwart off-line password
|
||||
* cracking using a computationally-intensive hashing algorithm,
|
||||
* based on Bruce Schneier's Blowfish cipher. The work factor of
|
||||
* the algorithm is parameterised, so it can be increased as
|
||||
* computers get faster.
|
||||
* This password hashing system tries to thwart off-line password cracking using a
|
||||
* computationally-intensive hashing algorithm, based on Bruce Schneier's Blowfish cipher.
|
||||
* The work factor of the algorithm is parameterised, so it can be increased as computers
|
||||
* get faster.
|
||||
* <p>
|
||||
* Usage is really simple. To hash a password for the first time,
|
||||
* call the hashpw method with a random salt, like this:
|
||||
* Usage is really simple. To hash a password for the first time, call the hashpw method
|
||||
* with a random salt, like this:
|
||||
* <p>
|
||||
* <code>
|
||||
* String pw_hash = BCrypt.hashpw(plain_password, BCrypt.gensalt()); <br />
|
||||
* </code>
|
||||
* <p>
|
||||
* To check whether a plaintext password matches one that has been
|
||||
* hashed previously, use the checkpw method:
|
||||
* To check whether a plaintext password matches one that has been hashed previously, use
|
||||
* the checkpw method:
|
||||
* <p>
|
||||
* <code>
|
||||
* if (BCrypt.checkpw(candidate_password, stored_hash))<br />
|
||||
@@ -46,347 +44,185 @@ import java.security.SecureRandom;
|
||||
* System.out.println("It does not match");<br />
|
||||
* </code>
|
||||
* <p>
|
||||
* The gensalt() method takes an optional parameter (log_rounds)
|
||||
* that determines the computational complexity of the hashing:
|
||||
* The gensalt() method takes an optional parameter (log_rounds) that determines the
|
||||
* computational complexity of the hashing:
|
||||
* <p>
|
||||
* <code>
|
||||
* String strong_salt = BCrypt.gensalt(10)<br />
|
||||
* String stronger_salt = BCrypt.gensalt(12)<br />
|
||||
* </code>
|
||||
* <p>
|
||||
* The amount of work increases exponentially (2**log_rounds), so
|
||||
* each increment is twice as much work. The default log_rounds is
|
||||
* 10, and the valid range is 4 to 31.
|
||||
* The amount of work increases exponentially (2**log_rounds), so each increment is twice
|
||||
* as much work. The default log_rounds is 10, and the valid range is 4 to 31.
|
||||
*
|
||||
* @author Damien Miller
|
||||
* @version 0.3
|
||||
*/
|
||||
public class BCrypt {
|
||||
|
||||
// BCrypt parameters
|
||||
private static final int GENSALT_DEFAULT_LOG2_ROUNDS = 10;
|
||||
|
||||
private static final int BCRYPT_SALT_LEN = 16;
|
||||
|
||||
// Blowfish parameters
|
||||
private static final int BLOWFISH_NUM_ROUNDS = 16;
|
||||
|
||||
// Initial contents of key schedule
|
||||
private static final int P_orig[] = {
|
||||
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
|
||||
0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
|
||||
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
|
||||
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
|
||||
0x9216d5d9, 0x8979fb1b
|
||||
};
|
||||
private static final int S_orig[] = {
|
||||
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
|
||||
0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
|
||||
0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
|
||||
0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e,
|
||||
0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee,
|
||||
0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
|
||||
0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef,
|
||||
0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e,
|
||||
0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
|
||||
0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440,
|
||||
0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce,
|
||||
0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
|
||||
0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e,
|
||||
0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677,
|
||||
0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
|
||||
0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032,
|
||||
0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88,
|
||||
0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
|
||||
0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e,
|
||||
0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0,
|
||||
0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
|
||||
0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98,
|
||||
0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88,
|
||||
0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
|
||||
0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6,
|
||||
0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d,
|
||||
0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
|
||||
0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7,
|
||||
0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba,
|
||||
0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
|
||||
0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f,
|
||||
0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09,
|
||||
0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
|
||||
0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb,
|
||||
0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279,
|
||||
0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
|
||||
0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab,
|
||||
0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82,
|
||||
0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
|
||||
0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573,
|
||||
0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0,
|
||||
0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
|
||||
0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790,
|
||||
0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8,
|
||||
0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
|
||||
0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0,
|
||||
0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7,
|
||||
0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
|
||||
0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad,
|
||||
0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1,
|
||||
0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
|
||||
0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9,
|
||||
0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477,
|
||||
0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
|
||||
0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49,
|
||||
0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af,
|
||||
0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
|
||||
0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5,
|
||||
0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41,
|
||||
0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
|
||||
0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400,
|
||||
0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915,
|
||||
0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
|
||||
0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a,
|
||||
0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623,
|
||||
0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266,
|
||||
0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
|
||||
0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e,
|
||||
0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6,
|
||||
0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
|
||||
0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e,
|
||||
0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1,
|
||||
0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
|
||||
0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8,
|
||||
0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff,
|
||||
0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
|
||||
0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701,
|
||||
0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7,
|
||||
0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
|
||||
0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331,
|
||||
0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf,
|
||||
0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
|
||||
0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e,
|
||||
0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87,
|
||||
0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
|
||||
0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2,
|
||||
0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16,
|
||||
0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
|
||||
0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b,
|
||||
0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509,
|
||||
0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
|
||||
0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3,
|
||||
0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f,
|
||||
0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
|
||||
0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4,
|
||||
0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960,
|
||||
0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
|
||||
0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28,
|
||||
0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802,
|
||||
0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
|
||||
0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510,
|
||||
0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf,
|
||||
0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
|
||||
0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e,
|
||||
0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50,
|
||||
0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
|
||||
0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8,
|
||||
0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281,
|
||||
0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
|
||||
0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696,
|
||||
0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128,
|
||||
0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
|
||||
0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0,
|
||||
0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0,
|
||||
0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
|
||||
0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250,
|
||||
0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3,
|
||||
0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
|
||||
0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00,
|
||||
0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061,
|
||||
0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
|
||||
0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e,
|
||||
0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735,
|
||||
0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
|
||||
0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9,
|
||||
0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340,
|
||||
0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
|
||||
0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7,
|
||||
0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934,
|
||||
0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068,
|
||||
0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
|
||||
0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
|
||||
0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45,
|
||||
0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
|
||||
0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a,
|
||||
0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb,
|
||||
0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
|
||||
0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6,
|
||||
0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42,
|
||||
0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
|
||||
0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2,
|
||||
0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb,
|
||||
0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
|
||||
0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b,
|
||||
0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33,
|
||||
0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
|
||||
0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3,
|
||||
0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc,
|
||||
0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
|
||||
0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564,
|
||||
0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b,
|
||||
0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
|
||||
0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922,
|
||||
0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728,
|
||||
0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
|
||||
0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e,
|
||||
0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37,
|
||||
0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
|
||||
0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804,
|
||||
0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b,
|
||||
0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
|
||||
0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb,
|
||||
0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d,
|
||||
0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
|
||||
0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350,
|
||||
0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9,
|
||||
0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
|
||||
0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe,
|
||||
0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d,
|
||||
0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
|
||||
0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f,
|
||||
0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61,
|
||||
0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
|
||||
0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9,
|
||||
0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2,
|
||||
0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
|
||||
0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e,
|
||||
0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633,
|
||||
0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
|
||||
0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169,
|
||||
0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52,
|
||||
0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
|
||||
0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5,
|
||||
0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62,
|
||||
0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
|
||||
0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76,
|
||||
0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24,
|
||||
0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
|
||||
0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4,
|
||||
0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c,
|
||||
0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
|
||||
0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0,
|
||||
0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b,
|
||||
0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe,
|
||||
0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
|
||||
0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4,
|
||||
0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8,
|
||||
0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
|
||||
0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304,
|
||||
0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22,
|
||||
0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
|
||||
0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6,
|
||||
0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9,
|
||||
0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
|
||||
0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593,
|
||||
0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51,
|
||||
0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
|
||||
0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c,
|
||||
0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b,
|
||||
0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
|
||||
0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c,
|
||||
0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd,
|
||||
0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
|
||||
0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319,
|
||||
0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb,
|
||||
0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
|
||||
0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991,
|
||||
0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32,
|
||||
0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
|
||||
0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166,
|
||||
0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae,
|
||||
0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
|
||||
0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5,
|
||||
0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47,
|
||||
0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
|
||||
0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d,
|
||||
0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84,
|
||||
0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
|
||||
0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8,
|
||||
0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd,
|
||||
0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
|
||||
0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7,
|
||||
0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38,
|
||||
0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
|
||||
0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c,
|
||||
0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525,
|
||||
0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
|
||||
0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442,
|
||||
0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964,
|
||||
0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
|
||||
0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8,
|
||||
0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d,
|
||||
0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
|
||||
0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299,
|
||||
0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02,
|
||||
0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
|
||||
0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614,
|
||||
0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a,
|
||||
0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
|
||||
0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b,
|
||||
0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0,
|
||||
0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
|
||||
0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e,
|
||||
0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9,
|
||||
0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
|
||||
0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6
|
||||
};
|
||||
private static final int P_orig[] = { 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
|
||||
0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5,
|
||||
0xb5470917, 0x9216d5d9, 0x8979fb1b };
|
||||
|
||||
private static final int S_orig[] = { 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
|
||||
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69, 0xa458fea3,
|
||||
0xf4933d7e, 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
|
||||
0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1,
|
||||
0xbd314b27, 0x78af2fda, 0x55605c60, 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6,
|
||||
0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, 0x2ba9c55d, 0x741831f6, 0xce5c3e16,
|
||||
0x9b87931e, 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
|
||||
0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, 0x23893e81,
|
||||
0xd396acc5, 0x0f6d6ff3, 0x83f44239, 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a,
|
||||
0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, 0x6eef0b6c, 0x137a3be4, 0xba3bf050,
|
||||
0x7efb2a98, 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
|
||||
0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, 0x37d0d724,
|
||||
0xd00a1248, 0xdb0fead3, 0x49f1c09b, 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b,
|
||||
0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb,
|
||||
0x3b52ec6f, 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
|
||||
0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, 0x679f25fe,
|
||||
0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760,
|
||||
0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, 0xd542a8f6, 0x287effc3, 0xac6732c6,
|
||||
0x8c4f5573, 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
|
||||
0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, 0xef20cada,
|
||||
0x36774c01, 0xd07e9efe, 0x2bf11fb4, 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0,
|
||||
0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, 0x4fad5ea0, 0x688fc31c, 0xd1cff191,
|
||||
0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
|
||||
0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, 0xe6ad2065,
|
||||
0x77b5fa86, 0xc75442f5, 0xfb9d35cf, 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e,
|
||||
0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, 0x78c14389, 0xd95a537f, 0x207d5ba2,
|
||||
0x02e5b9c5, 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
|
||||
0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, 0xb6636521,
|
||||
0xe7b9f9b6, 0xff34052e, 0xc5855664, 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a, 0x4b7a70e9, 0xb5b32944,
|
||||
0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c,
|
||||
0xc2b19ee1, 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6,
|
||||
0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f,
|
||||
0x3ebaefc9, 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, 0x3e07841c, 0x7fdeae5c,
|
||||
0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58,
|
||||
0xdc0921bd, 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7,
|
||||
0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38,
|
||||
0x4f6db908, 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, 0xde9a771f, 0xd9930810,
|
||||
0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc,
|
||||
0xe94b7d8c, 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16,
|
||||
0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1,
|
||||
0xd7a3c76b, 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, 0x86e34570, 0xeae96fb1,
|
||||
0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978,
|
||||
0x9c10b36a, 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960,
|
||||
0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef,
|
||||
0xbe6c5aa5, 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, 0x1521b628, 0x29076170,
|
||||
0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b,
|
||||
0xcbaade14, 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50,
|
||||
0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a,
|
||||
0x97e32d77, 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, 0x57f584a5, 0x1b227263,
|
||||
0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61,
|
||||
0xee7c3c73, 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0,
|
||||
0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2,
|
||||
0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, 0x095bbf00, 0xad19489d,
|
||||
0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1,
|
||||
0x6c223bdb, 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735,
|
||||
0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3,
|
||||
0x105588cd, 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, 0x153e21e7, 0x8fb03d4a,
|
||||
0xe6e39f2b, 0xdb83adf7, 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, 0xbcf46b2e,
|
||||
0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
|
||||
0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, 0x96eb27b3,
|
||||
0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900,
|
||||
0x680ec0a4, 0x27a18dee, 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, 0xce78a399,
|
||||
0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2,
|
||||
0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, 0x55533a3a,
|
||||
0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9,
|
||||
0x5ef47e1c, 0x9029317c, 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, 0x48c1133f,
|
||||
0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564,
|
||||
0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, 0x6b2395e0,
|
||||
0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd,
|
||||
0x647d0862, 0xe7ccf5f0, 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, 0x3a6f6eab,
|
||||
0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804,
|
||||
0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, 0xbb132f88,
|
||||
0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b,
|
||||
0x12754ccc, 0x782ef11c, 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, 0x3d25bdd8,
|
||||
0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe,
|
||||
0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, 0x83426b33,
|
||||
0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2,
|
||||
0xf474ef38, 0x8789bdc2, 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, 0x846a0e79,
|
||||
0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e,
|
||||
0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, 0x1ab93d1d,
|
||||
0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa,
|
||||
0xa002b5c4, 0x0de6d027, 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, 0x006058aa,
|
||||
0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76,
|
||||
0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, 0xed545578,
|
||||
0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4,
|
||||
0x362abfce, 0xddc6c837, 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0, 0x3a39ce37, 0xd3faf5cf, 0xabc27737,
|
||||
0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
|
||||
0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, 0x530ff8ee,
|
||||
0x468dde7d, 0xd5730a1d, 0x4cd04dc6, 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a,
|
||||
0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, 0x83c061ba, 0x9be96a4d, 0x8fe51550,
|
||||
0xba645bd6, 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
|
||||
0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a,
|
||||
0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6,
|
||||
0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, 0x785f0191, 0xed756055, 0xf7960e44,
|
||||
0xe3d35e8c, 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
|
||||
0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, 0x28517711,
|
||||
0xc20ad9f8, 0xabcc5167, 0xccad925f, 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce,
|
||||
0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, 0xa2ae0810, 0xdd6db224, 0x69852dfd,
|
||||
0x09072166, 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
|
||||
0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, 0xd29be463,
|
||||
0x542f5d9e, 0xaec2771b, 0xf64e6370, 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc,
|
||||
0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, 0x6f3f3b82, 0x3520ab82, 0x011a1d4b,
|
||||
0x277227f8, 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
|
||||
0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, 0x0339c32a,
|
||||
0xc6913667, 0x8df9317c, 0xe0b12b4f, 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a,
|
||||
0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 0xb6c1075e, 0xe3056a0c, 0x10d25065,
|
||||
0xcb03a442, 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
|
||||
0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, 0xe54cda54,
|
||||
0x1edad891, 0xce6279cf, 0xcd3e7e6f, 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623,
|
||||
0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, 0xde966292, 0x81b949d0, 0x4c50901b,
|
||||
0x71c65614, 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
|
||||
0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, 0xba38209c,
|
||||
0xf746ce76, 0x77afa1c5, 0x20756060, 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c,
|
||||
0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 0xb74e6132, 0xce77e25b, 0x578fdfe3,
|
||||
0x3ac372e6 };
|
||||
|
||||
// bcrypt IV: "OrpheanBeholderScryDoubt"
|
||||
static private final int bf_crypt_ciphertext[] = {
|
||||
0x4f727068, 0x65616e42, 0x65686f6c,
|
||||
0x64657253, 0x63727944, 0x6f756274
|
||||
};
|
||||
static private final int bf_crypt_ciphertext[] = { 0x4f727068, 0x65616e42, 0x65686f6c, 0x64657253, 0x63727944,
|
||||
0x6f756274 };
|
||||
|
||||
// Table for Base64 encoding
|
||||
static private final char base64_code[] = {
|
||||
'.', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
|
||||
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
|
||||
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
|
||||
'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
|
||||
'6', '7', '8', '9'
|
||||
};
|
||||
static private final char base64_code[] = { '.', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
|
||||
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
||||
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
|
||||
'2', '3', '4', '5', '6', '7', '8', '9' };
|
||||
|
||||
// Table for Base64 decoding
|
||||
static private final byte index_64[] = {
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, 0, 1, 54, 55,
|
||||
56, 57, 58, 59, 60, 61, 62, 63, -1, -1,
|
||||
-1, -1, -1, -1, -1, 2, 3, 4, 5, 6,
|
||||
7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
|
||||
-1, -1, -1, -1, -1, -1, 28, 29, 30,
|
||||
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
|
||||
51, 52, 53, -1, -1, -1, -1, -1
|
||||
};
|
||||
static private final byte index_64[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
0, 1, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, -1, -1, -1, -1, -1, -1, -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
|
||||
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1, 28, 29, 30, 31, 32,
|
||||
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1 };
|
||||
static final int MIN_LOG_ROUNDS = 4;
|
||||
static final int MAX_LOG_ROUNDS = 31;
|
||||
|
||||
// Expanded Blowfish key
|
||||
private int P[];
|
||||
|
||||
private int S[];
|
||||
|
||||
/**
|
||||
* Encode a byte array using bcrypt's slightly-modified base64 encoding scheme. Note
|
||||
* that this is <strong>not</strong> compatible with the standard MIME-base64
|
||||
* encoding.
|
||||
*
|
||||
* @param d the byte array to encode
|
||||
* @param len the number of bytes to encode
|
||||
* @param rs the destination buffer for the base64-encoded string
|
||||
* @exception IllegalArgumentException if the length is invalid
|
||||
*/
|
||||
static void encode_base64(byte d[], int len, StringBuilder rs)
|
||||
throws IllegalArgumentException {
|
||||
static void encode_base64(byte d[], int len, StringBuilder rs) throws IllegalArgumentException {
|
||||
int off = 0;
|
||||
int c1, c2;
|
||||
|
||||
@@ -418,10 +254,10 @@ public class BCrypt {
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up the 3 bits base64-encoded by the specified character,
|
||||
* range-checking againt conversion table
|
||||
* @param x the base64-encoded value
|
||||
* @return the decoded value of x
|
||||
* Look up the 3 bits base64-encoded by the specified character, range-checking againt
|
||||
* conversion table
|
||||
* @param x the base64-encoded value
|
||||
* @return the decoded value of x
|
||||
*/
|
||||
private static byte char64(char x) {
|
||||
if ((int) x < 0 || (int) x >= index_64.length)
|
||||
@@ -430,23 +266,21 @@ public class BCrypt {
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a string encoded using bcrypt's base64 scheme to a
|
||||
* byte array. Note that this is *not* compatible with
|
||||
* the standard MIME-base64 encoding.
|
||||
* @param s the string to decode
|
||||
* @param maxolen the maximum number of bytes to decode
|
||||
* @return an array containing the decoded bytes
|
||||
* Decode a string encoded using bcrypt's base64 scheme to a byte array. Note that
|
||||
* this is *not* compatible with the standard MIME-base64 encoding.
|
||||
* @param s the string to decode
|
||||
* @param maxolen the maximum number of bytes to decode
|
||||
* @return an array containing the decoded bytes
|
||||
* @throws IllegalArgumentException if maxolen is invalid
|
||||
*/
|
||||
static byte[] decode_base64(String s, int maxolen)
|
||||
throws IllegalArgumentException {
|
||||
static byte[] decode_base64(String s, int maxolen) throws IllegalArgumentException {
|
||||
StringBuilder rs = new StringBuilder();
|
||||
int off = 0, slen = s.length(), olen = 0;
|
||||
byte ret[];
|
||||
byte c1, c2, c3, c4, o;
|
||||
|
||||
if (maxolen <= 0)
|
||||
throw new IllegalArgumentException ("Invalid maxolen");
|
||||
throw new IllegalArgumentException("Invalid maxolen");
|
||||
|
||||
while (off < slen - 1 && olen < maxolen) {
|
||||
c1 = char64(s.charAt(off++));
|
||||
@@ -480,10 +314,9 @@ public class BCrypt {
|
||||
}
|
||||
|
||||
/**
|
||||
* Blowfish encipher a single 64-bit block encoded as
|
||||
* two 32-bit halves
|
||||
* @param lr an array containing the two 32-bit half blocks
|
||||
* @param off the position in the array of the blocks
|
||||
* Blowfish encipher a single 64-bit block encoded as two 32-bit halves
|
||||
* @param lr an array containing the two 32-bit half blocks
|
||||
* @param off the position in the array of the blocks
|
||||
*/
|
||||
private void encipher(int lr[], int off) {
|
||||
int i, n, l = lr[off], r = lr[off + 1];
|
||||
@@ -510,12 +343,11 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Cycically extract a word of key material
|
||||
* @param data the string to extract the data from
|
||||
* @param offp a "pointer" (as a one-entry array) to the
|
||||
* current offset into data
|
||||
* @param signp a "pointer" (as a one-entry array) to the
|
||||
* cumulative flag for non-benign sign extension
|
||||
* @return correct and buggy next word of material from data as int[2]
|
||||
* @param data the string to extract the data from
|
||||
* @param offp a "pointer" (as a one-entry array) to the current offset into data
|
||||
* @param signp a "pointer" (as a one-entry array) to the cumulative flag for
|
||||
* non-benign sign extension
|
||||
* @return correct and buggy next word of material from data as int[2]
|
||||
*/
|
||||
private static int[] streamtowords(byte data[], int offp[], int signp[]) {
|
||||
int i;
|
||||
@@ -526,7 +358,8 @@ public class BCrypt {
|
||||
for (i = 0; i < 4; i++) {
|
||||
words[0] = (words[0] << 8) | (data[off] & 0xff);
|
||||
words[1] = (words[1] << 8) | (int) data[off]; // sign extension bug
|
||||
if (i > 0) sign |= words[1] & 0x80;
|
||||
if (i > 0)
|
||||
sign |= words[1] & 0x80;
|
||||
off = (off + 1) % data.length;
|
||||
}
|
||||
|
||||
@@ -537,10 +370,9 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Cycically extract a word of key material
|
||||
* @param data the string to extract the data from
|
||||
* @param offp a "pointer" (as a one-entry array) to the
|
||||
* current offset into data
|
||||
* @return the next word of material from data
|
||||
* @param data the string to extract the data from
|
||||
* @param offp a "pointer" (as a one-entry array) to the current offset into data
|
||||
* @return the next word of material from data
|
||||
*/
|
||||
private static int streamtoword(byte data[], int offp[]) {
|
||||
int signp[] = { 0 };
|
||||
@@ -549,10 +381,9 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Cycically extract a word of key material, with sign-extension bug
|
||||
* @param data the string to extract the data from
|
||||
* @param offp a "pointer" (as a one-entry array) to the
|
||||
* current offset into data
|
||||
* @return the next word of material from data
|
||||
* @param data the string to extract the data from
|
||||
* @param offp a "pointer" (as a one-entry array) to the current offset into data
|
||||
* @return the next word of material from data
|
||||
*/
|
||||
private static int streamtoword_bug(byte data[], int offp[]) {
|
||||
int signp[] = { 0 };
|
||||
@@ -569,9 +400,9 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Key the Blowfish cipher
|
||||
* @param key an array containing the key
|
||||
* @param sign_ext_bug true to implement the 2x bug
|
||||
* @param safety bit 16 is set when the safety measure is requested
|
||||
* @param key an array containing the key
|
||||
* @param sign_ext_bug true to implement the 2x bug
|
||||
* @param safety bit 16 is set when the safety measure is requested
|
||||
*/
|
||||
private void key(byte key[], boolean sign_ext_bug, int safety) {
|
||||
int i;
|
||||
@@ -599,22 +430,20 @@ public class BCrypt {
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the "enhanced key schedule" step described by
|
||||
* Provos and Mazieres in "A Future-Adaptable Password Scheme"
|
||||
* https://www.openbsd.org/papers/bcrypt-paper.ps
|
||||
* @param data salt information
|
||||
* @param key password information
|
||||
* @param sign_ext_bug true to implement the 2x bug
|
||||
* @param safety bit 16 is set when the safety measure is requested
|
||||
* Perform the "enhanced key schedule" step described by Provos and Mazieres in "A
|
||||
* Future-Adaptable Password Scheme" https://www.openbsd.org/papers/bcrypt-paper.ps
|
||||
* @param data salt information
|
||||
* @param key password information
|
||||
* @param sign_ext_bug true to implement the 2x bug
|
||||
* @param safety bit 16 is set when the safety measure is requested
|
||||
*/
|
||||
private void ekskey(byte data[], byte key[],
|
||||
boolean sign_ext_bug, int safety) {
|
||||
private void ekskey(byte data[], byte key[], boolean sign_ext_bug, int safety) {
|
||||
int i;
|
||||
int koffp[] = { 0 }, doffp[] = { 0 };
|
||||
int lr[] = { 0, 0 };
|
||||
int plen = P.length, slen = S.length;
|
||||
int signp[] = { 0 }; // non-benign sign-extension flag
|
||||
int diff = 0; // zero iff correct and buggy are same
|
||||
int diff = 0; // zero iff correct and buggy are same
|
||||
|
||||
for (i = 0; i < plen; i++) {
|
||||
int words[] = streamtowords(key, koffp, signp);
|
||||
@@ -626,12 +455,12 @@ public class BCrypt {
|
||||
|
||||
/*
|
||||
* At this point, "diff" is zero iff the correct and buggy algorithms produced
|
||||
* exactly the same result. If so and if "sign" is non-zero, which indicates
|
||||
* that there was a non-benign sign extension, this means that we have a
|
||||
* collision between the correctly computed hash for this password and a set of
|
||||
* passwords that could be supplied to the buggy algorithm. Our safety measure
|
||||
* is meant to protect from such many-buggy to one-correct collisions, by
|
||||
* deviating from the correct algorithm in such cases. Let's check for this.
|
||||
* exactly the same result. If so and if "sign" is non-zero, which indicates that
|
||||
* there was a non-benign sign extension, this means that we have a collision
|
||||
* between the correctly computed hash for this password and a set of passwords
|
||||
* that could be supplied to the buggy algorithm. Our safety measure is meant to
|
||||
* protect from such many-buggy to one-correct collisions, by deviating from the
|
||||
* correct algorithm in such cases. Let's check for this.
|
||||
*/
|
||||
diff |= diff >> 16; /* still zero iff exact match */
|
||||
diff &= 0xffff; /* ditto */
|
||||
@@ -640,15 +469,15 @@ public class BCrypt {
|
||||
sign &= ~diff & safety; /* action needed? */
|
||||
|
||||
/*
|
||||
* If we have determined that we need to deviate from the correct algorithm,
|
||||
* flip bit 16 in initial expanded key. (The choice of 16 is arbitrary, but
|
||||
* let's stick to it now. It came out of the approach we used above, and it's
|
||||
* not any worse than any other choice we could make.)
|
||||
* If we have determined that we need to deviate from the correct algorithm, flip
|
||||
* bit 16 in initial expanded key. (The choice of 16 is arbitrary, but let's stick
|
||||
* to it now. It came out of the approach we used above, and it's not any worse
|
||||
* than any other choice we could make.)
|
||||
*
|
||||
* It is crucial that we don't do the same to the expanded key used in the main
|
||||
* Eksblowfish loop. By doing it to only one of these two, we deviate from a
|
||||
* state that could be directly specified by a password to the buggy algorithm
|
||||
* (and to the fully correct one as well, but that's a side-effect).
|
||||
* Eksblowfish loop. By doing it to only one of these two, we deviate from a state
|
||||
* that could be directly specified by a password to the buggy algorithm (and to
|
||||
* the fully correct one as well, but that's a side-effect).
|
||||
*/
|
||||
P[0] ^= sign;
|
||||
|
||||
@@ -677,28 +506,25 @@ public class BCrypt {
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the central password hashing step in the
|
||||
* bcrypt scheme
|
||||
* @param password the password to hash
|
||||
* @param salt the binary salt to hash with the password
|
||||
* @param log_rounds the binary logarithm of the number
|
||||
* of rounds of hashing to apply
|
||||
* @param sign_ext_bug true to implement the 2x bug
|
||||
* @param safety bit 16 is set when the safety measure is requested
|
||||
* @return an array containing the binary hashed password
|
||||
* Perform the central password hashing step in the bcrypt scheme
|
||||
* @param password the password to hash
|
||||
* @param salt the binary salt to hash with the password
|
||||
* @param log_rounds the binary logarithm of the number of rounds of hashing to apply
|
||||
* @param sign_ext_bug true to implement the 2x bug
|
||||
* @param safety bit 16 is set when the safety measure is requested
|
||||
* @return an array containing the binary hashed password
|
||||
*/
|
||||
private byte[] crypt_raw(byte password[], byte salt[], int log_rounds,
|
||||
boolean sign_ext_bug, int safety) {
|
||||
private byte[] crypt_raw(byte password[], byte salt[], int log_rounds, boolean sign_ext_bug, int safety) {
|
||||
int rounds, i, j;
|
||||
int cdata[] = bf_crypt_ciphertext.clone();
|
||||
int cdata[] = bf_crypt_ciphertext.clone();
|
||||
int clen = cdata.length;
|
||||
byte ret[];
|
||||
|
||||
if (log_rounds < 4 || log_rounds > 31)
|
||||
throw new IllegalArgumentException ("Bad number of rounds");
|
||||
throw new IllegalArgumentException("Bad number of rounds");
|
||||
rounds = 1 << log_rounds;
|
||||
if (salt.length != BCRYPT_SALT_LEN)
|
||||
throw new IllegalArgumentException ("Bad salt length");
|
||||
throw new IllegalArgumentException("Bad salt length");
|
||||
|
||||
init_key();
|
||||
ekskey(salt, password, sign_ext_bug, safety);
|
||||
@@ -724,10 +550,9 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Hash a password using the OpenBSD bcrypt scheme
|
||||
* @param password the password to hash
|
||||
* @param salt the salt to hash with (perhaps generated
|
||||
* using BCrypt.gensalt)
|
||||
* @return the hashed password
|
||||
* @param password the password to hash
|
||||
* @param salt the salt to hash with (perhaps generated using BCrypt.gensalt)
|
||||
* @return the hashed password
|
||||
*/
|
||||
public static String hashpw(String password, String salt) {
|
||||
byte passwordb[];
|
||||
@@ -739,10 +564,9 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Hash a password using the OpenBSD bcrypt scheme
|
||||
* @param passwordb the password to hash, as a byte array
|
||||
* @param salt the salt to hash with (perhaps generated
|
||||
* using BCrypt.gensalt)
|
||||
* @return the hashed password
|
||||
* @param passwordb the password to hash, as a byte array
|
||||
* @param salt the salt to hash with (perhaps generated using BCrypt.gensalt)
|
||||
* @return the hashed password
|
||||
*/
|
||||
public static String hashpw(byte passwordb[], String salt) {
|
||||
BCrypt B;
|
||||
@@ -763,20 +587,19 @@ public class BCrypt {
|
||||
}
|
||||
|
||||
if (salt.charAt(0) != '$' || salt.charAt(1) != '2')
|
||||
throw new IllegalArgumentException ("Invalid salt version");
|
||||
throw new IllegalArgumentException("Invalid salt version");
|
||||
if (salt.charAt(2) == '$')
|
||||
off = 3;
|
||||
else {
|
||||
minor = salt.charAt(2);
|
||||
if ((minor != 'a' && minor != 'x' && minor != 'y' && minor != 'b')
|
||||
|| salt.charAt(3) != '$')
|
||||
throw new IllegalArgumentException ("Invalid salt revision");
|
||||
if ((minor != 'a' && minor != 'x' && minor != 'y' && minor != 'b') || salt.charAt(3) != '$')
|
||||
throw new IllegalArgumentException("Invalid salt revision");
|
||||
off = 4;
|
||||
}
|
||||
|
||||
// Extract number of rounds
|
||||
if (salt.charAt(off + 2) > '$')
|
||||
throw new IllegalArgumentException ("Missing salt rounds");
|
||||
throw new IllegalArgumentException("Missing salt rounds");
|
||||
|
||||
if (off == 4 && saltLength < 29) {
|
||||
throw new IllegalArgumentException("Invalid salt");
|
||||
@@ -807,26 +630,23 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Generate a salt for use with the BCrypt.hashpw() method
|
||||
* @param prefix the prefix value (default $2a)
|
||||
* @param log_rounds the log2 of the number of rounds of
|
||||
* hashing to apply - the work factor therefore increases as
|
||||
* 2**log_rounds.
|
||||
* @param random an instance of SecureRandom to use
|
||||
* @return an encoded salt value
|
||||
* @param prefix the prefix value (default $2a)
|
||||
* @param log_rounds the log2 of the number of rounds of hashing to apply - the work
|
||||
* factor therefore increases as 2**log_rounds.
|
||||
* @param random an instance of SecureRandom to use
|
||||
* @return an encoded salt value
|
||||
* @exception IllegalArgumentException if prefix or log_rounds is invalid
|
||||
*/
|
||||
public static String gensalt(String prefix, int log_rounds, SecureRandom random)
|
||||
throws IllegalArgumentException {
|
||||
public static String gensalt(String prefix, int log_rounds, SecureRandom random) throws IllegalArgumentException {
|
||||
StringBuilder rs = new StringBuilder();
|
||||
byte rnd[] = new byte[BCRYPT_SALT_LEN];
|
||||
|
||||
if (!prefix.startsWith("$2") ||
|
||||
(prefix.charAt(2) != 'a' && prefix.charAt(2) != 'y' &&
|
||||
prefix.charAt(2) != 'b')) {
|
||||
throw new IllegalArgumentException ("Invalid prefix");
|
||||
if (!prefix.startsWith("$2")
|
||||
|| (prefix.charAt(2) != 'a' && prefix.charAt(2) != 'y' && prefix.charAt(2) != 'b')) {
|
||||
throw new IllegalArgumentException("Invalid prefix");
|
||||
}
|
||||
if (log_rounds < 4 || log_rounds > 31) {
|
||||
throw new IllegalArgumentException ("Invalid log_rounds");
|
||||
throw new IllegalArgumentException("Invalid log_rounds");
|
||||
}
|
||||
|
||||
random.nextBytes(rnd);
|
||||
@@ -844,42 +664,36 @@ public class BCrypt {
|
||||
|
||||
/**
|
||||
* Generate a salt for use with the BCrypt.hashpw() method
|
||||
* @param prefix the prefix value (default $2a)
|
||||
* @param log_rounds the log2 of the number of rounds of
|
||||
* hashing to apply - the work factor therefore increases as
|
||||
* 2**log_rounds.
|
||||
* @return an encoded salt value
|
||||
* @param prefix the prefix value (default $2a)
|
||||
* @param log_rounds the log2 of the number of rounds of hashing to apply - the work
|
||||
* factor therefore increases as 2**log_rounds.
|
||||
* @return an encoded salt value
|
||||
* @exception IllegalArgumentException if prefix or log_rounds is invalid
|
||||
*/
|
||||
public static String gensalt(String prefix, int log_rounds)
|
||||
throws IllegalArgumentException {
|
||||
public static String gensalt(String prefix, int log_rounds) throws IllegalArgumentException {
|
||||
return gensalt(prefix, log_rounds, new SecureRandom());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a salt for use with the BCrypt.hashpw() method
|
||||
* @param log_rounds the log2 of the number of rounds of
|
||||
* hashing to apply - the work factor therefore increases as
|
||||
* 2**log_rounds.
|
||||
* @param random an instance of SecureRandom to use
|
||||
* @return an encoded salt value
|
||||
* @param log_rounds the log2 of the number of rounds of hashing to apply - the work
|
||||
* factor therefore increases as 2**log_rounds.
|
||||
* @param random an instance of SecureRandom to use
|
||||
* @return an encoded salt value
|
||||
* @exception IllegalArgumentException if log_rounds is invalid
|
||||
*/
|
||||
public static String gensalt(int log_rounds, SecureRandom random)
|
||||
throws IllegalArgumentException {
|
||||
public static String gensalt(int log_rounds, SecureRandom random) throws IllegalArgumentException {
|
||||
return gensalt("$2a", log_rounds, random);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a salt for use with the BCrypt.hashpw() method
|
||||
* @param log_rounds the log2 of the number of rounds of
|
||||
* hashing to apply - the work factor therefore increases as
|
||||
* 2**log_rounds.
|
||||
* @return an encoded salt value
|
||||
* @param log_rounds the log2 of the number of rounds of hashing to apply - the work
|
||||
* factor therefore increases as 2**log_rounds.
|
||||
* @return an encoded salt value
|
||||
* @exception IllegalArgumentException if log_rounds is invalid
|
||||
*/
|
||||
public static String gensalt(int log_rounds)
|
||||
throws IllegalArgumentException {
|
||||
public static String gensalt(int log_rounds) throws IllegalArgumentException {
|
||||
return gensalt(log_rounds, new SecureRandom());
|
||||
}
|
||||
|
||||
@@ -888,32 +702,29 @@ public class BCrypt {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a salt for use with the BCrypt.hashpw() method,
|
||||
* selecting a reasonable default for the number of hashing
|
||||
* rounds to apply
|
||||
* @return an encoded salt value
|
||||
* Generate a salt for use with the BCrypt.hashpw() method, selecting a reasonable
|
||||
* default for the number of hashing rounds to apply
|
||||
* @return an encoded salt value
|
||||
*/
|
||||
public static String gensalt() {
|
||||
return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a plaintext password matches a previously hashed
|
||||
* one
|
||||
* @param plaintext the plaintext password to verify
|
||||
* @param hashed the previously-hashed password
|
||||
* @return true if the passwords match, false otherwise
|
||||
* Check that a plaintext password matches a previously hashed one
|
||||
* @param plaintext the plaintext password to verify
|
||||
* @param hashed the previously-hashed password
|
||||
* @return true if the passwords match, false otherwise
|
||||
*/
|
||||
public static boolean checkpw(String plaintext, String hashed) {
|
||||
return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a password (as a byte array) matches a previously hashed
|
||||
* one
|
||||
* @param passwordb the password to verify, as a byte array
|
||||
* @param hashed the previously-hashed password
|
||||
* @return true if the passwords match, false otherwise
|
||||
* Check that a password (as a byte array) matches a previously hashed one
|
||||
* @param passwordb the password to verify, as a byte array
|
||||
* @param hashed the previously-hashed password
|
||||
* @return true if the passwords match, false otherwise
|
||||
* @since 5.3
|
||||
*/
|
||||
public static boolean checkpw(byte[] passwordb, String hashed) {
|
||||
@@ -923,4 +734,5 @@ public class BCrypt {
|
||||
static boolean equalsNoEarlyReturn(String a, String b) {
|
||||
return MessageDigest.isEqual(a.getBytes(StandardCharsets.UTF_8), b.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,23 +25,24 @@ import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Implementation of PasswordEncoder that uses the BCrypt strong hashing function. Clients
|
||||
* can optionally supply a "version" ($2a, $2b, $2y) and a "strength" (a.k.a. log rounds in BCrypt)
|
||||
* and a SecureRandom instance. The larger the strength parameter the more work will have to be done
|
||||
* (exponentially) to hash the passwords. The default value is 10.
|
||||
* can optionally supply a "version" ($2a, $2b, $2y) and a "strength" (a.k.a. log rounds
|
||||
* in BCrypt) and a SecureRandom instance. The larger the strength parameter the more work
|
||||
* will have to be done (exponentially) to hash the passwords. The default value is 10.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class BCryptPasswordEncoder implements PasswordEncoder {
|
||||
private Pattern BCRYPT_PATTERN = Pattern
|
||||
.compile("\\A\\$2(a|y|b)?\\$(\\d\\d)\\$[./0-9A-Za-z]{53}");
|
||||
|
||||
private Pattern BCRYPT_PATTERN = Pattern.compile("\\A\\$2(a|y|b)?\\$(\\d\\d)\\$[./0-9A-Za-z]{53}");
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final int strength;
|
||||
|
||||
private final BCryptVersion version;
|
||||
|
||||
private final SecureRandom random;
|
||||
|
||||
|
||||
public BCryptPasswordEncoder() {
|
||||
this(-1);
|
||||
}
|
||||
@@ -62,7 +63,7 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
/**
|
||||
* @param version the version of bcrypt, can be 2a,2b,2y
|
||||
* @param random the secure random instance to use
|
||||
* @param random the secure random instance to use
|
||||
*/
|
||||
public BCryptPasswordEncoder(BCryptVersion version, SecureRandom random) {
|
||||
this(version, -1, random);
|
||||
@@ -70,14 +71,14 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
/**
|
||||
* @param strength the log rounds to use, between 4 and 31
|
||||
* @param random the secure random instance to use
|
||||
* @param random the secure random instance to use
|
||||
*/
|
||||
public BCryptPasswordEncoder(int strength, SecureRandom random) {
|
||||
this(BCryptVersion.$2A, strength, random);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param version the version of bcrypt, can be 2a,2b,2y
|
||||
* @param version the version of bcrypt, can be 2a,2b,2y
|
||||
* @param strength the log rounds to use, between 4 and 31
|
||||
*/
|
||||
public BCryptPasswordEncoder(BCryptVersion version, int strength) {
|
||||
@@ -85,9 +86,9 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param version the version of bcrypt, can be 2a,2b,2y
|
||||
* @param version the version of bcrypt, can be 2a,2b,2y
|
||||
* @param strength the log rounds to use, between 4 and 31
|
||||
* @param random the secure random instance to use
|
||||
* @param random the secure random instance to use
|
||||
*/
|
||||
public BCryptPasswordEncoder(BCryptVersion version, int strength, SecureRandom random) {
|
||||
if (strength != -1 && (strength < BCrypt.MIN_LOG_ROUNDS || strength > BCrypt.MAX_LOG_ROUNDS)) {
|
||||
@@ -106,7 +107,8 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
||||
String salt;
|
||||
if (random != null) {
|
||||
salt = BCrypt.gensalt(version.getVersion(), strength, random);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
salt = BCrypt.gensalt(version.getVersion(), strength);
|
||||
}
|
||||
return BCrypt.hashpw(rawPassword.toString(), salt);
|
||||
@@ -153,9 +155,8 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
||||
* @author Lin Feng
|
||||
*/
|
||||
public enum BCryptVersion {
|
||||
$2A("$2a"),
|
||||
$2Y("$2y"),
|
||||
$2B("$2b");
|
||||
|
||||
$2A("$2a"), $2Y("$2y"), $2B("$2b");
|
||||
|
||||
private final String version;
|
||||
|
||||
@@ -166,5 +167,7 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
||||
public String getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,8 +17,9 @@ package org.springframework.security.crypto.codec;
|
||||
|
||||
/**
|
||||
* Base64 encoder which is a reduced version of Robert Harder's public domain
|
||||
* implementation (version 2.3.7). See <a
|
||||
* href="http://iharder.sourceforge.net/current/java/base64/">http://iharder.sourceforge.net/current/java/base64/</a> for more information.
|
||||
* implementation (version 2.3.7). See <a href=
|
||||
* "http://iharder.sourceforge.net/current/java/base64/">http://iharder.sourceforge.net/current/java/base64/</a>
|
||||
* for more information.
|
||||
* <p>
|
||||
* For internal use only.
|
||||
*
|
||||
@@ -43,12 +44,11 @@ public final class Base64 {
|
||||
|
||||
/**
|
||||
* Encode using Base64-like encoding that is URL- and Filename-safe as described in
|
||||
* Section 4 of RFC3548: <a
|
||||
* href="https://tools.ietf.org/html/rfc3548">https://tools.ietf.org/html/rfc3548</a>.
|
||||
* It is important to note that data encoded this way is
|
||||
* <em>not</em> officially valid Base64, or at the very least should not be called
|
||||
* Base64 without also specifying that is was encoded using the URL- and Filename-safe
|
||||
* dialect.
|
||||
* Section 4 of RFC3548: <a href=
|
||||
* "https://tools.ietf.org/html/rfc3548">https://tools.ietf.org/html/rfc3548</a>. It
|
||||
* is important to note that data encoded this way is <em>not</em> officially valid
|
||||
* Base64, or at the very least should not be called Base64 without also specifying
|
||||
* that is was encoded using the URL- and Filename-safe dialect.
|
||||
*/
|
||||
public final static int URL_SAFE = 16;
|
||||
|
||||
@@ -67,31 +67,30 @@ public final class Base64 {
|
||||
private final static byte NEW_LINE = (byte) '\n';
|
||||
|
||||
private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding
|
||||
|
||||
private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding
|
||||
|
||||
/* ******** S T A N D A R D B A S E 6 4 A L P H A B E T ******** */
|
||||
|
||||
/** The 64 valid Base64 values. */
|
||||
/* Host platform me be something funny like EBCDIC, so we hardcode these values. */
|
||||
private final static byte[] _STANDARD_ALPHABET = { (byte) 'A', (byte) 'B',
|
||||
(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
|
||||
(byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
|
||||
(byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
|
||||
(byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z',
|
||||
(byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
|
||||
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l',
|
||||
(byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r',
|
||||
(byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
|
||||
(byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
|
||||
(byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',
|
||||
(byte) '+', (byte) '/' };
|
||||
private final static byte[] _STANDARD_ALPHABET = { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
|
||||
(byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
|
||||
(byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W',
|
||||
(byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
|
||||
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o',
|
||||
(byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
|
||||
(byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
|
||||
(byte) '7', (byte) '8', (byte) '9', (byte) '+', (byte) '/' };
|
||||
|
||||
/**
|
||||
* Translates a Base64 value to either its 6-bit reconstruction value or a negative
|
||||
* number indicating some other meaning.
|
||||
**/
|
||||
private final static byte[] _STANDARD_DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9,
|
||||
-9, // Decimal 0 - 8
|
||||
private final static byte[] _STANDARD_DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal
|
||||
// 0
|
||||
// -
|
||||
// 8
|
||||
-5, -5, // Whitespace: Tab and Linefeed
|
||||
-9, -9, // Decimal 11 - 12
|
||||
-5, // Whitespace: Carriage Return
|
||||
@@ -127,30 +126,28 @@ public final class Base64 {
|
||||
/* ******** U R L S A F E B A S E 6 4 A L P H A B E T ******** */
|
||||
|
||||
/**
|
||||
* Used in the URL- and Filename-safe dialect described in Section 4 of RFC3548: <a
|
||||
* href
|
||||
* Used in the URL- and Filename-safe dialect described in Section 4 of RFC3548:
|
||||
* <a href
|
||||
* ="https://tools.ietf.org/html/rfc3548">https://tools.ietf.org/html/rfc3548</a>.
|
||||
* Notice that the last two bytes become "hyphen" and "underscore" instead of "plus"
|
||||
* and "slash."
|
||||
*/
|
||||
private final static byte[] _URL_SAFE_ALPHABET = { (byte) 'A', (byte) 'B',
|
||||
(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
|
||||
(byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
|
||||
(byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
|
||||
(byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z',
|
||||
(byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
|
||||
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l',
|
||||
(byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r',
|
||||
(byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
|
||||
(byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
|
||||
(byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',
|
||||
(byte) '-', (byte) '_' };
|
||||
private final static byte[] _URL_SAFE_ALPHABET = { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
|
||||
(byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
|
||||
(byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W',
|
||||
(byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
|
||||
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o',
|
||||
(byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
|
||||
(byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
|
||||
(byte) '7', (byte) '8', (byte) '9', (byte) '-', (byte) '_' };
|
||||
|
||||
/**
|
||||
* Used in decoding URL- and Filename-safe dialects of Base64.
|
||||
*/
|
||||
private final static byte[] _URL_SAFE_DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9,
|
||||
-9, // Decimal 0 - 8
|
||||
private final static byte[] _URL_SAFE_DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal
|
||||
// 0
|
||||
// -
|
||||
// 8
|
||||
-5, -5, // Whitespace: Tab and Linefeed
|
||||
-9, -9, // Decimal 11 - 12
|
||||
-5, // Whitespace: Carriage Return
|
||||
@@ -189,24 +186,22 @@ public final class Base64 {
|
||||
|
||||
/* ******** O R D E R E D B A S E 6 4 A L P H A B E T ******** */
|
||||
|
||||
private final static byte[] _ORDERED_ALPHABET = { (byte) '-', (byte) '0', (byte) '1',
|
||||
(byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
|
||||
(byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D',
|
||||
(byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
|
||||
(byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',
|
||||
(byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',
|
||||
(byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) '_', (byte) 'a',
|
||||
(byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g',
|
||||
(byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm',
|
||||
(byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
|
||||
(byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y',
|
||||
(byte) 'z' };
|
||||
private final static byte[] _ORDERED_ALPHABET = { (byte) '-', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
|
||||
(byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C',
|
||||
(byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
|
||||
(byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U',
|
||||
(byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) '_', (byte) 'a', (byte) 'b', (byte) 'c',
|
||||
(byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l',
|
||||
(byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',
|
||||
(byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z' };
|
||||
|
||||
/**
|
||||
* Used in decoding the "ordered" dialect of Base64.
|
||||
*/
|
||||
private final static byte[] _ORDERED_DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9,
|
||||
-9, // Decimal 0 - 8
|
||||
private final static byte[] _ORDERED_DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal
|
||||
// 0
|
||||
// -
|
||||
// 8
|
||||
-5, -5, // Whitespace: Tab and Linefeed
|
||||
-9, -9, // Decimal 11 - 12
|
||||
-5, // Whitespace: Carriage Return
|
||||
@@ -312,7 +307,6 @@ public final class Base64 {
|
||||
* <p>
|
||||
* This is the lowest level of the encoding methods with all possible parameters.
|
||||
* </p>
|
||||
*
|
||||
* @param source the array to convert
|
||||
* @param srcOffset the index where conversion begins
|
||||
* @param numSigBytes the number of significant bytes in your array
|
||||
@@ -321,8 +315,8 @@ public final class Base64 {
|
||||
* @return the <var>destination</var> array
|
||||
* @since 1.3
|
||||
*/
|
||||
private static byte[] encode3to4(byte[] source, int srcOffset, int numSigBytes,
|
||||
byte[] destination, int destOffset, int options) {
|
||||
private static byte[] encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset,
|
||||
int options) {
|
||||
|
||||
byte[] ALPHABET = getAlphabet(options);
|
||||
|
||||
@@ -369,7 +363,6 @@ public final class Base64 {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source The data to convert
|
||||
* @param off Offset in array where conversion should begin
|
||||
* @param len Length of data to convert
|
||||
@@ -397,8 +390,7 @@ public final class Base64 {
|
||||
|
||||
if (off + len > source.length) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Cannot have offset of %d and length of %d with array of length %d",
|
||||
off, len, source.length));
|
||||
"Cannot have offset of %d and length of %d with array of length %d", off, len, source.length));
|
||||
} // end if: off < 0
|
||||
|
||||
boolean breakLines = (options & DO_BREAK_LINES) > 0;
|
||||
@@ -464,8 +456,6 @@ public final class Base64 {
|
||||
* <p>
|
||||
* This is the lowest level of the decoding methods with all possible parameters.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @param source the array to convert
|
||||
* @param srcOffset the index where conversion begins
|
||||
* @param destination the array to hold the conversion
|
||||
@@ -477,8 +467,8 @@ public final class Base64 {
|
||||
* not enough room in the array.
|
||||
* @since 1.3
|
||||
*/
|
||||
private static int decode4to3(final byte[] source, final int srcOffset,
|
||||
final byte[] destination, final int destOffset, final int options) {
|
||||
private static int decode4to3(final byte[] source, final int srcOffset, final byte[] destination,
|
||||
final int destOffset, final int options) {
|
||||
|
||||
// Lots of error checking and exception throwing
|
||||
if (source == null) {
|
||||
@@ -489,15 +479,13 @@ public final class Base64 {
|
||||
} // end if
|
||||
if (srcOffset < 0 || srcOffset + 3 >= source.length) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
"Source array with length %d cannot have offset of %d and still process four bytes.",
|
||||
String.format("Source array with length %d cannot have offset of %d and still process four bytes.",
|
||||
source.length, srcOffset));
|
||||
} // end if
|
||||
if (destOffset < 0 || destOffset + 2 >= destination.length) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
"Destination array with length %d cannot have offset of %d and still store three bytes.",
|
||||
destination.length, destOffset));
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Destination array with length %d cannot have offset of %d and still store three bytes.",
|
||||
destination.length, destOffset));
|
||||
} // end if
|
||||
|
||||
byte[] DECODABET = getDecodabet(options);
|
||||
@@ -538,8 +526,7 @@ public final class Base64 {
|
||||
// | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );
|
||||
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
|
||||
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
|
||||
| ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6)
|
||||
| ((DECODABET[source[srcOffset + 3]] & 0xFF));
|
||||
| ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6) | ((DECODABET[source[srcOffset + 3]] & 0xFF));
|
||||
|
||||
destination[destOffset] = (byte) (outBuff >> 16);
|
||||
destination[destOffset + 1] = (byte) (outBuff >> 8);
|
||||
@@ -555,7 +542,6 @@ public final class Base64 {
|
||||
* recommended method, although it is used internally as part of the decoding process.
|
||||
* Special case: if len = 0, an empty array is returned. Still, if you need more speed
|
||||
* and reduced memory footprint (and aren't gzipping), consider this method.
|
||||
*
|
||||
* @param source The Base64 encoded data
|
||||
* @param off The offset of where to begin decoding
|
||||
* @param len The length of characters to decode
|
||||
@@ -563,8 +549,7 @@ public final class Base64 {
|
||||
* @return decoded data
|
||||
* @throws IllegalArgumentException If bogus characters exist in source data
|
||||
*/
|
||||
private static byte[] decode(final byte[] source, final int off, final int len,
|
||||
final int options) {
|
||||
private static byte[] decode(final byte[] source, final int off, final int len, final int options) {
|
||||
|
||||
// Lots of error checking and exception throwing
|
||||
if (source == null) {
|
||||
@@ -572,8 +557,7 @@ public final class Base64 {
|
||||
} // end if
|
||||
if (off < 0 || off + len > source.length) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
"Source array with length %d cannot have offset of %d and process %d bytes.",
|
||||
String.format("Source array with length %d cannot have offset of %d and process %d bytes.",
|
||||
source.length, off, len));
|
||||
} // end if
|
||||
|
||||
@@ -582,8 +566,7 @@ public final class Base64 {
|
||||
}
|
||||
else if (len < 4) {
|
||||
throw new IllegalArgumentException(
|
||||
"Base64-encoded string must have at least four characters, but length specified was "
|
||||
+ len);
|
||||
"Base64-encoded string must have at least four characters, but length specified was " + len);
|
||||
} // end if
|
||||
|
||||
byte[] DECODABET = getDecodabet(options);
|
||||
@@ -621,8 +604,7 @@ public final class Base64 {
|
||||
else {
|
||||
// There's a bad input character in the Base64 stream.
|
||||
throw new InvalidBase64CharacterException(String.format(
|
||||
"Bad Base64 input character decimal %d in array position %d",
|
||||
((int) source[i]) & 0xFF, i));
|
||||
"Bad Base64 input character decimal %d in array position %d", ((int) source[i]) & 0xFF, i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -630,6 +612,7 @@ public final class Base64 {
|
||||
System.arraycopy(outBuff, 0, out, 0, outBuffPosn);
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InvalidBase64CharacterException extends IllegalArgumentException {
|
||||
@@ -637,4 +620,5 @@ class InvalidBase64CharacterException extends IllegalArgumentException {
|
||||
InvalidBase64CharacterException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ package org.springframework.security.crypto.codec;
|
||||
*/
|
||||
public final class Hex {
|
||||
|
||||
private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
|
||||
'f' };
|
||||
|
||||
public static char[] encode(byte[] bytes) {
|
||||
final int nBytes = bytes.length;
|
||||
@@ -48,8 +48,7 @@ public final class Hex {
|
||||
int nChars = s.length();
|
||||
|
||||
if (nChars % 2 != 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Hex-encoded string must have an even number of characters");
|
||||
throw new IllegalArgumentException("Hex-encoded string must have an even number of characters");
|
||||
}
|
||||
|
||||
byte[] result = new byte[nChars / 2];
|
||||
@@ -60,7 +59,7 @@ public final class Hex {
|
||||
|
||||
if (msb < 0 || lsb < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Detected a Non-hex character at " + (i + 1) + " or " + (i + 2) + " position");
|
||||
"Detected a Non-hex character at " + (i + 1) + " or " + (i + 2) + " position");
|
||||
}
|
||||
result[i / 2] = (byte) ((msb << 4) | lsb);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.nio.charset.StandardCharsets;
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public final class Utf8 {
|
||||
|
||||
private static final Charset CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
/**
|
||||
@@ -58,4 +59,5 @@ public final class Utf8 {
|
||||
throw new IllegalArgumentException("Decoding failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,4 +17,3 @@
|
||||
* Internal codec classes. Only intended for use within the framework.
|
||||
*/
|
||||
package org.springframework.security.crypto.codec;
|
||||
|
||||
|
||||
@@ -59,10 +59,10 @@ public final class AesBytesEncryptor implements BytesEncryptor {
|
||||
|
||||
public enum CipherAlgorithm {
|
||||
|
||||
CBC(AES_CBC_ALGORITHM, NULL_IV_GENERATOR), GCM(AES_GCM_ALGORITHM, KeyGenerators
|
||||
.secureRandom(16));
|
||||
CBC(AES_CBC_ALGORITHM, NULL_IV_GENERATOR), GCM(AES_GCM_ALGORITHM, KeyGenerators.secureRandom(16));
|
||||
|
||||
private BytesKeyGenerator ivGenerator;
|
||||
|
||||
private String name;
|
||||
|
||||
CipherAlgorithm(String name, BytesKeyGenerator ivGenerator) {
|
||||
@@ -86,29 +86,28 @@ public final class AesBytesEncryptor implements BytesEncryptor {
|
||||
public BytesKeyGenerator defaultIvGenerator() {
|
||||
return this.ivGenerator;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public AesBytesEncryptor(String password, CharSequence salt) {
|
||||
this(password, salt, null);
|
||||
}
|
||||
|
||||
public AesBytesEncryptor(String password, CharSequence salt,
|
||||
BytesKeyGenerator ivGenerator) {
|
||||
public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) {
|
||||
this(password, salt, ivGenerator, CipherAlgorithm.CBC);
|
||||
}
|
||||
|
||||
public AesBytesEncryptor(String password, CharSequence salt,
|
||||
BytesKeyGenerator ivGenerator, CipherAlgorithm alg) {
|
||||
this(newSecretKey("PBKDF2WithHmacSHA1", new PBEKeySpec(password.toCharArray(), Hex.decode(salt),
|
||||
1024, 256)), ivGenerator, alg);
|
||||
public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) {
|
||||
this(newSecretKey("PBKDF2WithHmacSHA1", new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256)),
|
||||
ivGenerator, alg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an encryptor that uses AES encryption.
|
||||
*
|
||||
* @param secretKey the secret (symmetric) key
|
||||
* @param ivGenerator the generator used to generate the initialization vector. If null,
|
||||
* then a default algorithm will be used based on the provided {@link CipherAlgorithm}
|
||||
* @param ivGenerator the generator used to generate the initialization vector. If
|
||||
* null, then a default algorithm will be used based on the provided
|
||||
* {@link CipherAlgorithm}
|
||||
* @param alg the {@link CipherAlgorithm} to be used
|
||||
*/
|
||||
public AesBytesEncryptor(SecretKey secretKey, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) {
|
||||
@@ -122,31 +121,26 @@ public final class AesBytesEncryptor implements BytesEncryptor {
|
||||
public byte[] encrypt(byte[] bytes) {
|
||||
synchronized (this.encryptor) {
|
||||
byte[] iv = this.ivGenerator.generateKey();
|
||||
initCipher(this.encryptor, Cipher.ENCRYPT_MODE, this.secretKey,
|
||||
this.alg.getParameterSpec(iv));
|
||||
initCipher(this.encryptor, Cipher.ENCRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv));
|
||||
byte[] encrypted = doFinal(this.encryptor, bytes);
|
||||
return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted)
|
||||
: encrypted;
|
||||
return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] encryptedBytes) {
|
||||
synchronized (this.decryptor) {
|
||||
byte[] iv = iv(encryptedBytes);
|
||||
initCipher(this.decryptor, Cipher.DECRYPT_MODE, this.secretKey,
|
||||
this.alg.getParameterSpec(iv));
|
||||
return doFinal(
|
||||
this.decryptor,
|
||||
this.ivGenerator != NULL_IV_GENERATOR ? encrypted(encryptedBytes,
|
||||
iv.length) : encryptedBytes);
|
||||
initCipher(this.decryptor, Cipher.DECRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv));
|
||||
return doFinal(this.decryptor,
|
||||
this.ivGenerator != NULL_IV_GENERATOR ? encrypted(encryptedBytes, iv.length) : encryptedBytes);
|
||||
}
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
private byte[] iv(byte[] encrypted) {
|
||||
return this.ivGenerator != NULL_IV_GENERATOR ? subArray(encrypted, 0,
|
||||
this.ivGenerator.getKeyLength()) : NULL_IV_GENERATOR.generateKey();
|
||||
return this.ivGenerator != NULL_IV_GENERATOR ? subArray(encrypted, 0, this.ivGenerator.getKeyLength())
|
||||
: NULL_IV_GENERATOR.generateKey();
|
||||
}
|
||||
|
||||
private byte[] encrypted(byte[] encryptedBytes, int ivLength) {
|
||||
@@ -166,4 +160,5 @@ public final class AesBytesEncryptor implements BytesEncryptor {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -31,22 +31,22 @@ import org.springframework.security.crypto.keygen.KeyGenerators;
|
||||
abstract class BouncyCastleAesBytesEncryptor implements BytesEncryptor {
|
||||
|
||||
final KeyParameter secretKey;
|
||||
|
||||
final BytesKeyGenerator ivGenerator;
|
||||
|
||||
BouncyCastleAesBytesEncryptor(String password, CharSequence salt) {
|
||||
this(password, salt, KeyGenerators.secureRandom(16));
|
||||
}
|
||||
|
||||
BouncyCastleAesBytesEncryptor(String password, CharSequence salt,
|
||||
BytesKeyGenerator ivGenerator) {
|
||||
BouncyCastleAesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) {
|
||||
if (ivGenerator.getKeyLength() != 16) {
|
||||
throw new IllegalArgumentException("ivGenerator key length != block size 16");
|
||||
}
|
||||
this.ivGenerator = ivGenerator;
|
||||
PBEParametersGenerator keyGenerator = new PKCS5S2ParametersGenerator();
|
||||
byte[] pkcs12PasswordBytes = PBEParametersGenerator
|
||||
.PKCS5PasswordToUTF8Bytes(password.toCharArray());
|
||||
byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toCharArray());
|
||||
keyGenerator.init(pkcs12PasswordBytes, Hex.decode(salt), 1024);
|
||||
this.secretKey = (KeyParameter) keyGenerator.generateDerivedParameters(256);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ import org.springframework.security.crypto.encrypt.AesBytesEncryptor.CipherAlgor
|
||||
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
|
||||
|
||||
/**
|
||||
* An Encryptor equivalent to {@link AesBytesEncryptor} using
|
||||
* {@link CipherAlgorithm#CBC} that uses Bouncy Castle instead of JCE. The
|
||||
* algorithm is equivalent to "AES/CBC/PKCS5Padding".
|
||||
* An Encryptor equivalent to {@link AesBytesEncryptor} using {@link CipherAlgorithm#CBC}
|
||||
* that uses Bouncy Castle instead of JCE. The algorithm is equivalent to
|
||||
* "AES/CBC/PKCS5Padding".
|
||||
*
|
||||
* @author William Tran
|
||||
*
|
||||
@@ -41,8 +41,7 @@ public class BouncyCastleAesCbcBytesEncryptor extends BouncyCastleAesBytesEncryp
|
||||
super(password, salt);
|
||||
}
|
||||
|
||||
public BouncyCastleAesCbcBytesEncryptor(String password, CharSequence salt,
|
||||
BytesKeyGenerator ivGenerator) {
|
||||
public BouncyCastleAesCbcBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) {
|
||||
super(password, salt, ivGenerator);
|
||||
}
|
||||
|
||||
@@ -61,8 +60,7 @@ public class BouncyCastleAesCbcBytesEncryptor extends BouncyCastleAesBytesEncryp
|
||||
@Override
|
||||
public byte[] decrypt(byte[] encryptedBytes) {
|
||||
byte[] iv = subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength());
|
||||
encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(),
|
||||
encryptedBytes.length);
|
||||
encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length);
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(
|
||||
@@ -87,4 +85,5 @@ public class BouncyCastleAesCbcBytesEncryptor extends BouncyCastleAesBytesEncryp
|
||||
System.arraycopy(buf, 0, out, 0, bytesWritten);
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ import org.springframework.security.crypto.encrypt.AesBytesEncryptor.CipherAlgor
|
||||
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
|
||||
|
||||
/**
|
||||
* An Encryptor equivalent to {@link AesBytesEncryptor} using
|
||||
* {@link CipherAlgorithm#GCM} that uses Bouncy Castle instead of JCE. The
|
||||
* algorithm is equivalent to "AES/GCM/NoPadding".
|
||||
* An Encryptor equivalent to {@link AesBytesEncryptor} using {@link CipherAlgorithm#GCM}
|
||||
* that uses Bouncy Castle instead of JCE. The algorithm is equivalent to
|
||||
* "AES/GCM/NoPadding".
|
||||
*
|
||||
* @author William Tran
|
||||
*
|
||||
@@ -39,8 +39,7 @@ public class BouncyCastleAesGcmBytesEncryptor extends BouncyCastleAesBytesEncryp
|
||||
super(password, salt);
|
||||
}
|
||||
|
||||
public BouncyCastleAesGcmBytesEncryptor(String password, CharSequence salt,
|
||||
BytesKeyGenerator ivGenerator) {
|
||||
public BouncyCastleAesGcmBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) {
|
||||
super(password, salt, ivGenerator);
|
||||
}
|
||||
|
||||
@@ -59,8 +58,7 @@ public class BouncyCastleAesGcmBytesEncryptor extends BouncyCastleAesBytesEncryp
|
||||
@Override
|
||||
public byte[] decrypt(byte[] encryptedBytes) {
|
||||
byte[] iv = subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength());
|
||||
encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(),
|
||||
encryptedBytes.length);
|
||||
encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length);
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine());
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.security.crypto.encrypt;
|
||||
|
||||
/**
|
||||
* Service interface for symmetric data encryption.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface BytesEncryptor {
|
||||
|
||||
@@ -33,6 +33,7 @@ import javax.crypto.spec.PBEParameterSpec;
|
||||
|
||||
/**
|
||||
* Static helper for working with the Cipher API.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
class CipherUtils {
|
||||
@@ -78,8 +79,7 @@ class CipherUtils {
|
||||
/**
|
||||
* Initializes the Cipher for use.
|
||||
*/
|
||||
public static <T extends AlgorithmParameterSpec> T getParameterSpec(Cipher cipher,
|
||||
Class<T> parameterSpecClass) {
|
||||
public static <T extends AlgorithmParameterSpec> T getParameterSpec(Cipher cipher, Class<T> parameterSpecClass) {
|
||||
try {
|
||||
return cipher.getParameters().getParameterSpec(parameterSpecClass);
|
||||
}
|
||||
@@ -98,16 +98,14 @@ class CipherUtils {
|
||||
/**
|
||||
* Initializes the Cipher for use.
|
||||
*/
|
||||
public static void initCipher(Cipher cipher, int mode, SecretKey secretKey,
|
||||
byte[] salt, int iterationCount) {
|
||||
public static void initCipher(Cipher cipher, int mode, SecretKey secretKey, byte[] salt, int iterationCount) {
|
||||
initCipher(cipher, mode, secretKey, new PBEParameterSpec(salt, iterationCount));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the Cipher for use.
|
||||
*/
|
||||
public static void initCipher(Cipher cipher, int mode, SecretKey secretKey,
|
||||
AlgorithmParameterSpec parameterSpec) {
|
||||
public static void initCipher(Cipher cipher, int mode, SecretKey secretKey, AlgorithmParameterSpec parameterSpec) {
|
||||
try {
|
||||
if (parameterSpec != null) {
|
||||
cipher.init(mode, secretKey, parameterSpec);
|
||||
@@ -117,12 +115,10 @@ class CipherUtils {
|
||||
}
|
||||
}
|
||||
catch (InvalidKeyException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Unable to initialize due to invalid secret key", e);
|
||||
throw new IllegalArgumentException("Unable to initialize due to invalid secret key", e);
|
||||
}
|
||||
catch (InvalidAlgorithmParameterException e) {
|
||||
throw new IllegalStateException(
|
||||
"Unable to initialize due to invalid decryption parameter spec", e);
|
||||
throw new IllegalStateException("Unable to initialize due to invalid decryption parameter spec", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,12 +131,10 @@ class CipherUtils {
|
||||
return cipher.doFinal(input);
|
||||
}
|
||||
catch (IllegalBlockSizeException e) {
|
||||
throw new IllegalStateException(
|
||||
"Unable to invoke Cipher due to illegal block size", e);
|
||||
throw new IllegalStateException("Unable to invoke Cipher due to illegal block size", e);
|
||||
}
|
||||
catch (BadPaddingException e) {
|
||||
throw new IllegalStateException("Unable to invoke Cipher due to bad padding",
|
||||
e);
|
||||
throw new IllegalStateException("Unable to invoke Cipher due to bad padding", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,15 +34,13 @@ public class Encryptors {
|
||||
* hex-encoded; it should be random and at least 8 bytes in length. Also applies a
|
||||
* random 16-byte initialization vector to ensure each encrypted message will be
|
||||
* unique. Requires Java 6.
|
||||
*
|
||||
* @param password the password used to generate the encryptor's secret key; should
|
||||
* not be shared
|
||||
* @param salt a hex-encoded, random, site-global salt value to use to generate the
|
||||
* key
|
||||
*/
|
||||
public static BytesEncryptor stronger(CharSequence password, CharSequence salt) {
|
||||
return new AesBytesEncryptor(password.toString(), salt,
|
||||
KeyGenerators.secureRandom(16), CipherAlgorithm.GCM);
|
||||
return new AesBytesEncryptor(password.toString(), salt, KeyGenerators.secureRandom(16), CipherAlgorithm.GCM);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,13 +49,11 @@ public class Encryptors {
|
||||
* Function #2). Salts the password to prevent dictionary attacks against the key. The
|
||||
* provided salt is expected to be hex-encoded; it should be random and at least 8
|
||||
* bytes in length. Also applies a random 16-byte initialization vector to ensure each
|
||||
* encrypted message will be unique. Requires Java 6.
|
||||
* NOTE: This mode is not
|
||||
* encrypted message will be unique. Requires Java 6. NOTE: This mode is not
|
||||
* <a href="https://en.wikipedia.org/wiki/Authenticated_encryption">authenticated</a>
|
||||
* and does not provide any guarantees about the authenticity of the data.
|
||||
* For a more secure alternative, users should prefer
|
||||
* and does not provide any guarantees about the authenticity of the data. For a more
|
||||
* secure alternative, users should prefer
|
||||
* {@link #stronger(CharSequence, CharSequence)}.
|
||||
*
|
||||
* @param password the password used to generate the encryptor's secret key; should
|
||||
* not be shared
|
||||
* @param salt a hex-encoded, random, site-global salt value to use to generate the
|
||||
@@ -67,14 +63,12 @@ public class Encryptors {
|
||||
* GCM (instead of CBC)
|
||||
*/
|
||||
public static BytesEncryptor standard(CharSequence password, CharSequence salt) {
|
||||
return new AesBytesEncryptor(password.toString(), salt,
|
||||
KeyGenerators.secureRandom(16));
|
||||
return new AesBytesEncryptor(password.toString(), salt, KeyGenerators.secureRandom(16));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a text encryptor that uses "stronger" password-based encryption. Encrypted
|
||||
* text is hex-encoded.
|
||||
*
|
||||
* @param password the password used to generate the encryptor's secret key; should
|
||||
* not be shared
|
||||
* @see Encryptors#stronger(CharSequence, CharSequence)
|
||||
@@ -86,7 +80,6 @@ public class Encryptors {
|
||||
/**
|
||||
* Creates a text encryptor that uses "standard" password-based encryption. Encrypted
|
||||
* text is hex-encoded.
|
||||
*
|
||||
* @param password the password used to generate the encryptor's secret key; should
|
||||
* not be shared
|
||||
* @see Encryptors#standard(CharSequence, CharSequence)
|
||||
@@ -100,7 +93,6 @@ public class Encryptors {
|
||||
* encryption. Uses a 16-byte all-zero initialization vector so encrypting the same
|
||||
* data results in the same encryption result. This is done to allow encrypted data to
|
||||
* be queried against. Encrypted text is hex-encoded.
|
||||
*
|
||||
* @param password the password used to generate the encryptor's secret key; should
|
||||
* not be shared
|
||||
* @param salt a hex-encoded, random, site-global salt value to use to generate the
|
||||
@@ -110,8 +102,7 @@ public class Encryptors {
|
||||
*/
|
||||
@Deprecated
|
||||
public static TextEncryptor queryableText(CharSequence password, CharSequence salt) {
|
||||
return new HexEncodingTextEncryptor(new AesBytesEncryptor(password.toString(),
|
||||
salt));
|
||||
return new HexEncodingTextEncryptor(new AesBytesEncryptor(password.toString(), salt));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.security.crypto.codec.Utf8;
|
||||
* Delegates to an {@link BytesEncryptor} to encrypt text strings. Raw text strings are
|
||||
* UTF-8 encoded before being passed to the encryptor. Encrypted strings are returned
|
||||
* hex-encoded.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
final class HexEncodingTextEncryptor implements TextEncryptor {
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Used for creating {@link PasswordEncoder} instances
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
@@ -41,18 +42,21 @@ public class PasswordEncoderFactories {
|
||||
*
|
||||
* <ul>
|
||||
* <li>bcrypt - {@link BCryptPasswordEncoder} (Also used for encoding)</li>
|
||||
* <li>ldap - {@link org.springframework.security.crypto.password.LdapShaPasswordEncoder}</li>
|
||||
* <li>MD4 - {@link org.springframework.security.crypto.password.Md4PasswordEncoder}</li>
|
||||
* <li>ldap -
|
||||
* {@link org.springframework.security.crypto.password.LdapShaPasswordEncoder}</li>
|
||||
* <li>MD4 -
|
||||
* {@link org.springframework.security.crypto.password.Md4PasswordEncoder}</li>
|
||||
* <li>MD5 - {@code new MessageDigestPasswordEncoder("MD5")}</li>
|
||||
* <li>noop - {@link org.springframework.security.crypto.password.NoOpPasswordEncoder}</li>
|
||||
* <li>noop -
|
||||
* {@link org.springframework.security.crypto.password.NoOpPasswordEncoder}</li>
|
||||
* <li>pbkdf2 - {@link Pbkdf2PasswordEncoder}</li>
|
||||
* <li>scrypt - {@link SCryptPasswordEncoder}</li>
|
||||
* <li>SHA-1 - {@code new MessageDigestPasswordEncoder("SHA-1")}</li>
|
||||
* <li>SHA-256 - {@code new MessageDigestPasswordEncoder("SHA-256")}</li>
|
||||
* <li>sha256 - {@link org.springframework.security.crypto.password.StandardPasswordEncoder}</li>
|
||||
* <li>sha256 -
|
||||
* {@link org.springframework.security.crypto.password.StandardPasswordEncoder}</li>
|
||||
* <li>argon2 - {@link Argon2PasswordEncoder}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return the {@link PasswordEncoder} to use
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
@@ -67,12 +71,15 @@ public class PasswordEncoderFactories {
|
||||
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
|
||||
encoders.put("scrypt", new SCryptPasswordEncoder());
|
||||
encoders.put("SHA-1", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1"));
|
||||
encoders.put("SHA-256", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256"));
|
||||
encoders.put("SHA-256",
|
||||
new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256"));
|
||||
encoders.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder());
|
||||
encoders.put("argon2", new Argon2PasswordEncoder());
|
||||
|
||||
return new DelegatingPasswordEncoder(encodingId, encoders);
|
||||
}
|
||||
|
||||
private PasswordEncoderFactories() {}
|
||||
private PasswordEncoderFactories() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,8 +26,11 @@ import java.util.Base64;
|
||||
* @since 5.0
|
||||
*/
|
||||
public class Base64StringKeyGenerator implements StringKeyGenerator {
|
||||
|
||||
private static final int DEFAULT_KEY_LENGTH = 32;
|
||||
|
||||
private final BytesKeyGenerator keyGenerator;
|
||||
|
||||
private final Base64.Encoder encoder;
|
||||
|
||||
/**
|
||||
@@ -76,4 +79,5 @@ public class Base64StringKeyGenerator implements StringKeyGenerator {
|
||||
byte[] base64EncodedKey = this.encoder.encode(key);
|
||||
return new String(base64EncodedKey);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.security.crypto.keygen;
|
||||
|
||||
/**
|
||||
* A generator for unique byte array-based keys.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface BytesKeyGenerator {
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.springframework.security.crypto.codec.Hex;
|
||||
/**
|
||||
* A StringKeyGenerator that generates hex-encoded String keys. Delegates to a
|
||||
* {@link BytesKeyGenerator} for the actual key generation.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
final class HexEncodingStringKeyGenerator implements StringKeyGenerator {
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.security.SecureRandom;
|
||||
/**
|
||||
* Factory for commonly used key generators. Public API for constructing a
|
||||
* {@link BytesKeyGenerator} or {@link StringKeyGenerator}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class KeyGenerators {
|
||||
@@ -62,4 +63,5 @@ public class KeyGenerators {
|
||||
|
||||
private KeyGenerators() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.security.crypto.keygen;
|
||||
|
||||
/**
|
||||
* A generator for unique string keys.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface StringKeyGenerator {
|
||||
|
||||
@@ -63,4 +63,5 @@ public abstract class AbstractPasswordEncoder implements PasswordEncoder {
|
||||
protected static boolean matches(byte[] expected, byte[] actual) {
|
||||
return MessageDigest.isEqual(expected, actual);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,8 +50,8 @@ import java.util.Map;
|
||||
* {id}encodedPassword
|
||||
* </pre>
|
||||
*
|
||||
* Such that "id" is an identifier used to look up which {@link PasswordEncoder} should
|
||||
* be used and "encodedPassword" is the original encoded password for the selected
|
||||
* Such that "id" is an identifier used to look up which {@link PasswordEncoder} should be
|
||||
* used and "encodedPassword" is the original encoded password for the selected
|
||||
* {@link PasswordEncoder}. The "id" must be at the beginning of the password, start with
|
||||
* "{" and end with "}". If the "id" cannot be found, the "id" will be null.
|
||||
*
|
||||
@@ -70,8 +70,8 @@ import java.util.Map;
|
||||
*
|
||||
* <ol>
|
||||
* <li>The first password would have a {@code PasswordEncoder} id of "bcrypt" and
|
||||
* encodedPassword of "$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG".
|
||||
* When matching it would delegate to
|
||||
* encodedPassword of "$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG". When
|
||||
* matching it would delegate to
|
||||
* {@link org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder}</li>
|
||||
* <li>The second password would have a {@code PasswordEncoder} id of "noop" and
|
||||
* encodedPassword of "password". When matching it would delegate to
|
||||
@@ -115,17 +115,22 @@ import java.util.Map;
|
||||
* {@link #setDefaultPasswordEncoderForMatches(PasswordEncoder)}.
|
||||
*
|
||||
* @see org.springframework.security.crypto.factory.PasswordEncoderFactories
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @author Michael Simons
|
||||
* @since 5.0
|
||||
*/
|
||||
public class DelegatingPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
private static final String PREFIX = "{";
|
||||
|
||||
private static final String SUFFIX = "}";
|
||||
|
||||
private final String idForEncode;
|
||||
|
||||
private final PasswordEncoder passwordEncoderForEncode;
|
||||
|
||||
private final Map<String, PasswordEncoder> idToPasswordEncoder;
|
||||
|
||||
private PasswordEncoder defaultPasswordEncoderForMatches = new UnmappedIdPasswordEncoder();
|
||||
|
||||
/**
|
||||
@@ -133,15 +138,16 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
|
||||
* @param idForEncode the id used to lookup which {@link PasswordEncoder} should be
|
||||
* used for {@link #encode(CharSequence)}
|
||||
* @param idToPasswordEncoder a Map of id to {@link PasswordEncoder} used to determine
|
||||
* which {@link PasswordEncoder} should be used for {@link #matches(CharSequence, String)}
|
||||
* which {@link PasswordEncoder} should be used for
|
||||
* {@link #matches(CharSequence, String)}
|
||||
*/
|
||||
public DelegatingPasswordEncoder(String idForEncode,
|
||||
Map<String, PasswordEncoder> idToPasswordEncoder) {
|
||||
public DelegatingPasswordEncoder(String idForEncode, Map<String, PasswordEncoder> idToPasswordEncoder) {
|
||||
if (idForEncode == null) {
|
||||
throw new IllegalArgumentException("idForEncode cannot be null");
|
||||
}
|
||||
if (!idToPasswordEncoder.containsKey(idForEncode)) {
|
||||
throw new IllegalArgumentException("idForEncode " + idForEncode + "is not found in idToPasswordEncoder " + idToPasswordEncoder);
|
||||
throw new IllegalArgumentException(
|
||||
"idForEncode " + idForEncode + "is not found in idToPasswordEncoder " + idToPasswordEncoder);
|
||||
}
|
||||
for (String id : idToPasswordEncoder.keySet()) {
|
||||
if (id == null) {
|
||||
@@ -165,16 +171,15 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
|
||||
* {@link PasswordEncoder}.
|
||||
*
|
||||
* <p>
|
||||
The encodedPassword provided will be the full password
|
||||
* passed in including the {"id"} portion.* For example, if the password of
|
||||
* "{notmapped}foobar" was used, the "id" would be "notmapped" and the encodedPassword
|
||||
* passed into the {@link PasswordEncoder} would be "{notmapped}foobar".
|
||||
* The encodedPassword provided will be the full password passed in including the
|
||||
* {"id"} portion.* For example, if the password of "{notmapped}foobar" was used, the
|
||||
* "id" would be "notmapped" and the encodedPassword passed into the
|
||||
* {@link PasswordEncoder} would be "{notmapped}foobar".
|
||||
* </p>
|
||||
* @param defaultPasswordEncoderForMatches the encoder to use. The default is to
|
||||
* throw an {@link IllegalArgumentException}
|
||||
* @param defaultPasswordEncoderForMatches the encoder to use. The default is to throw
|
||||
* an {@link IllegalArgumentException}
|
||||
*/
|
||||
public void setDefaultPasswordEncoderForMatches(
|
||||
PasswordEncoder defaultPasswordEncoderForMatches) {
|
||||
public void setDefaultPasswordEncoderForMatches(PasswordEncoder defaultPasswordEncoderForMatches) {
|
||||
if (defaultPasswordEncoderForMatches == null) {
|
||||
throw new IllegalArgumentException("defaultPasswordEncoderForMatches cannot be null");
|
||||
}
|
||||
@@ -194,8 +199,7 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
|
||||
String id = extractId(prefixEncodedPassword);
|
||||
PasswordEncoder delegate = this.idToPasswordEncoder.get(id);
|
||||
if (delegate == null) {
|
||||
return this.defaultPasswordEncoderForMatches
|
||||
.matches(rawPassword, prefixEncodedPassword);
|
||||
return this.defaultPasswordEncoderForMatches.matches(rawPassword, prefixEncodedPassword);
|
||||
}
|
||||
String encodedPassword = extractEncodedPassword(prefixEncodedPassword);
|
||||
return delegate.matches(rawPassword, encodedPassword);
|
||||
@@ -244,10 +248,11 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(CharSequence rawPassword,
|
||||
String prefixEncodedPassword) {
|
||||
public boolean matches(CharSequence rawPassword, String prefixEncodedPassword) {
|
||||
String id = extractId(prefixEncodedPassword);
|
||||
throw new IllegalArgumentException("There is no PasswordEncoder mapped for the id \"" + id + "\"");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -68,4 +68,5 @@ final class Digester {
|
||||
throw new IllegalStateException("No such hashing algorithm", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,19 +39,24 @@ import java.util.Base64;
|
||||
* @deprecated Digest based password encoding is not considered secure. Instead use an
|
||||
* adaptive one way function like BCryptPasswordEncoder, Pbkdf2PasswordEncoder, or
|
||||
* SCryptPasswordEncoder. Even better use {@link DelegatingPasswordEncoder} which supports
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to indicate
|
||||
* that this is a legacy implementation and using it is considered insecure.
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to
|
||||
* indicate that this is a legacy implementation and using it is considered insecure.
|
||||
*/
|
||||
@Deprecated
|
||||
public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
// ~ Static fields/initializers
|
||||
// =====================================================================================
|
||||
|
||||
/** The number of bytes in a SHA hash */
|
||||
private static final int SHA_LENGTH = 20;
|
||||
|
||||
private static final String SSHA_PREFIX = "{SSHA}";
|
||||
|
||||
private static final String SSHA_PREFIX_LC = SSHA_PREFIX.toLowerCase();
|
||||
|
||||
private static final String SHA_PREFIX = "{SHA}";
|
||||
|
||||
private static final String SHA_PREFIX_LC = SHA_PREFIX.toLowerCase();
|
||||
|
||||
// ~ Instance fields
|
||||
@@ -93,9 +98,7 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
* Calculates the hash of password (and salt bytes, if supplied) and returns a base64
|
||||
* encoded concatenation of the hash and salt, prefixed with {SHA} (or {SSHA} if salt
|
||||
* was used).
|
||||
*
|
||||
* @param rawPass the password to be encoded.
|
||||
*
|
||||
* @return the encoded password in the specified format
|
||||
*
|
||||
*/
|
||||
@@ -104,7 +107,6 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
return encode(rawPass, salt);
|
||||
}
|
||||
|
||||
|
||||
private String encode(CharSequence rawPassword, byte[] salt) {
|
||||
MessageDigest sha;
|
||||
|
||||
@@ -148,10 +150,8 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
/**
|
||||
* Checks the validity of an unencoded password against an encoded one in the form
|
||||
* "{SSHA}sQuQF8vj8Eg2Y1hPdh3bkQhCKQBgjhQI".
|
||||
*
|
||||
* @param rawPassword unencoded password to be verified.
|
||||
* @param encodedPassword the actual SSHA or SHA encoded password
|
||||
*
|
||||
* @return true if they match (independent of the case of the prefix).
|
||||
*/
|
||||
public boolean matches(CharSequence rawPassword, String encodedPassword) {
|
||||
@@ -170,8 +170,7 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
salt = extractSalt(encodedPassword);
|
||||
}
|
||||
else if (!prefix.equals(SHA_PREFIX) && !prefix.equals(SHA_PREFIX_LC)) {
|
||||
throw new IllegalArgumentException("Unsupported password prefix '" + prefix
|
||||
+ "'");
|
||||
throw new IllegalArgumentException("Unsupported password prefix '" + prefix + "'");
|
||||
}
|
||||
else {
|
||||
// Standard SHA
|
||||
@@ -182,8 +181,7 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
String encodedRawPass = encode(rawPassword, salt).substring(startOfHash);
|
||||
|
||||
return PasswordEncoderUtils
|
||||
.equals(encodedRawPass, encodedPassword.substring(startOfHash));
|
||||
return PasswordEncoderUtils.equals(encodedRawPass, encodedPassword.substring(startOfHash));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,8 +195,7 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
int secondBrace = encPass.lastIndexOf('}');
|
||||
|
||||
if (secondBrace < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Couldn't find closing brace for SHA prefix");
|
||||
throw new IllegalArgumentException("Couldn't find closing brace for SHA prefix");
|
||||
}
|
||||
|
||||
return encPass.substring(0, secondBrace + 1);
|
||||
@@ -207,4 +204,5 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
|
||||
public void setForceLowerCasePrefix(boolean forceLowerCasePrefix) {
|
||||
this.forceLowerCasePrefix = forceLowerCasePrefix;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,12 +22,19 @@ package org.springframework.security.crypto.password;
|
||||
* @author Alan Stewart
|
||||
*/
|
||||
class Md4 {
|
||||
|
||||
private static final int BLOCK_SIZE = 64;
|
||||
|
||||
private static final int HASH_SIZE = 16;
|
||||
|
||||
private final byte[] buffer = new byte[BLOCK_SIZE];
|
||||
|
||||
private int bufferOffset;
|
||||
|
||||
private long byteCount;
|
||||
|
||||
private final int[] state = new int[4];
|
||||
|
||||
private final int[] tmp = new int[16];
|
||||
|
||||
Md4() {
|
||||
@@ -99,8 +106,8 @@ class Md4 {
|
||||
|
||||
private void update(byte[] block, int offset) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
tmp[i] = (block[offset++] & 0xFF) | (block[offset++] & 0xFF) << 8
|
||||
| (block[offset++] & 0xFF) << 16 | (block[offset++] & 0xFF) << 24;
|
||||
tmp[i] = (block[offset++] & 0xFF) | (block[offset++] & 0xFF) << 8 | (block[offset++] & 0xFF) << 16
|
||||
| (block[offset++] & 0xFF) << 24;
|
||||
}
|
||||
|
||||
int A = state[0];
|
||||
@@ -179,4 +186,5 @@ class Md4 {
|
||||
int t = a + (b ^ c ^ d) + x + 0x6ED9EBA1;
|
||||
return t << s | t >>> (32 - s);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@ import org.springframework.security.crypto.keygen.StringKeyGenerator;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* This {@link PasswordEncoder} is provided for legacy purposes only and is not considered secure.
|
||||
* This {@link PasswordEncoder} is provided for legacy purposes only and is not considered
|
||||
* secure.
|
||||
*
|
||||
* Encodes passwords using MD4. The general format of the password is:
|
||||
*
|
||||
@@ -33,8 +34,7 @@ import java.util.Base64;
|
||||
* </pre>
|
||||
*
|
||||
* Such that "salt" is the salt, md4 is the digest method, and password is the actual
|
||||
* password. For example with a password of "password", and a salt of
|
||||
* "thisissalt":
|
||||
* password. For example with a password of "password", and a salt of "thisissalt":
|
||||
*
|
||||
* <pre>
|
||||
* String s = salt == null ? "" : "{" + salt + "}";
|
||||
@@ -55,9 +55,9 @@ import java.util.Base64;
|
||||
* "{}" + md4(password + "{}")
|
||||
* </pre>
|
||||
*
|
||||
* The format is intended to work with the Md4PasswordEncoder that was found in the
|
||||
* Spring Security core module. However, the passwords will need to be migrated to include
|
||||
* any salt with the password since this API provides Salt internally vs making it the
|
||||
* The format is intended to work with the Md4PasswordEncoder that was found in the Spring
|
||||
* Security core module. However, the passwords will need to be migrated to include any
|
||||
* salt with the password since this API provides Salt internally vs making it the
|
||||
* responsibility of the user. To migrate passwords from the SaltSource use the following:
|
||||
*
|
||||
* <pre>
|
||||
@@ -73,16 +73,19 @@ import java.util.Base64;
|
||||
* @deprecated Digest based password encoding is not considered secure. Instead use an
|
||||
* adaptive one way function like BCryptPasswordEncoder, Pbkdf2PasswordEncoder, or
|
||||
* SCryptPasswordEncoder. Even better use {@link DelegatingPasswordEncoder} which supports
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to indicate
|
||||
* that this is a legacy implementation and using it is considered insecure.
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to
|
||||
* indicate that this is a legacy implementation and using it is considered insecure.
|
||||
*/
|
||||
@Deprecated
|
||||
public class Md4PasswordEncoder implements PasswordEncoder {
|
||||
private static final String PREFIX = "{";
|
||||
private static final String SUFFIX = "}";
|
||||
private StringKeyGenerator saltGenerator = new Base64StringKeyGenerator();
|
||||
private boolean encodeHashAsBase64;
|
||||
|
||||
private static final String PREFIX = "{";
|
||||
|
||||
private static final String SUFFIX = "}";
|
||||
|
||||
private StringKeyGenerator saltGenerator = new Base64StringKeyGenerator();
|
||||
|
||||
private boolean encodeHashAsBase64;
|
||||
|
||||
public void setEncodeHashAsBase64(boolean encodeHashAsBase64) {
|
||||
this.encodeHashAsBase64 = encodeHashAsBase64;
|
||||
@@ -91,7 +94,6 @@ public class Md4PasswordEncoder implements PasswordEncoder {
|
||||
/**
|
||||
* Encodes the rawPass using a MessageDigest. If a salt is specified it will be merged
|
||||
* with the password before encoding.
|
||||
*
|
||||
* @param rawPassword The plain text password
|
||||
* @return Hex string of password digest (or base64 encoded string if
|
||||
* encodeHashAsBase64 is enabled.
|
||||
@@ -128,7 +130,6 @@ public class Md4PasswordEncoder implements PasswordEncoder {
|
||||
/**
|
||||
* Takes a previously encoded password and compares it with a rawpassword after mixing
|
||||
* in the salt and encoding that value
|
||||
*
|
||||
* @param rawPassword plain text password
|
||||
* @param encodedPassword previously encoded password
|
||||
* @return true or false
|
||||
@@ -150,4 +151,5 @@ public class Md4PasswordEncoder implements PasswordEncoder {
|
||||
}
|
||||
return prefixEncodedPassword.substring(start, end + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,8 @@ import java.security.MessageDigest;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* This {@link PasswordEncoder} is provided for legacy purposes only and is not considered secure.
|
||||
* This {@link PasswordEncoder} is provided for legacy purposes only and is not considered
|
||||
* secure.
|
||||
*
|
||||
* Encodes passwords using the passed in {@link MessageDigest}.
|
||||
*
|
||||
@@ -76,14 +77,18 @@ import java.util.Base64;
|
||||
* @deprecated Digest based password encoding is not considered secure. Instead use an
|
||||
* adaptive one way function like BCryptPasswordEncoder, Pbkdf2PasswordEncoder, or
|
||||
* SCryptPasswordEncoder. Even better use {@link DelegatingPasswordEncoder} which supports
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to indicate
|
||||
* that this is a legacy implementation and using it is considered insecure.
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to
|
||||
* indicate that this is a legacy implementation and using it is considered insecure.
|
||||
*/
|
||||
@Deprecated
|
||||
public class MessageDigestPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
private static final String PREFIX = "{";
|
||||
|
||||
private static final String SUFFIX = "}";
|
||||
|
||||
private StringKeyGenerator saltGenerator = new Base64StringKeyGenerator();
|
||||
|
||||
private boolean encodeHashAsBase64;
|
||||
|
||||
private Digester digester;
|
||||
@@ -92,7 +97,6 @@ public class MessageDigestPasswordEncoder implements PasswordEncoder {
|
||||
* The digest algorithm to use Supports the named
|
||||
* <a href="https://java.sun.com/j2se/1.4.2/docs/guide/security/CryptoSpec.html#AppA">
|
||||
* Message Digest Algorithms</a> in the Java environment.
|
||||
*
|
||||
* @param algorithm
|
||||
*/
|
||||
public MessageDigestPasswordEncoder(String algorithm) {
|
||||
@@ -106,7 +110,6 @@ public class MessageDigestPasswordEncoder implements PasswordEncoder {
|
||||
/**
|
||||
* Encodes the rawPass using a MessageDigest. If a salt is specified it will be merged
|
||||
* with the password before encoding.
|
||||
*
|
||||
* @param rawPassword The plain text password
|
||||
* @return Hex string of password digest (or base64 encoded string if
|
||||
* encodeHashAsBase64 is enabled.
|
||||
@@ -136,7 +139,6 @@ public class MessageDigestPasswordEncoder implements PasswordEncoder {
|
||||
/**
|
||||
* Takes a previously encoded password and compares it with a rawpassword after mixing
|
||||
* in the salt and encoding that value
|
||||
*
|
||||
* @param rawPassword plain text password
|
||||
* @param encodedPassword previously encoded password
|
||||
* @return true or false
|
||||
@@ -152,7 +154,6 @@ public class MessageDigestPasswordEncoder implements PasswordEncoder {
|
||||
* "stretched". If this is greater than one, the initial digest is calculated, the
|
||||
* digest function will be called repeatedly on the result for the additional number
|
||||
* of iterations.
|
||||
*
|
||||
* @param iterations the number of iterations which will be executed on the hashed
|
||||
* password/salt value. Defaults to 1.
|
||||
*/
|
||||
@@ -171,4 +172,5 @@ public class MessageDigestPasswordEncoder implements PasswordEncoder {
|
||||
}
|
||||
return prefixEncodedPassword.substring(start, end + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ package org.springframework.security.crypto.password;
|
||||
* passwords may be preferred.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @deprecated This PasswordEncoder is not secure. Instead use an
|
||||
* adaptive one way function like BCryptPasswordEncoder, Pbkdf2PasswordEncoder, or
|
||||
* SCryptPasswordEncoder. Even better use {@link DelegatingPasswordEncoder} which supports
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to indicate that
|
||||
* this is a legacy implementation and using it is considered insecure.
|
||||
* @deprecated This PasswordEncoder is not secure. Instead use an adaptive one way
|
||||
* function like BCryptPasswordEncoder, Pbkdf2PasswordEncoder, or SCryptPasswordEncoder.
|
||||
* Even better use {@link DelegatingPasswordEncoder} which supports password upgrades.
|
||||
* There are no plans to remove this support. It is deprecated to indicate that this is a
|
||||
* legacy implementation and using it is considered insecure.
|
||||
*/
|
||||
@Deprecated
|
||||
public final class NoOpPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
@@ -34,7 +34,6 @@ public interface PasswordEncoder {
|
||||
* Verify the encoded password obtained from storage matches the submitted raw
|
||||
* password after it too is encoded. Returns true if the passwords match, false if
|
||||
* they do not. The stored password itself is never decoded.
|
||||
*
|
||||
* @param rawPassword the raw password to encode and match
|
||||
* @param encodedPassword the encoded password from storage to compare with
|
||||
* @return true if the raw password, after encoding, matches the encoded password from
|
||||
@@ -52,4 +51,5 @@ public interface PasswordEncoder {
|
||||
default boolean upgradeEncoding(String encodedPassword) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,9 +44,11 @@ class PasswordEncoderUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Utf8.encode(s); // need to check if Utf8.encode() runs in constant time (probably not). This may leak length of string.
|
||||
return Utf8.encode(s); // need to check if Utf8.encode() runs in constant time
|
||||
// (probably not). This may leak length of string.
|
||||
}
|
||||
|
||||
private PasswordEncoderUtils() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,21 +46,27 @@ import static org.springframework.security.crypto.util.EncodingUtils.subArray;
|
||||
public class Pbkdf2PasswordEncoder implements PasswordEncoder {
|
||||
|
||||
private static final int DEFAULT_HASH_WIDTH = 256;
|
||||
|
||||
private static final int DEFAULT_ITERATIONS = 185000;
|
||||
|
||||
private final BytesKeyGenerator saltGenerator = KeyGenerators.secureRandom();
|
||||
|
||||
private final byte[] secret;
|
||||
|
||||
private final int hashWidth;
|
||||
|
||||
private final int iterations;
|
||||
|
||||
private String algorithm = SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1.name();
|
||||
|
||||
private boolean encodeHashAsBase64;
|
||||
|
||||
/**
|
||||
* Constructs a PBKDF2 password encoder with no additional secret value. There will be
|
||||
* {@value DEFAULT_ITERATIONS} iterations and a hash width of {@value DEFAULT_HASH_WIDTH}. The default is based upon aiming for .5
|
||||
* seconds to validate the password when this class was added.. Users should tune
|
||||
* password verification to their own systems.
|
||||
* {@value DEFAULT_ITERATIONS} iterations and a hash width of
|
||||
* {@value DEFAULT_HASH_WIDTH}. The default is based upon aiming for .5 seconds to
|
||||
* validate the password when this class was added.. Users should tune password
|
||||
* verification to their own systems.
|
||||
*/
|
||||
public Pbkdf2PasswordEncoder() {
|
||||
this("");
|
||||
@@ -68,8 +74,8 @@ public class Pbkdf2PasswordEncoder implements PasswordEncoder {
|
||||
|
||||
/**
|
||||
* Constructs a standard password encoder with a secret value which is also included
|
||||
* in the password hash. There will be {@value DEFAULT_ITERATIONS} iterations and a hash width of {@value DEFAULT_HASH_WIDTH}.
|
||||
*
|
||||
* in the password hash. There will be {@value DEFAULT_ITERATIONS} iterations and a
|
||||
* hash width of {@value DEFAULT_HASH_WIDTH}.
|
||||
* @param secret the secret key used in the encoding process (should not be shared)
|
||||
*/
|
||||
public Pbkdf2PasswordEncoder(CharSequence secret) {
|
||||
@@ -79,7 +85,6 @@ public class Pbkdf2PasswordEncoder implements PasswordEncoder {
|
||||
/**
|
||||
* Constructs a standard password encoder with a secret value as well as iterations
|
||||
* and hash.
|
||||
*
|
||||
* @param secret the secret
|
||||
* @param iterations the number of iterations. Users should aim for taking about .5
|
||||
* seconds on their own system.
|
||||
@@ -92,8 +97,9 @@ public class Pbkdf2PasswordEncoder implements PasswordEncoder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the algorithm to use. See
|
||||
* <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SecretKeyFactory">SecretKeyFactory Algorithms</a>
|
||||
* Sets the algorithm to use. See <a href=
|
||||
* "https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SecretKeyFactory">SecretKeyFactory
|
||||
* Algorithms</a>
|
||||
* @param secretKeyFactoryAlgorithm the algorithm to use (i.e.
|
||||
* {@code SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1},
|
||||
* {@code SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA256},
|
||||
@@ -154,8 +160,8 @@ public class Pbkdf2PasswordEncoder implements PasswordEncoder {
|
||||
|
||||
private byte[] encode(CharSequence rawPassword, byte[] salt) {
|
||||
try {
|
||||
PBEKeySpec spec = new PBEKeySpec(rawPassword.toString().toCharArray(),
|
||||
concatenate(salt, this.secret), this.iterations, this.hashWidth);
|
||||
PBEKeySpec spec = new PBEKeySpec(rawPassword.toString().toCharArray(), concatenate(salt, this.secret),
|
||||
this.iterations, this.hashWidth);
|
||||
SecretKeyFactory skf = SecretKeyFactory.getInstance(this.algorithm);
|
||||
return concatenate(salt, skf.generateSecret(spec).getEncoded());
|
||||
}
|
||||
@@ -170,8 +176,9 @@ public class Pbkdf2PasswordEncoder implements PasswordEncoder {
|
||||
* @since 5.0
|
||||
*/
|
||||
public enum SecretKeyFactoryAlgorithm {
|
||||
PBKDF2WithHmacSHA1,
|
||||
PBKDF2WithHmacSHA256,
|
||||
PBKDF2WithHmacSHA512
|
||||
|
||||
PBKDF2WithHmacSHA1, PBKDF2WithHmacSHA256, PBKDF2WithHmacSHA512
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,8 +45,8 @@ import java.security.MessageDigest;
|
||||
* @deprecated Digest based password encoding is not considered secure. Instead use an
|
||||
* adaptive one way function like BCryptPasswordEncoder, Pbkdf2PasswordEncoder, or
|
||||
* SCryptPasswordEncoder. Even better use {@link DelegatingPasswordEncoder} which supports
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to indicate
|
||||
* that this is a legacy implementation and using it is considered insecure.
|
||||
* password upgrades. There are no plans to remove this support. It is deprecated to
|
||||
* indicate that this is a legacy implementation and using it is considered insecure.
|
||||
*/
|
||||
@Deprecated
|
||||
public final class StandardPasswordEncoder implements PasswordEncoder {
|
||||
@@ -67,7 +67,6 @@ public final class StandardPasswordEncoder implements PasswordEncoder {
|
||||
/**
|
||||
* Constructs a standard password encoder with a secret value which is also included
|
||||
* in the password hash.
|
||||
*
|
||||
* @param secret the secret key used in the encoding process (should not be shared)
|
||||
*/
|
||||
public StandardPasswordEncoder(CharSequence secret) {
|
||||
@@ -98,8 +97,7 @@ public final class StandardPasswordEncoder implements PasswordEncoder {
|
||||
}
|
||||
|
||||
private byte[] digest(CharSequence rawPassword, byte[] salt) {
|
||||
byte[] digest = digester.digest(concatenate(salt, secret,
|
||||
Utf8.encode(rawPassword)));
|
||||
byte[] digest = digester.digest(concatenate(salt, secret, Utf8.encode(rawPassword)));
|
||||
return concatenate(salt, digest);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,9 +28,9 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Implementation of PasswordEncoder that uses the SCrypt hashing function.
|
||||
* Clients can optionally supply a cpu cost parameter, a memory cost parameter
|
||||
* and a parallelization parameter.
|
||||
* Implementation of PasswordEncoder that uses the SCrypt hashing function. Clients can
|
||||
* optionally supply a cpu cost parameter, a memory cost parameter and a parallelization
|
||||
* parameter.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
@@ -41,13 +41,13 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
*
|
||||
* <ul>
|
||||
* <li>The currently implementation uses Bouncy castle which does not exploit
|
||||
* parallelism/optimizations that password crackers will, so there is an
|
||||
* unnecessary asymmetry between attacker and defender.</li>
|
||||
* <li>Scrypt is based on Salsa20 which performs poorly in Java (on par with
|
||||
* AES) but performs awesome (~4-5x faster) on SIMD capable platforms</li>
|
||||
* parallelism/optimizations that password crackers will, so there is an unnecessary
|
||||
* asymmetry between attacker and defender.</li>
|
||||
* <li>Scrypt is based on Salsa20 which performs poorly in Java (on par with AES) but
|
||||
* performs awesome (~4-5x faster) on SIMD capable platforms</li>
|
||||
* <li>While there are some that would disagree, consider reading -
|
||||
* <a href="https://blog.ircmaxell.com/2014/03/why-i-dont-recommend-scrypt.html">
|
||||
* Why I Don't Recommend Scrypt</a> (for password storage)</li>
|
||||
* <a href="https://blog.ircmaxell.com/2014/03/why-i-dont-recommend-scrypt.html"> Why I
|
||||
* Don't Recommend Scrypt</a> (for password storage)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Shazin Sadakath
|
||||
@@ -74,25 +74,17 @@ public class SCryptPasswordEncoder implements PasswordEncoder {
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param cpuCost
|
||||
* cpu cost of the algorithm (as defined in scrypt this is N).
|
||||
* must be power of 2 greater than 1. Default is currently 16,384
|
||||
* or 2^14)
|
||||
* @param memoryCost
|
||||
* memory cost of the algorithm (as defined in scrypt this is r)
|
||||
* Default is currently 8.
|
||||
* @param parallelization
|
||||
* the parallelization of the algorithm (as defined in scrypt
|
||||
* this is p) Default is currently 1. Note that the
|
||||
* implementation does not currently take advantage of
|
||||
* parallelization.
|
||||
* @param keyLength
|
||||
* key length for the algorithm (as defined in scrypt this is
|
||||
* dkLen). The default is currently 32.
|
||||
* @param saltLength
|
||||
* salt length (as defined in scrypt this is the length of S).
|
||||
* The default is currently 64.
|
||||
* @param cpuCost cpu cost of the algorithm (as defined in scrypt this is N). must be
|
||||
* power of 2 greater than 1. Default is currently 16,384 or 2^14)
|
||||
* @param memoryCost memory cost of the algorithm (as defined in scrypt this is r)
|
||||
* Default is currently 8.
|
||||
* @param parallelization the parallelization of the algorithm (as defined in scrypt
|
||||
* this is p) Default is currently 1. Note that the implementation does not currently
|
||||
* take advantage of parallelization.
|
||||
* @param keyLength key length for the algorithm (as defined in scrypt this is dkLen).
|
||||
* The default is currently 32.
|
||||
* @param saltLength salt length (as defined in scrypt this is the length of S). The
|
||||
* default is currently 64.
|
||||
*/
|
||||
public SCryptPasswordEncoder(int cpuCost, int memoryCost, int parallelization, int keyLength, int saltLength) {
|
||||
if (cpuCost <= 1) {
|
||||
@@ -153,9 +145,7 @@ public class SCryptPasswordEncoder implements PasswordEncoder {
|
||||
int memoryCost = (int) params >> 8 & 0xff;
|
||||
int parallelization = (int) params & 0xff;
|
||||
|
||||
return cpuCost < this.cpuCost
|
||||
|| memoryCost < this.memoryCost
|
||||
|| parallelization < this.parallelization;
|
||||
return cpuCost < this.cpuCost || memoryCost < this.memoryCost || parallelization < this.parallelization;
|
||||
}
|
||||
|
||||
private boolean decodeAndCheckMatches(CharSequence rawPassword, String encodedPassword) {
|
||||
@@ -180,7 +170,8 @@ public class SCryptPasswordEncoder implements PasswordEncoder {
|
||||
}
|
||||
|
||||
private String digest(CharSequence rawPassword, byte[] salt) {
|
||||
byte[] derived = SCrypt.generate(Utf8.encode(rawPassword), salt, cpuCost, memoryCost, parallelization, keyLength);
|
||||
byte[] derived = SCrypt.generate(Utf8.encode(rawPassword), salt, cpuCost, memoryCost, parallelization,
|
||||
keyLength);
|
||||
|
||||
String params = Long
|
||||
.toString(((int) (Math.log(cpuCost) / Math.log(2)) << 16L) | memoryCost << 8 | parallelization, 16);
|
||||
@@ -200,4 +191,5 @@ public class SCryptPasswordEncoder implements PasswordEncoder {
|
||||
private String encodePart(byte[] part) {
|
||||
return Utf8.decode(Base64.getEncoder().encode(part));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,18 +31,16 @@ public class Argon2EncodingUtilsTests {
|
||||
private TestDataEntry testDataEntry1 = new TestDataEntry(
|
||||
"$argon2i$v=19$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs",
|
||||
new Argon2EncodingUtils.Argon2Hash(decoder.decode("cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs"),
|
||||
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_i)).
|
||||
withVersion(19).withMemoryAsKB(1024).withIterations(3).withParallelism(2).
|
||||
withSalt("cRdFbCw23gz2Mlxk".getBytes()).build()
|
||||
));
|
||||
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_i)).withVersion(19).withMemoryAsKB(1024)
|
||||
.withIterations(3).withParallelism(2).withSalt("cRdFbCw23gz2Mlxk".getBytes()).build()));
|
||||
|
||||
private TestDataEntry testDataEntry2 = new TestDataEntry(
|
||||
"$argon2id$v=19$m=333,t=5,p=2$JDR8N3k1QWx0$+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA",
|
||||
new Argon2EncodingUtils.Argon2Hash(decoder.decode("+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA"),
|
||||
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)).
|
||||
withVersion(19).withMemoryAsKB(333).withIterations(5).withParallelism(2).
|
||||
withSalt("$4|7y5Alt".getBytes()).build()
|
||||
));
|
||||
new Argon2EncodingUtils.Argon2Hash(
|
||||
decoder.decode(
|
||||
"+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA"),
|
||||
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)).withVersion(19).withMemoryAsKB(333)
|
||||
.withIterations(5).withParallelism(2).withSalt("$4|7y5Alt".getBytes()).build()));
|
||||
|
||||
@Test
|
||||
public void decodeWhenValidEncodedHashWithIThenDecodeCorrectly() {
|
||||
@@ -56,22 +54,20 @@ public class Argon2EncodingUtilsTests {
|
||||
|
||||
@Test
|
||||
public void encodeWhenValidArgumentsWithIThenEncodeToCorrectHash() {
|
||||
assertThat(Argon2EncodingUtils
|
||||
.encode(testDataEntry1.decoded.getHash(), testDataEntry1.decoded.getParameters()))
|
||||
assertThat(Argon2EncodingUtils.encode(testDataEntry1.decoded.getHash(), testDataEntry1.decoded.getParameters()))
|
||||
.isEqualTo(testDataEntry1.encoded);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeWhenValidArgumentsWithID2ThenEncodeToCorrectHash() {
|
||||
assertThat(Argon2EncodingUtils
|
||||
.encode(testDataEntry2.decoded.getHash(), testDataEntry2.decoded.getParameters()))
|
||||
assertThat(Argon2EncodingUtils.encode(testDataEntry2.decoded.getHash(), testDataEntry2.decoded.getParameters()))
|
||||
.isEqualTo(testDataEntry2.encoded);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void encodeWhenNonexistingAlgorithmThenThrowException() {
|
||||
Argon2EncodingUtils.encode(new byte[]{0, 1, 2, 3}, (new Argon2Parameters.Builder(3)).
|
||||
withVersion(19).withMemoryAsKB(333).withIterations(5).withParallelism(2).build());
|
||||
Argon2EncodingUtils.encode(new byte[] { 0, 1, 2, 3 }, (new Argon2Parameters.Builder(3)).withVersion(19)
|
||||
.withMemoryAsKB(333).withIterations(5).withParallelism(2).build());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -81,70 +77,80 @@ public class Argon2EncodingUtilsTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenNonexistingAlgorithmThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2x$v=19$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils.decode(
|
||||
"$argon2x$v=19$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenIllegalVersionParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$v=x$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils.decode(
|
||||
"$argon2i$v=x$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenIllegalMemoryParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$v=19$m=x,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils
|
||||
.decode("$argon2i$v=19$m=x,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenIllegalIterationsParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,t=x,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils.decode(
|
||||
"$argon2i$v=19$m=1024,t=x,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenIllegalParallelityParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils.decode(
|
||||
"$argon2i$v=19$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenMissingVersionParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils
|
||||
.decode("$argon2i$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenMissingMemoryParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$v=19$t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils
|
||||
.decode("$argon2i$v=19$t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenMissingIterationsParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils
|
||||
.decode("$argon2i$v=19$m=1024,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeWhenMissingParallelityParameterThenThrowException() {
|
||||
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,t=3$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
Argon2EncodingUtils
|
||||
.decode("$argon2i$v=19$m=1024,t=3$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
|
||||
}
|
||||
|
||||
private void assertArgon2HashEquals(Argon2EncodingUtils.Argon2Hash expected, Argon2EncodingUtils.Argon2Hash actual) {
|
||||
private void assertArgon2HashEquals(Argon2EncodingUtils.Argon2Hash expected,
|
||||
Argon2EncodingUtils.Argon2Hash actual) {
|
||||
assertThat(actual.getHash()).isEqualTo(expected.getHash());
|
||||
assertThat(actual.getParameters().getSalt()).isEqualTo(expected.getParameters().getSalt());
|
||||
assertThat(actual.getParameters().getType()).isEqualTo(expected.getParameters().getType());
|
||||
assertThat(actual.getParameters().getVersion())
|
||||
.isEqualTo(expected.getParameters().getVersion());
|
||||
assertThat(actual.getParameters().getMemory())
|
||||
.isEqualTo(expected.getParameters().getMemory());
|
||||
assertThat(actual.getParameters().getIterations())
|
||||
.isEqualTo(expected.getParameters().getIterations());
|
||||
assertThat(actual.getParameters().getLanes())
|
||||
.isEqualTo(expected.getParameters().getLanes());
|
||||
assertThat(actual.getParameters().getVersion()).isEqualTo(expected.getParameters().getVersion());
|
||||
assertThat(actual.getParameters().getMemory()).isEqualTo(expected.getParameters().getMemory());
|
||||
assertThat(actual.getParameters().getIterations()).isEqualTo(expected.getParameters().getIterations());
|
||||
assertThat(actual.getParameters().getLanes()).isEqualTo(expected.getParameters().getLanes());
|
||||
}
|
||||
|
||||
private static class TestDataEntry {
|
||||
|
||||
String encoded;
|
||||
|
||||
Argon2EncodingUtils.Argon2Hash decoded;
|
||||
|
||||
TestDataEntry(String encoded, Argon2EncodingUtils.Argon2Hash decoded) {
|
||||
this.encoded = encoded;
|
||||
this.decoded = decoded;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -198,17 +198,18 @@ public class Argon2PasswordEncoderTests {
|
||||
encoder.upgradeEncoding("thisIsNoValidHash");
|
||||
}
|
||||
|
||||
|
||||
private void injectPredictableSaltGen() throws Exception {
|
||||
byte[] bytes = new byte[16];
|
||||
Arrays.fill(bytes, (byte) 0x41);
|
||||
Mockito.when(keyGeneratorMock.generateKey()).thenReturn(bytes);
|
||||
|
||||
//we can't use the @InjectMock-annotation because the salt-generator is set in the constructor
|
||||
//and Mockito will only inject mocks if they are null
|
||||
// we can't use the @InjectMock-annotation because the salt-generator is set in
|
||||
// the constructor
|
||||
// and Mockito will only inject mocks if they are null
|
||||
Field saltGen = encoder.getClass().getDeclaredField("saltGenerator");
|
||||
saltGen.setAccessible(true);
|
||||
saltGen.set(encoder, keyGeneratorMock);
|
||||
saltGen.setAccessible(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -79,8 +79,7 @@ public class BCryptPasswordEncoderTests {
|
||||
|
||||
@Test
|
||||
public void $2bUnicode() {
|
||||
BCryptPasswordEncoder encoder =
|
||||
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
|
||||
String result = encoder.encode("passw\u9292rd");
|
||||
assertThat(encoder.matches("pass\u9292\u9292rd", result)).isFalse();
|
||||
assertThat(encoder.matches("passw\u9292rd", result)).isTrue();
|
||||
@@ -96,16 +95,14 @@ public class BCryptPasswordEncoderTests {
|
||||
|
||||
@Test
|
||||
public void $2aNotMatches() {
|
||||
BCryptPasswordEncoder encoder =
|
||||
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A);
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A);
|
||||
String result = encoder.encode("password");
|
||||
assertThat(encoder.matches("bogus", result)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void $2bNotMatches() {
|
||||
BCryptPasswordEncoder encoder =
|
||||
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
|
||||
String result = encoder.encode("password");
|
||||
assertThat(encoder.matches("bogus", result)).isFalse();
|
||||
}
|
||||
@@ -120,16 +117,14 @@ public class BCryptPasswordEncoderTests {
|
||||
|
||||
@Test
|
||||
public void $2aCustomStrength() {
|
||||
BCryptPasswordEncoder encoder =
|
||||
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A, 8);
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A, 8);
|
||||
String result = encoder.encode("password");
|
||||
assertThat(encoder.matches("password", result)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void $2bCustomStrength() {
|
||||
BCryptPasswordEncoder encoder =
|
||||
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B, 8);
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B, 8);
|
||||
String result = encoder.encode("password");
|
||||
assertThat(encoder.matches("password", result)).isTrue();
|
||||
}
|
||||
@@ -182,7 +177,8 @@ public class BCryptPasswordEncoderTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see <a href="https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
|
||||
* @see <a href=
|
||||
* "https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
|
||||
*/
|
||||
@Test
|
||||
public void upgradeFromNullOrEmpty() {
|
||||
@@ -192,7 +188,8 @@ public class BCryptPasswordEncoderTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see <a href="https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
|
||||
* @see <a href=
|
||||
* "https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void upgradeFromNonBCrypt() {
|
||||
|
||||
@@ -25,13 +25,17 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* JUnit unit tests for BCrypt routines
|
||||
*
|
||||
* @author Damien Miller
|
||||
*/
|
||||
public class BCryptTests {
|
||||
|
||||
private static class TestObject<T> {
|
||||
|
||||
private final T password;
|
||||
|
||||
private final String salt;
|
||||
|
||||
private final String expected;
|
||||
|
||||
private TestObject(T password, String salt, String expected) {
|
||||
@@ -39,6 +43,7 @@ public class BCryptTests {
|
||||
this.salt = salt;
|
||||
this.expected = expected;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void print(String s) {
|
||||
@@ -138,21 +143,21 @@ public class BCryptTests {
|
||||
"$2y$06$6Xm0gCw4g7ZNDCEp4yTisez0kSdpXEl66MvdxGidnmChIe8dFmMnq"));
|
||||
|
||||
testObjectsByteArray = new ArrayList<>();
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$06$fPIsBO8qRqkjj273rfaOI.",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$06$fPIsBO8qRqkjj273rfaOI.",
|
||||
"$2a$06$fPIsBO8qRqkjj273rfaOI.uiVGfgi6Z1Iz.vZr11mi/38o09TUVCy"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$08$Eq2r4G/76Wv39MzSX262hu",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$08$Eq2r4G/76Wv39MzSX262hu",
|
||||
"$2a$08$Eq2r4G/76Wv39MzSX262hu2lrqIItOWKIkPsMMvm5LAFD.iVB7Nmm"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$10$LgfYWkbzEvQ4JakH7rOvHe",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$10$LgfYWkbzEvQ4JakH7rOvHe",
|
||||
"$2a$10$LgfYWkbzEvQ4JakH7rOvHeU6pINYiHnazYxe4GikGWx9MaUr27Vpa"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$12$WApznUOJfkEGSmYRfnkrPO",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$12$WApznUOJfkEGSmYRfnkrPO",
|
||||
"$2a$12$WApznUOJfkEGSmYRfnkrPONS3wcUvmKuh3LpjxSs6g78T77gZta3W"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2b$06$FGWA8OlY6RtQhXBXuCJ8Wu",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2b$06$FGWA8OlY6RtQhXBXuCJ8Wu",
|
||||
"$2b$06$FGWA8OlY6RtQhXBXuCJ8Wu5oPJaT8BeCRmS273I6cpp5RwwjAWn7S"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2b$06$G6aYU7UhUEUDJBdTgq3CRe",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2b$06$G6aYU7UhUEUDJBdTgq3CRe",
|
||||
"$2b$06$G6aYU7UhUEUDJBdTgq3CRebzUYAyG8MCS3WdBk0CcPb9bfj1.3cSG"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2y$06$sYDFHqOcXTjBgOsqC0WCKe",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2y$06$sYDFHqOcXTjBgOsqC0WCKe",
|
||||
"$2y$06$sYDFHqOcXTjBgOsqC0WCKeOv88fqPKkuV1yGVh./TROmn1mL8gYh2"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2y$06$6Xm0gCw4g7ZNDCEp4yTise",
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2y$06$6Xm0gCw4g7ZNDCEp4yTise",
|
||||
"$2y$06$6Xm0gCw4g7ZNDCEp4yTisecBqTHmLJBHxTNZa8w2hupJKsIhPWOgG"));
|
||||
testObjectsByteArray.add(new TestObject<>(new byte[] { -11 }, "$2a$06$fPIsBO8qRqkjj273rfaOI.",
|
||||
"$2a$06$fPIsBO8qRqkjj273rfaOI.AyMTPwvUEmZ2EdJM/p0S0eP3UQpBas."));
|
||||
@@ -342,8 +347,7 @@ public class BCryptTests {
|
||||
BCrypt.decode_base64("", 0);
|
||||
}
|
||||
|
||||
private static String encode_base64(byte d[], int len)
|
||||
throws IllegalArgumentException {
|
||||
private static String encode_base64(byte d[], int len) throws IllegalArgumentException {
|
||||
StringBuilder rs = new StringBuilder();
|
||||
BCrypt.encode_base64(d, len, rs);
|
||||
return rs.toString();
|
||||
@@ -353,7 +357,7 @@ public class BCryptTests {
|
||||
public void testBase64EncodeSimpleByteArrays() {
|
||||
assertThat(encode_base64(new byte[] { 0 }, 1)).isEqualTo("..");
|
||||
assertThat(encode_base64(new byte[] { 0, 0 }, 2)).isEqualTo("...");
|
||||
assertThat(encode_base64(new byte[] { 0, 0 , 0 }, 3)).isEqualTo("....");
|
||||
assertThat(encode_base64(new byte[] { 0, 0, 0 }, 3)).isEqualTo("....");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -435,8 +439,8 @@ public class BCryptTests {
|
||||
|
||||
@Test
|
||||
public void hashpwWorksWithOldRevision() {
|
||||
assertThat(BCrypt.hashpw("password", "$2$05$......................")).isEqualTo(
|
||||
"$2$05$......................bvpG2UfzdyW/S0ny/4YyEZrmczoJfVm");
|
||||
assertThat(BCrypt.hashpw("password", "$2$05$......................"))
|
||||
.isEqualTo("$2$05$......................bvpG2UfzdyW/S0ny/4YyEZrmczoJfVm");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -454,4 +458,5 @@ public class BCryptTests {
|
||||
|
||||
assertThat(BCrypt.equalsNoEarlyReturn("test", "pass")).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,15 +29,13 @@ public class Base64Tests {
|
||||
public void isBase64ReturnsTrueForValidBase64() {
|
||||
new Base64(); // unused
|
||||
|
||||
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
|
||||
(byte) 'D' })).isTrue();
|
||||
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D' })).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isBase64ReturnsFalseForInvalidBase64() {
|
||||
// Include invalid '`' character
|
||||
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
|
||||
(byte) '`' })).isFalse();
|
||||
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) '`' })).isFalse();
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
@@ -49,4 +47,5 @@ public class Base64Tests {
|
||||
public void isBase64RejectsInvalidLength() {
|
||||
Base64.isBase64(new byte[] { (byte) 'A' });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ public class HexTests {
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
assertThat(Hex.encode(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
|
||||
(byte) 'D' })).isEqualTo(new char[] {'4', '1', '4', '2', '4', '3', '4', '4'});
|
||||
assertThat(Hex.encode(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D' }))
|
||||
.isEqualTo(new char[] { '4', '1', '4', '2', '4', '3', '4', '4' });
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -44,8 +44,7 @@ public class HexTests {
|
||||
|
||||
@Test
|
||||
public void decode() {
|
||||
assertThat(Hex.decode("41424344")).isEqualTo(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
|
||||
(byte) 'D' });
|
||||
assertThat(Hex.decode("41424344")).isEqualTo(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D' });
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -37,4 +37,5 @@ public class Utf8Tests {
|
||||
|
||||
assertThat(decoded).isEqualTo("6048b75ed560785c");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,8 +36,11 @@ import static org.springframework.security.crypto.password.Pbkdf2PasswordEncoder
|
||||
* Tests for {@link AesBytesEncryptor}
|
||||
*/
|
||||
public class AesBytesEncryptorTests {
|
||||
|
||||
private String secret = "value";
|
||||
|
||||
private String password = "password";
|
||||
|
||||
private String hexSalt = "deadbeef";
|
||||
|
||||
private BytesKeyGenerator generator;
|
||||
@@ -86,8 +89,7 @@ public class AesBytesEncryptorTests {
|
||||
@Test
|
||||
public void roundtripWhenUsingSecretKeyThenEncryptsAndDecrypts() {
|
||||
CryptoAssumptions.assumeGCMJCE();
|
||||
PBEKeySpec keySpec = new PBEKeySpec(this.password.toCharArray(), Hex.decode(this.hexSalt),
|
||||
1024, 256);
|
||||
PBEKeySpec keySpec = new PBEKeySpec(this.password.toCharArray(), Hex.decode(this.hexSalt), 1024, 256);
|
||||
SecretKey secretKey = newSecretKey(PBKDF2WithHmacSHA1.name(), keySpec);
|
||||
AesBytesEncryptor encryptor = new AesBytesEncryptor(secretKey, this.generator, GCM);
|
||||
|
||||
|
||||
@@ -30,15 +30,18 @@ import org.springframework.security.crypto.keygen.KeyGenerators;
|
||||
public class BouncyCastleAesBytesEncryptorEquivalencyTests {
|
||||
|
||||
private byte[] testData;
|
||||
|
||||
private String password;
|
||||
|
||||
private String salt;
|
||||
|
||||
private SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
// generate random password, salt, and test data
|
||||
password = UUID.randomUUID().toString();
|
||||
/** insecure salt byte, recommend 64 or larger than 64*/
|
||||
/** insecure salt byte, recommend 64 or larger than 64 */
|
||||
byte[] saltBytes = new byte[16];
|
||||
secureRandom.nextBytes(saltBytes);
|
||||
salt = new String(Hex.encode(saltBytes));
|
||||
@@ -49,8 +52,7 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
|
||||
CryptoAssumptions.assumeCBCJCE();
|
||||
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
|
||||
new PredictableRandomBytesKeyGenerator(16));
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
|
||||
new PredictableRandomBytesKeyGenerator(16));
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16));
|
||||
testEquivalence(bcEncryptor, jceEncryptor);
|
||||
}
|
||||
|
||||
@@ -59,8 +61,7 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
|
||||
CryptoAssumptions.assumeCBCJCE();
|
||||
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
|
||||
KeyGenerators.secureRandom(16));
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
|
||||
KeyGenerators.secureRandom(16));
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16));
|
||||
testCompatibility(bcEncryptor, jceEncryptor);
|
||||
}
|
||||
|
||||
@@ -69,8 +70,8 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
|
||||
CryptoAssumptions.assumeGCMJCE();
|
||||
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
|
||||
new PredictableRandomBytesKeyGenerator(16));
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
|
||||
new PredictableRandomBytesKeyGenerator(16), CipherAlgorithm.GCM);
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16),
|
||||
CipherAlgorithm.GCM);
|
||||
testEquivalence(bcEncryptor, jceEncryptor);
|
||||
}
|
||||
|
||||
@@ -79,8 +80,8 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
|
||||
CryptoAssumptions.assumeGCMJCE();
|
||||
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
|
||||
KeyGenerators.secureRandom(16));
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
|
||||
KeyGenerators.secureRandom(16), CipherAlgorithm.GCM);
|
||||
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16),
|
||||
CipherAlgorithm.GCM);
|
||||
testCompatibility(bcEncryptor, jceEncryptor);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,9 @@ import org.springframework.security.crypto.keygen.KeyGenerators;
|
||||
public class BouncyCastleAesBytesEncryptorTests {
|
||||
|
||||
private byte[] testData;
|
||||
|
||||
private String password;
|
||||
|
||||
private String salt;
|
||||
|
||||
@Before
|
||||
@@ -67,13 +69,12 @@ public class BouncyCastleAesBytesEncryptorTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bcCbcWithWrongLengthIv() {
|
||||
new BouncyCastleAesCbcBytesEncryptor(password, salt,
|
||||
KeyGenerators.secureRandom(8));
|
||||
new BouncyCastleAesCbcBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bcGcmWithWrongLengthIv() {
|
||||
new BouncyCastleAesGcmBytesEncryptor(password, salt,
|
||||
KeyGenerators.secureRandom(8));
|
||||
new BouncyCastleAesGcmBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,16 +41,12 @@ public class CryptoAssumptions {
|
||||
aes256Available = Cipher.getMaxAllowedKeyLength("AES") >= 256;
|
||||
}
|
||||
catch (NoSuchAlgorithmException e) {
|
||||
throw new AssumptionViolatedException(
|
||||
cipherAlgorithm + " not available, skipping test", e);
|
||||
throw new AssumptionViolatedException(cipherAlgorithm + " not available, skipping test", e);
|
||||
}
|
||||
catch (NoSuchPaddingException e) {
|
||||
throw new AssumptionViolatedException(
|
||||
cipherAlgorithm + " padding not available, skipping test", e);
|
||||
throw new AssumptionViolatedException(cipherAlgorithm + " padding not available, skipping test", e);
|
||||
}
|
||||
Assume.assumeTrue(
|
||||
"AES key length of 256 not allowed, skipping test",
|
||||
aes256Available);
|
||||
Assume.assumeTrue("AES key length of 256 not allowed, skipping test", aes256Available);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -29,8 +29,7 @@ public class EncryptorsTests {
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(new String(result).equals("text")).isFalse();
|
||||
assertThat(new String(encryptor.decrypt(result))).isEqualTo("text");
|
||||
assertThat(new String(result)).isNotEqualTo(
|
||||
new String(encryptor.encrypt("text".getBytes())));
|
||||
assertThat(new String(result)).isNotEqualTo(new String(encryptor.encrypt("text".getBytes())));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -41,8 +40,7 @@ public class EncryptorsTests {
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(new String(result).equals("text")).isFalse();
|
||||
assertThat(new String(encryptor.decrypt(result))).isEqualTo("text");
|
||||
assertThat(new String(result)).isNotEqualTo(
|
||||
new String(encryptor.encrypt("text".getBytes())));
|
||||
assertThat(new String(result)).isNotEqualTo(new String(encryptor.encrypt("text".getBytes())));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -70,8 +68,7 @@ public class EncryptorsTests {
|
||||
@Test
|
||||
public void queryableText() {
|
||||
CryptoAssumptions.assumeCBCJCE();
|
||||
TextEncryptor encryptor = Encryptors.queryableText("password",
|
||||
"5c0744940b5c369b");
|
||||
TextEncryptor encryptor = Encryptors.queryableText("password", "5c0744940b5c369b");
|
||||
String result = encryptor.encrypt("text");
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.equals("text")).isFalse();
|
||||
|
||||
@@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @since 5.0
|
||||
*/
|
||||
public class PasswordEncoderFactoriesTests {
|
||||
|
||||
private PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||
|
||||
private String rawPassword = "password";
|
||||
|
||||
@@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.*;
|
||||
* @since 5.0
|
||||
*/
|
||||
public class Base64StringKeyGeneratorTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorIntWhenLessThan32ThenIllegalArgumentException() {
|
||||
new Base64StringKeyGenerator(31);
|
||||
@@ -63,4 +64,5 @@ public class Base64StringKeyGeneratorTests {
|
||||
String result = new Base64StringKeyGenerator(Base64.getUrlEncoder(), size).generateKey();
|
||||
assertThat(Base64.getUrlDecoder().decode(result.getBytes())).hasSize(size);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.when;
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DelegatingPasswordEncoderTests {
|
||||
|
||||
@Mock
|
||||
private PasswordEncoder bcrypt;
|
||||
|
||||
@@ -128,8 +129,8 @@ public class DelegatingPasswordEncoderTests {
|
||||
@Test
|
||||
public void matchesWhenUnMappedThenIllegalArgumentException() {
|
||||
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{unmapped}" + this.rawPassword))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"unmapped\"");
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"unmapped\"");
|
||||
|
||||
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||
}
|
||||
@@ -137,8 +138,8 @@ public class DelegatingPasswordEncoderTests {
|
||||
@Test
|
||||
public void matchesWhenNoClosingPrefixStringThenIllegalArgumentExcetion() {
|
||||
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{bcrypt" + this.rawPassword))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
|
||||
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||
}
|
||||
@@ -146,8 +147,8 @@ public class DelegatingPasswordEncoderTests {
|
||||
@Test
|
||||
public void matchesWhenNoStartingPrefixStringThenFalse() {
|
||||
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "bcrypt}" + this.rawPassword))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
|
||||
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||
}
|
||||
@@ -155,8 +156,8 @@ public class DelegatingPasswordEncoderTests {
|
||||
@Test
|
||||
public void matchesWhenNoIdStringThenFalse() {
|
||||
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{}" + this.rawPassword))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"\"");
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"\"");
|
||||
|
||||
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||
}
|
||||
@@ -164,8 +165,8 @@ public class DelegatingPasswordEncoderTests {
|
||||
@Test
|
||||
public void matchesWhenPrefixInMiddleThenFalse() {
|
||||
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "invalid" + this.bcryptEncodedPassword))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
|
||||
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||
}
|
||||
@@ -177,8 +178,8 @@ public class DelegatingPasswordEncoderTests {
|
||||
DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder(this.bcryptId, this.delegates);
|
||||
|
||||
assertThatThrownBy(() -> passwordEncoder.matches(this.rawPassword, this.rawPassword))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||
|
||||
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||
}
|
||||
@@ -212,7 +213,7 @@ public class DelegatingPasswordEncoderTests {
|
||||
|
||||
@Test
|
||||
public void upgradeEncodingWhenIdInvalidFormatThenTrue() {
|
||||
assertThat(this.passwordEncoder.upgradeEncoding("{bcrypt"+ this.encodedPassword)).isTrue();
|
||||
assertThat(this.passwordEncoder.upgradeEncoding("{bcrypt" + this.encodedPassword)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -237,4 +238,5 @@ public class DelegatingPasswordEncoderTests {
|
||||
|
||||
verifyZeroInteractions(bcrypt);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class LdapShaPasswordEncoderTests {
|
||||
|
||||
// ~ Instance fields
|
||||
// ================================================================================================
|
||||
|
||||
@@ -109,4 +110,5 @@ public class LdapShaPasswordEncoderTests {
|
||||
// No right brace
|
||||
this.sha.matches("somepassword", "{SSHA25ro4PKC8jhQZ26jVsozhX/xaP0suHgX");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.security.crypto.password;
|
||||
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -72,5 +71,5 @@ public class Md4PasswordEncoderTests {
|
||||
Md4PasswordEncoder encoder = new Md4PasswordEncoder();
|
||||
assertThat(encoder.matches("password", "{thisissalt}6cc7924dad12ade79dfb99e424f25260"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class MessageDigestPasswordEncoderTests {
|
||||
|
||||
// ~ Methods
|
||||
// ========================================================================================================
|
||||
|
||||
@@ -39,7 +40,7 @@ public class MessageDigestPasswordEncoderTests {
|
||||
public void md5BasicFunctionality() {
|
||||
MessageDigestPasswordEncoder pe = new MessageDigestPasswordEncoder("MD5");
|
||||
String raw = "abc123";
|
||||
assertThat(pe.matches( raw, "{THIS_IS_A_SALT}a68aafd90299d0b137de28fb4bb68573")).isTrue();
|
||||
assertThat(pe.matches(raw, "{THIS_IS_A_SALT}a68aafd90299d0b137de28fb4bb68573")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -119,4 +120,5 @@ public class MessageDigestPasswordEncoderTests {
|
||||
public void testInvalidStrength() {
|
||||
new MessageDigestPasswordEncoder("SHA-666");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,4 +67,5 @@ public class PasswordEncoderUtilsTests {
|
||||
public void equalsWhenSameThenTrue() {
|
||||
assertThat(PasswordEncoderUtils.equals("abcdef", "abcdef")).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.springframework.security.crypto.keygen.KeyGenerators;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class Pbkdf2PasswordEncoderTests {
|
||||
|
||||
private Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder("secret");
|
||||
|
||||
@Test
|
||||
@@ -37,8 +38,7 @@ public class Pbkdf2PasswordEncoderTests {
|
||||
@Test
|
||||
public void matchesLengthChecked() {
|
||||
String result = this.encoder.encode("password");
|
||||
assertThat(this.encoder.matches("password",
|
||||
result.substring(0, result.length() - 2))).isFalse();
|
||||
assertThat(this.encoder.matches("password", result.substring(0, result.length() - 2))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -68,8 +68,7 @@ public class Pbkdf2PasswordEncoderTests {
|
||||
String encodedPassword = "ab1146a8458d4ce4e65789e5a3f60e423373cfa10b01abd23739e5ae2fdc37f8e9ede4ae6da65264";
|
||||
String originalEncodedPassword = "ab1146a8458d4ce4ab1146a8458d4ce4e65789e5a3f60e423373cfa10b01abd23739e5ae2fdc37f8e9ede4ae6da65264";
|
||||
byte[] originalBytes = Hex.decode(originalEncodedPassword);
|
||||
byte[] fixedBytes = Arrays.copyOfRange(originalBytes, saltLength,
|
||||
originalBytes.length);
|
||||
byte[] fixedBytes = Arrays.copyOfRange(originalBytes, saltLength, originalBytes.length);
|
||||
String fixedHex = String.valueOf(Hex.encode(fixedBytes));
|
||||
|
||||
assertThat(fixedHex).isEqualTo(encodedPassword);
|
||||
@@ -91,7 +90,8 @@ public class Pbkdf2PasswordEncoderTests {
|
||||
String encodedPassword = "3FOwOMcDgxP+z1x/sv184LFY2WVD+ZGMgYP3LPOSmCcDmk1XPYvcCQ==";
|
||||
|
||||
assertThat(this.encoder.matches(rawPassword, encodedPassword)).isTrue();
|
||||
java.util.Base64.getDecoder().decode(encodedPassword); // validate can decode as Base64
|
||||
java.util.Base64.getDecoder().decode(encodedPassword); // validate can decode as
|
||||
// Base64
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,6 +111,7 @@ public class Pbkdf2PasswordEncoderTests {
|
||||
String encodedPassword = "821447f994e2b04c5014e31fa9fca4ae1cc9f2188c4ed53d3ddb5ba7980982b51a0ecebfc0b81a79";
|
||||
assertThat(this.encoder.matches(rawPassword, encodedPassword)).isTrue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to find the iteration count that takes .5 seconds.
|
||||
*/
|
||||
@@ -126,8 +127,7 @@ public class Pbkdf2PasswordEncoderTests {
|
||||
long avg = 0;
|
||||
while (avg < HALF_SECOND) {
|
||||
iterations += 10000;
|
||||
Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder("", iterations,
|
||||
256);
|
||||
Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder("", iterations, 256);
|
||||
String encoded = encoder.encode("password");
|
||||
System.out.println("Trying " + iterations);
|
||||
long start = System.currentTimeMillis();
|
||||
@@ -141,4 +141,5 @@ public class Pbkdf2PasswordEncoderTests {
|
||||
}
|
||||
System.out.println("Iterations " + iterations);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -161,5 +161,5 @@ public class SCryptPasswordEncoderTests {
|
||||
public void upgradeEncodingWhenInvalidInputThenException() {
|
||||
new SCryptPasswordEncoder().upgradeEncoding("not-a-scrypt-password");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,35 +26,34 @@ public class EncodingUtilsTests {
|
||||
|
||||
@Test
|
||||
public void hexEncode() {
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
|
||||
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
|
||||
(byte) 0xC2 };
|
||||
String result = new String(Hex.encode(bytes));
|
||||
assertThat(result).isEqualTo("01ff414243c0c1c2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hexDecode() {
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
|
||||
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
|
||||
(byte) 0xC2 };
|
||||
byte[] result = Hex.decode("01ff414243c0c1c2");
|
||||
assertThat(Arrays.equals(bytes, result)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concatenate() {
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
|
||||
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
|
||||
(byte) 0xC2 };
|
||||
byte[] one = new byte[] { (byte) 0x01 };
|
||||
byte[] two = new byte[] { (byte) 0xFF, (byte) 65, (byte) 66 };
|
||||
byte[] three = new byte[] { (byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
|
||||
assertThat(Arrays.equals(bytes,
|
||||
EncodingUtils.concatenate(one, two, three))).isTrue();
|
||||
assertThat(Arrays.equals(bytes, EncodingUtils.concatenate(one, two, three))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subArray() {
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
|
||||
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
|
||||
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
|
||||
(byte) 0xC2 };
|
||||
byte[] two = new byte[] { (byte) 0xFF, (byte) 65, (byte) 66 };
|
||||
byte[] subArray = EncodingUtils.subArray(bytes, 1, 4);
|
||||
assertThat(subArray).hasSize(3);
|
||||
|
||||
Reference in New Issue
Block a user