Always use 'this.' when accessing fields

Apply an Eclipse cleanup rules to ensure that fields are always accessed
using `this.`. This aligns with the style used by Spring Framework and
helps users quickly see the difference between a local and member
variable.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-07-26 11:51:05 -07:00
committed by Rob Winch
parent 6894ff5d12
commit 8866fa6fb0
793 changed files with 8689 additions and 8459 deletions

View File

@@ -174,7 +174,7 @@ class Argon2EncodingUtils {
}
public byte[] getHash() {
return Arrays.clone(hash);
return Arrays.clone(this.hash);
}
public void setHash(byte[] hash) {
@@ -182,7 +182,7 @@ class Argon2EncodingUtils {
}
public Argon2Parameters getParameters() {
return parameters;
return this.parameters;
}
public void setParameters(Argon2Parameters parameters) {

View File

@@ -83,11 +83,11 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
byte[] salt = saltGenerator.generateKey();
byte[] hash = new byte[hashLength];
byte[] salt = this.saltGenerator.generateKey();
byte[] hash = new byte[this.hashLength];
Argon2Parameters params = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id).withSalt(salt)
.withParallelism(parallelism).withMemoryAsKB(memory).withIterations(iterations).build();
.withParallelism(this.parallelism).withMemoryAsKB(this.memory).withIterations(this.iterations).build();
Argon2BytesGenerator generator = new Argon2BytesGenerator();
generator.init(params);
generator.generateBytes(rawPassword.toString().toCharArray(), hash);
@@ -98,7 +98,7 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
if (encodedPassword == null) {
logger.warn("password hash is null");
this.logger.warn("password hash is null");
return false;
}
@@ -108,7 +108,7 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
decoded = Argon2EncodingUtils.decode(encodedPassword);
}
catch (IllegalArgumentException e) {
logger.warn("Malformed password hash", e);
this.logger.warn("Malformed password hash", e);
return false;
}
@@ -124,7 +124,7 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
@Override
public boolean upgradeEncoding(String encodedPassword) {
if (encodedPassword == null || encodedPassword.length() == 0) {
logger.warn("password hash is null");
this.logger.warn("password hash is null");
return false;
}

View File

@@ -321,23 +321,23 @@ public class BCrypt {
private void encipher(int lr[], int off) {
int i, n, l = lr[off], r = lr[off + 1];
l ^= P[0];
l ^= this.P[0];
for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;) {
// Feistel substitution on left word
n = S[(l >> 24) & 0xff];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[++i];
n = this.S[(l >> 24) & 0xff];
n += this.S[0x100 | ((l >> 16) & 0xff)];
n ^= this.S[0x200 | ((l >> 8) & 0xff)];
n += this.S[0x300 | (l & 0xff)];
r ^= n ^ this.P[++i];
// Feistel substitution on right word
n = S[(r >> 24) & 0xff];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[++i];
n = this.S[(r >> 24) & 0xff];
n += this.S[0x100 | ((r >> 16) & 0xff)];
n ^= this.S[0x200 | ((r >> 8) & 0xff)];
n += this.S[0x300 | (r & 0xff)];
l ^= n ^ this.P[++i];
}
lr[off] = r ^ P[BLOWFISH_NUM_ROUNDS + 1];
lr[off] = r ^ this.P[BLOWFISH_NUM_ROUNDS + 1];
lr[off + 1] = l;
}
@@ -394,8 +394,8 @@ public class BCrypt {
* Initialise the Blowfish key schedule
*/
private void init_key() {
P = P_orig.clone();
S = S_orig.clone();
this.P = P_orig.clone();
this.S = S_orig.clone();
}
/**
@@ -408,24 +408,24 @@ public class BCrypt {
int i;
int koffp[] = { 0 };
int lr[] = { 0, 0 };
int plen = P.length, slen = S.length;
int plen = this.P.length, slen = this.S.length;
for (i = 0; i < plen; i++)
if (!sign_ext_bug)
P[i] = P[i] ^ streamtoword(key, koffp);
this.P[i] = this.P[i] ^ streamtoword(key, koffp);
else
P[i] = P[i] ^ streamtoword_bug(key, koffp);
this.P[i] = this.P[i] ^ streamtoword_bug(key, koffp);
for (i = 0; i < plen; i += 2) {
encipher(lr, 0);
P[i] = lr[0];
P[i + 1] = lr[1];
this.P[i] = lr[0];
this.P[i + 1] = lr[1];
}
for (i = 0; i < slen; i += 2) {
encipher(lr, 0);
S[i] = lr[0];
S[i + 1] = lr[1];
this.S[i] = lr[0];
this.S[i + 1] = lr[1];
}
}
@@ -441,14 +441,14 @@ public class BCrypt {
int i;
int koffp[] = { 0 }, doffp[] = { 0 };
int lr[] = { 0, 0 };
int plen = P.length, slen = S.length;
int plen = this.P.length, slen = this.S.length;
int signp[] = { 0 }; // non-benign sign-extension flag
int diff = 0; // zero iff correct and buggy are same
for (i = 0; i < plen; i++) {
int words[] = streamtowords(key, koffp, signp);
diff |= words[0] ^ words[1];
P[i] = P[i] ^ words[sign_ext_bug ? 1 : 0];
this.P[i] = this.P[i] ^ words[sign_ext_bug ? 1 : 0];
}
int sign = signp[0];
@@ -479,22 +479,22 @@ public class BCrypt {
* 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;
this.P[0] ^= sign;
for (i = 0; i < plen; i += 2) {
lr[0] ^= streamtoword(data, doffp);
lr[1] ^= streamtoword(data, doffp);
encipher(lr, 0);
P[i] = lr[0];
P[i + 1] = lr[1];
this.P[i] = lr[0];
this.P[i + 1] = lr[1];
}
for (i = 0; i < slen; i += 2) {
lr[0] ^= streamtoword(data, doffp);
lr[1] ^= streamtoword(data, doffp);
encipher(lr, 0);
S[i] = lr[0];
S[i + 1] = lr[1];
this.S[i] = lr[0];
this.S[i + 1] = lr[1];
}
}

View File

@@ -106,11 +106,11 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
}
String salt;
if (random != null) {
salt = BCrypt.gensalt(version.getVersion(), strength, random);
if (this.random != null) {
salt = BCrypt.gensalt(this.version.getVersion(), this.strength, this.random);
}
else {
salt = BCrypt.gensalt(version.getVersion(), strength);
salt = BCrypt.gensalt(this.version.getVersion(), this.strength);
}
return BCrypt.hashpw(rawPassword.toString(), salt);
}
@@ -121,12 +121,12 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
}
if (encodedPassword == null || encodedPassword.length() == 0) {
logger.warn("Empty encoded password");
this.logger.warn("Empty encoded password");
return false;
}
if (!BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
logger.warn("Encoded password does not look like BCrypt");
if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
this.logger.warn("Encoded password does not look like BCrypt");
return false;
}
@@ -136,11 +136,11 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
@Override
public boolean upgradeEncoding(String encodedPassword) {
if (encodedPassword == null || encodedPassword.length() == 0) {
logger.warn("Empty encoded password");
this.logger.warn("Empty encoded password");
return false;
}
Matcher matcher = BCRYPT_PATTERN.matcher(encodedPassword);
Matcher matcher = this.BCRYPT_PATTERN.matcher(encodedPassword);
if (!matcher.matches()) {
throw new IllegalArgumentException("Encoded password does not look like BCrypt: " + encodedPassword);
}

View File

@@ -53,7 +53,7 @@ public class BouncyCastleAesCbcBytesEncryptor extends BouncyCastleAesBytesEncryp
@SuppressWarnings("deprecation")
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
blockCipher.init(true, new ParametersWithIV(secretKey, iv));
blockCipher.init(true, new ParametersWithIV(this.secretKey, iv));
byte[] encrypted = process(blockCipher, bytes);
return iv != null ? concatenate(iv, encrypted) : encrypted;
}
@@ -66,7 +66,7 @@ public class BouncyCastleAesCbcBytesEncryptor extends BouncyCastleAesBytesEncryp
@SuppressWarnings("deprecation")
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
blockCipher.init(false, new ParametersWithIV(secretKey, iv));
blockCipher.init(false, new ParametersWithIV(this.secretKey, iv));
return process(blockCipher, encryptedBytes);
}

View File

@@ -50,7 +50,7 @@ public class BouncyCastleAesGcmBytesEncryptor extends BouncyCastleAesBytesEncryp
@SuppressWarnings("deprecation")
GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine());
blockCipher.init(true, new AEADParameters(secretKey, 128, iv, null));
blockCipher.init(true, new AEADParameters(this.secretKey, 128, iv, null));
byte[] encrypted = process(blockCipher, bytes);
return iv != null ? concatenate(iv, encrypted) : encrypted;
@@ -63,7 +63,7 @@ public class BouncyCastleAesGcmBytesEncryptor extends BouncyCastleAesBytesEncryp
@SuppressWarnings("deprecation")
GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine());
blockCipher.init(false, new AEADParameters(secretKey, 128, iv, null));
blockCipher.init(false, new AEADParameters(this.secretKey, 128, iv, null));
return process(blockCipher, encryptedBytes);
}

View File

@@ -34,11 +34,11 @@ final class HexEncodingTextEncryptor implements TextEncryptor {
}
public String encrypt(String text) {
return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text))));
return new String(Hex.encode(this.encryptor.encrypt(Utf8.encode(text))));
}
public String decrypt(String encryptedText) {
return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText)));
return Utf8.decode(this.encryptor.decrypt(Hex.decode(encryptedText)));
}
}

View File

@@ -32,7 +32,7 @@ final class HexEncodingStringKeyGenerator implements StringKeyGenerator {
}
public String generateKey() {
return new String(Hex.encode(keyGenerator.generateKey()));
return new String(Hex.encode(this.keyGenerator.generateKey()));
}
}

View File

@@ -47,12 +47,12 @@ final class SecureRandomBytesKeyGenerator implements BytesKeyGenerator {
}
public int getKeyLength() {
return keyLength;
return this.keyLength;
}
public byte[] generateKey() {
byte[] bytes = new byte[keyLength];
random.nextBytes(bytes);
byte[] bytes = new byte[this.keyLength];
this.random.nextBytes(bytes);
return bytes;
}

View File

@@ -31,11 +31,11 @@ final class SharedKeyGenerator implements BytesKeyGenerator {
}
public int getKeyLength() {
return sharedKey.length;
return this.sharedKey.length;
}
public byte[] generateKey() {
return sharedKey;
return this.sharedKey;
}
}

View File

@@ -46,8 +46,8 @@ final class Digester {
}
public byte[] digest(byte[] value) {
MessageDigest messageDigest = createDigest(algorithm);
for (int i = 0; i < iterations; i++) {
MessageDigest messageDigest = createDigest(this.algorithm);
for (int i = 0; i < this.iterations; i++) {
value = messageDigest.digest(value);
}
return value;

View File

@@ -116,10 +116,10 @@ public class LdapShaPasswordEncoder implements PasswordEncoder {
String prefix;
if (salt == null || salt.length == 0) {
prefix = forceLowerCasePrefix ? SHA_PREFIX_LC : SHA_PREFIX;
prefix = this.forceLowerCasePrefix ? SHA_PREFIX_LC : SHA_PREFIX;
}
else {
prefix = forceLowerCasePrefix ? SSHA_PREFIX_LC : SSHA_PREFIX;
prefix = this.forceLowerCasePrefix ? SSHA_PREFIX_LC : SSHA_PREFIX;
}
return prefix + Utf8.decode(Base64.getEncoder().encode(hash));

View File

@@ -42,12 +42,12 @@ class Md4 {
}
public void reset() {
bufferOffset = 0;
byteCount = 0;
state[0] = 0x67452301;
state[1] = 0xEFCDAB89;
state[2] = 0x98BADCFE;
state[3] = 0x10325476;
this.bufferOffset = 0;
this.byteCount = 0;
this.state[0] = 0x67452301;
this.state[1] = 0xEFCDAB89;
this.state[2] = 0x98BADCFE;
this.state[3] = 0x10325476;
}
public byte[] digest() {
@@ -59,7 +59,7 @@ class Md4 {
private void digest(byte[] buffer, int off) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
buffer[off + (i * 4 + j)] = (byte) (state[i] >>> (8 * j));
buffer[off + (i * 4 + j)] = (byte) (this.state[i] >>> (8 * j));
}
}
}
@@ -80,7 +80,7 @@ class Md4 {
this.buffer[this.bufferOffset++] = (byte) 0x00;
}
long bitCount = byteCount * 8;
long bitCount = this.byteCount * 8;
for (int i = 0; i < 64; i += 8) {
this.buffer[this.bufferOffset++] = (byte) (bitCount >>> (i));
}
@@ -90,7 +90,7 @@ class Md4 {
}
public void update(byte[] input, int offset, int length) {
byteCount += length;
this.byteCount += length;
int todo;
while (length >= (todo = BLOCK_SIZE - this.bufferOffset)) {
System.arraycopy(input, offset, this.buffer, this.bufferOffset, todo);
@@ -101,75 +101,75 @@ class Md4 {
}
System.arraycopy(input, offset, this.buffer, this.bufferOffset, length);
bufferOffset += length;
this.bufferOffset += length;
}
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
this.tmp[i] = (block[offset++] & 0xFF) | (block[offset++] & 0xFF) << 8 | (block[offset++] & 0xFF) << 16
| (block[offset++] & 0xFF) << 24;
}
int A = state[0];
int B = state[1];
int C = state[2];
int D = state[3];
int A = this.state[0];
int B = this.state[1];
int C = this.state[2];
int D = this.state[3];
A = FF(A, B, C, D, tmp[0], 3);
D = FF(D, A, B, C, tmp[1], 7);
C = FF(C, D, A, B, tmp[2], 11);
B = FF(B, C, D, A, tmp[3], 19);
A = FF(A, B, C, D, tmp[4], 3);
D = FF(D, A, B, C, tmp[5], 7);
C = FF(C, D, A, B, tmp[6], 11);
B = FF(B, C, D, A, tmp[7], 19);
A = FF(A, B, C, D, tmp[8], 3);
D = FF(D, A, B, C, tmp[9], 7);
C = FF(C, D, A, B, tmp[10], 11);
B = FF(B, C, D, A, tmp[11], 19);
A = FF(A, B, C, D, tmp[12], 3);
D = FF(D, A, B, C, tmp[13], 7);
C = FF(C, D, A, B, tmp[14], 11);
B = FF(B, C, D, A, tmp[15], 19);
A = FF(A, B, C, D, this.tmp[0], 3);
D = FF(D, A, B, C, this.tmp[1], 7);
C = FF(C, D, A, B, this.tmp[2], 11);
B = FF(B, C, D, A, this.tmp[3], 19);
A = FF(A, B, C, D, this.tmp[4], 3);
D = FF(D, A, B, C, this.tmp[5], 7);
C = FF(C, D, A, B, this.tmp[6], 11);
B = FF(B, C, D, A, this.tmp[7], 19);
A = FF(A, B, C, D, this.tmp[8], 3);
D = FF(D, A, B, C, this.tmp[9], 7);
C = FF(C, D, A, B, this.tmp[10], 11);
B = FF(B, C, D, A, this.tmp[11], 19);
A = FF(A, B, C, D, this.tmp[12], 3);
D = FF(D, A, B, C, this.tmp[13], 7);
C = FF(C, D, A, B, this.tmp[14], 11);
B = FF(B, C, D, A, this.tmp[15], 19);
A = GG(A, B, C, D, tmp[0], 3);
D = GG(D, A, B, C, tmp[4], 5);
C = GG(C, D, A, B, tmp[8], 9);
B = GG(B, C, D, A, tmp[12], 13);
A = GG(A, B, C, D, tmp[1], 3);
D = GG(D, A, B, C, tmp[5], 5);
C = GG(C, D, A, B, tmp[9], 9);
B = GG(B, C, D, A, tmp[13], 13);
A = GG(A, B, C, D, tmp[2], 3);
D = GG(D, A, B, C, tmp[6], 5);
C = GG(C, D, A, B, tmp[10], 9);
B = GG(B, C, D, A, tmp[14], 13);
A = GG(A, B, C, D, tmp[3], 3);
D = GG(D, A, B, C, tmp[7], 5);
C = GG(C, D, A, B, tmp[11], 9);
B = GG(B, C, D, A, tmp[15], 13);
A = GG(A, B, C, D, this.tmp[0], 3);
D = GG(D, A, B, C, this.tmp[4], 5);
C = GG(C, D, A, B, this.tmp[8], 9);
B = GG(B, C, D, A, this.tmp[12], 13);
A = GG(A, B, C, D, this.tmp[1], 3);
D = GG(D, A, B, C, this.tmp[5], 5);
C = GG(C, D, A, B, this.tmp[9], 9);
B = GG(B, C, D, A, this.tmp[13], 13);
A = GG(A, B, C, D, this.tmp[2], 3);
D = GG(D, A, B, C, this.tmp[6], 5);
C = GG(C, D, A, B, this.tmp[10], 9);
B = GG(B, C, D, A, this.tmp[14], 13);
A = GG(A, B, C, D, this.tmp[3], 3);
D = GG(D, A, B, C, this.tmp[7], 5);
C = GG(C, D, A, B, this.tmp[11], 9);
B = GG(B, C, D, A, this.tmp[15], 13);
A = HH(A, B, C, D, tmp[0], 3);
D = HH(D, A, B, C, tmp[8], 9);
C = HH(C, D, A, B, tmp[4], 11);
B = HH(B, C, D, A, tmp[12], 15);
A = HH(A, B, C, D, tmp[2], 3);
D = HH(D, A, B, C, tmp[10], 9);
C = HH(C, D, A, B, tmp[6], 11);
B = HH(B, C, D, A, tmp[14], 15);
A = HH(A, B, C, D, tmp[1], 3);
D = HH(D, A, B, C, tmp[9], 9);
C = HH(C, D, A, B, tmp[5], 11);
B = HH(B, C, D, A, tmp[13], 15);
A = HH(A, B, C, D, tmp[3], 3);
D = HH(D, A, B, C, tmp[11], 9);
C = HH(C, D, A, B, tmp[7], 11);
B = HH(B, C, D, A, tmp[15], 15);
A = HH(A, B, C, D, this.tmp[0], 3);
D = HH(D, A, B, C, this.tmp[8], 9);
C = HH(C, D, A, B, this.tmp[4], 11);
B = HH(B, C, D, A, this.tmp[12], 15);
A = HH(A, B, C, D, this.tmp[2], 3);
D = HH(D, A, B, C, this.tmp[10], 9);
C = HH(C, D, A, B, this.tmp[6], 11);
B = HH(B, C, D, A, this.tmp[14], 15);
A = HH(A, B, C, D, this.tmp[1], 3);
D = HH(D, A, B, C, this.tmp[9], 9);
C = HH(C, D, A, B, this.tmp[5], 11);
B = HH(B, C, D, A, this.tmp[13], 15);
A = HH(A, B, C, D, this.tmp[3], 3);
D = HH(D, A, B, C, this.tmp[11], 9);
C = HH(C, D, A, B, this.tmp[7], 11);
B = HH(B, C, D, A, this.tmp[15], 15);
state[0] += A;
state[1] += B;
state[2] += C;
state[3] += D;
this.state[0] += A;
this.state[1] += B;
this.state[2] += C;
this.state[3] += D;
}
private int FF(int a, int b, int c, int d, int x, int s) {

View File

@@ -74,12 +74,12 @@ public final class StandardPasswordEncoder implements PasswordEncoder {
}
public String encode(CharSequence rawPassword) {
return encode(rawPassword, saltGenerator.generateKey());
return encode(rawPassword, this.saltGenerator.generateKey());
}
public boolean matches(CharSequence rawPassword, String encodedPassword) {
byte[] digested = decode(encodedPassword);
byte[] salt = subArray(digested, 0, saltGenerator.getKeyLength());
byte[] salt = subArray(digested, 0, this.saltGenerator.getKeyLength());
return MessageDigest.isEqual(digested, digest(rawPassword, salt));
}
@@ -97,7 +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 = this.digester.digest(concatenate(salt, this.secret, Utf8.encode(rawPassword)));
return concatenate(salt, digest);
}

View File

@@ -117,12 +117,12 @@ public class SCryptPasswordEncoder implements PasswordEncoder {
}
public String encode(CharSequence rawPassword) {
return digest(rawPassword, saltGenerator.generateKey());
return digest(rawPassword, this.saltGenerator.generateKey());
}
public boolean matches(CharSequence rawPassword, String encodedPassword) {
if (encodedPassword == null || encodedPassword.length() < keyLength) {
logger.warn("Empty encoded password");
if (encodedPassword == null || encodedPassword.length() < this.keyLength) {
this.logger.warn("Empty encoded password");
return false;
}
return decodeAndCheckMatches(rawPassword, encodedPassword);
@@ -165,17 +165,18 @@ public class SCryptPasswordEncoder implements PasswordEncoder {
int parallelization = (int) params & 0xff;
byte[] generated = SCrypt.generate(Utf8.encode(rawPassword), salt, cpuCost, memoryCost, parallelization,
keyLength);
this.keyLength);
return MessageDigest.isEqual(derived, generated);
}
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, this.cpuCost, this.memoryCost,
this.parallelization, this.keyLength);
String params = Long
.toString(((int) (Math.log(cpuCost) / Math.log(2)) << 16L) | memoryCost << 8 | parallelization, 16);
String params = Long.toString(
((int) (Math.log(this.cpuCost) / Math.log(2)) << 16L) | this.memoryCost << 8 | this.parallelization,
16);
StringBuilder sb = new StringBuilder((salt.length + derived.length) * 2);
sb.append("$").append(params).append('$');

View File

@@ -31,38 +31,38 @@ 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 Argon2EncodingUtils.Argon2Hash(this.decoder.decode("cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs"),
(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(
this.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() {
assertArgon2HashEquals(testDataEntry1.decoded, Argon2EncodingUtils.decode(testDataEntry1.encoded));
assertArgon2HashEquals(this.testDataEntry1.decoded, Argon2EncodingUtils.decode(this.testDataEntry1.encoded));
}
@Test
public void decodeWhenValidEncodedHashWithIDThenDecodeCorrectly() {
assertArgon2HashEquals(testDataEntry2.decoded, Argon2EncodingUtils.decode(testDataEntry2.encoded));
assertArgon2HashEquals(this.testDataEntry2.decoded, Argon2EncodingUtils.decode(this.testDataEntry2.encoded));
}
@Test
public void encodeWhenValidArgumentsWithIThenEncodeToCorrectHash() {
assertThat(Argon2EncodingUtils.encode(testDataEntry1.decoded.getHash(), testDataEntry1.decoded.getParameters()))
.isEqualTo(testDataEntry1.encoded);
assertThat(Argon2EncodingUtils.encode(this.testDataEntry1.decoded.getHash(),
this.testDataEntry1.decoded.getParameters())).isEqualTo(this.testDataEntry1.encoded);
}
@Test
public void encodeWhenValidArgumentsWithID2ThenEncodeToCorrectHash() {
assertThat(Argon2EncodingUtils.encode(testDataEntry2.decoded.getHash(), testDataEntry2.decoded.getParameters()))
.isEqualTo(testDataEntry2.encoded);
assertThat(Argon2EncodingUtils.encode(this.testDataEntry2.decoded.getHash(),
this.testDataEntry2.decoded.getParameters())).isEqualTo(this.testDataEntry2.encoded);
}
@Test(expected = IllegalArgumentException.class)

View File

@@ -41,47 +41,47 @@ public class Argon2PasswordEncoderTests {
@Test
public void encodeDoesNotEqualPassword() {
String result = encoder.encode("password");
String result = this.encoder.encode("password");
assertThat(result).isNotEqualTo("password");
}
@Test
public void encodeWhenEqualPasswordThenMatches() {
String result = encoder.encode("password");
assertThat(encoder.matches("password", result)).isTrue();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("password", result)).isTrue();
}
@Test
public void encodeWhenEqualWithUnicodeThenMatches() {
String result = encoder.encode("passw\u9292rd");
assertThat(encoder.matches("pass\u9292\u9292rd", result)).isFalse();
assertThat(encoder.matches("passw\u9292rd", result)).isTrue();
String result = this.encoder.encode("passw\u9292rd");
assertThat(this.encoder.matches("pass\u9292\u9292rd", result)).isFalse();
assertThat(this.encoder.matches("passw\u9292rd", result)).isTrue();
}
@Test
public void encodeWhenNotEqualThenNotMatches() {
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("bogus", result)).isFalse();
}
@Test
public void encodeWhenEqualPasswordWithCustomParamsThenMatches() {
encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
String result = encoder.encode("password");
assertThat(encoder.matches("password", result)).isTrue();
this.encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("password", result)).isTrue();
}
@Test
public void encodeWhenRanTwiceThenResultsNotEqual() {
String password = "secret";
assertThat(encoder.encode(password)).isNotEqualTo(encoder.encode(password));
assertThat(this.encoder.encode(password)).isNotEqualTo(this.encoder.encode(password));
}
@Test
public void encodeWhenRanTwiceWithCustomParamsThenNotEquals() {
encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
this.encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
String password = "secret";
assertThat(encoder.encode(password)).isNotEqualTo(encoder.encode(password));
assertThat(this.encoder.encode(password)).isNotEqualTo(this.encoder.encode(password));
}
@Test
@@ -96,24 +96,24 @@ public class Argon2PasswordEncoderTests {
@Test
public void matchesWhenEncodedPassIsNullThenFalse() {
assertThat(encoder.matches("password", null)).isFalse();
assertThat(this.encoder.matches("password", null)).isFalse();
}
@Test
public void matchesWhenEncodedPassIsEmptyThenFalse() {
assertThat(encoder.matches("password", "")).isFalse();
assertThat(this.encoder.matches("password", "")).isFalse();
}
@Test
public void matchesWhenEncodedPassIsBogusThenFalse() {
assertThat(encoder.matches("password", "012345678901234567890123456789")).isFalse();
assertThat(this.encoder.matches("password", "012345678901234567890123456789")).isFalse();
}
@Test
public void encodeWhenUsingPredictableSaltThenEqualTestHash() throws Exception {
injectPredictableSaltGen();
String hash = encoder.encode("sometestpassword");
String hash = this.encoder.encode("sometestpassword");
assertThat(hash).isEqualTo(
"$argon2id$v=19$m=4096,t=3,p=1$QUFBQUFBQUFBQUFBQUFBQQ$hmmTNyJlwbb6HAvFoHFWF+u03fdb0F2qA+39oPlcAqo");
@@ -121,9 +121,9 @@ public class Argon2PasswordEncoderTests {
@Test
public void encodeWhenUsingPredictableSaltWithCustomParamsThenEqualTestHash() throws Exception {
encoder = new Argon2PasswordEncoder(16, 32, 4, 512, 5);
this.encoder = new Argon2PasswordEncoder(16, 32, 4, 512, 5);
injectPredictableSaltGen();
String hash = encoder.encode("sometestpassword");
String hash = this.encoder.encode("sometestpassword");
assertThat(hash).isEqualTo(
"$argon2id$v=19$m=512,t=5,p=4$QUFBQUFBQUFBQUFBQUFBQQ$PNv4C3K50bz3rmON+LtFpdisD7ePieLNq+l5iUHgc1k");
@@ -131,16 +131,16 @@ public class Argon2PasswordEncoderTests {
@Test
public void upgradeEncodingWhenSameEncodingThenFalse() {
String hash = encoder.encode("password");
String hash = this.encoder.encode("password");
assertThat(encoder.upgradeEncoding(hash)).isFalse();
assertThat(this.encoder.upgradeEncoding(hash)).isFalse();
}
@Test
public void upgradeEncodingWhenSameStandardParamsThenFalse() {
Argon2PasswordEncoder newEncoder = new Argon2PasswordEncoder();
String hash = encoder.encode("password");
String hash = this.encoder.encode("password");
assertThat(newEncoder.upgradeEncoding(hash)).isFalse();
}
@@ -187,30 +187,30 @@ public class Argon2PasswordEncoderTests {
@Test
public void upgradeEncodingWhenEncodedPassIsNullThenFalse() {
assertThat(encoder.upgradeEncoding(null)).isFalse();
assertThat(this.encoder.upgradeEncoding(null)).isFalse();
}
@Test
public void upgradeEncodingWhenEncodedPassIsEmptyThenFalse() {
assertThat(encoder.upgradeEncoding("")).isFalse();
assertThat(this.encoder.upgradeEncoding("")).isFalse();
}
@Test(expected = IllegalArgumentException.class)
public void upgradeEncodingWhenEncodedPassIsBogusThenThrowException() {
encoder.upgradeEncoding("thisIsNoValidHash");
this.encoder.upgradeEncoding("thisIsNoValidHash");
}
private void injectPredictableSaltGen() throws Exception {
byte[] bytes = new byte[16];
Arrays.fill(bytes, (byte) 0x41);
Mockito.when(keyGeneratorMock.generateKey()).thenReturn(bytes);
Mockito.when(this.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
Field saltGen = encoder.getClass().getDeclaredField("saltGenerator");
Field saltGen = this.encoder.getClass().getDeclaredField("saltGenerator");
saltGen.setAccessible(true);
saltGen.set(encoder, keyGeneratorMock);
saltGen.set(this.encoder, this.keyGeneratorMock);
saltGen.setAccessible(false);
}

View File

@@ -54,29 +54,29 @@ public class HexTests {
@Test
public void decodeNotEven() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Hex-encoded string must have an even number of characters");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Hex-encoded string must have an even number of characters");
Hex.decode("414243444");
}
@Test
public void decodeExistNonHexCharAtFirst() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Detected a Non-hex character at 1 or 2 position");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Detected a Non-hex character at 1 or 2 position");
Hex.decode("G0");
}
@Test
public void decodeExistNonHexCharAtSecond() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Detected a Non-hex character at 3 or 4 position");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Detected a Non-hex character at 3 or 4 position");
Hex.decode("410G");
}
@Test
public void decodeExistNonHexCharAtBoth() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Detected a Non-hex character at 5 or 6 position");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Detected a Non-hex character at 5 or 6 position");
Hex.decode("4142GG");
}

View File

@@ -41,64 +41,65 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
@Before
public void setup() {
// generate random password, salt, and test data
password = UUID.randomUUID().toString();
this.password = UUID.randomUUID().toString();
/** insecure salt byte, recommend 64 or larger than 64 */
byte[] saltBytes = new byte[16];
secureRandom.nextBytes(saltBytes);
salt = new String(Hex.encode(saltBytes));
this.secureRandom.nextBytes(saltBytes);
this.salt = new String(Hex.encode(saltBytes));
}
@Test
public void bouncyCastleAesCbcWithPredictableIvEquvalent() throws Exception {
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16));
testEquivalence(bcEncryptor, jceEncryptor);
}
@Test
public void bouncyCastleAesCbcWithSecureIvCompatible() throws Exception {
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(16));
testCompatibility(bcEncryptor, jceEncryptor);
}
@Test
public void bouncyCastleAesGcmWithPredictableIvEquvalent() throws Exception {
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16),
CipherAlgorithm.GCM);
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16), CipherAlgorithm.GCM);
testEquivalence(bcEncryptor, jceEncryptor);
}
@Test
public void bouncyCastleAesGcmWithSecureIvCompatible() throws Exception {
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16),
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(16),
CipherAlgorithm.GCM);
testCompatibility(bcEncryptor, jceEncryptor);
}
private void testEquivalence(BytesEncryptor left, BytesEncryptor right) {
for (int size = 1; size < 2048; size++) {
testData = new byte[size];
secureRandom.nextBytes(testData);
this.testData = new byte[size];
this.secureRandom.nextBytes(this.testData);
// tests that right and left generate the same encrypted bytes
// and can decrypt back to the original input
byte[] leftEncrypted = left.encrypt(testData);
byte[] rightEncrypted = right.encrypt(testData);
byte[] leftEncrypted = left.encrypt(this.testData);
byte[] rightEncrypted = right.encrypt(this.testData);
Assert.assertArrayEquals(leftEncrypted, rightEncrypted);
byte[] leftDecrypted = left.decrypt(leftEncrypted);
byte[] rightDecrypted = right.decrypt(rightEncrypted);
Assert.assertArrayEquals(testData, leftDecrypted);
Assert.assertArrayEquals(testData, rightDecrypted);
Assert.assertArrayEquals(this.testData, leftDecrypted);
Assert.assertArrayEquals(this.testData, rightDecrypted);
}
}
@@ -107,14 +108,14 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
// tests that right can decrypt what left encrypted and vice versa
// and that the decypted data is the same as the original
for (int size = 1; size < 2048; size++) {
testData = new byte[size];
secureRandom.nextBytes(testData);
byte[] leftEncrypted = left.encrypt(testData);
byte[] rightEncrypted = right.encrypt(testData);
this.testData = new byte[size];
this.secureRandom.nextBytes(this.testData);
byte[] leftEncrypted = left.encrypt(this.testData);
byte[] rightEncrypted = right.encrypt(this.testData);
byte[] leftDecrypted = left.decrypt(rightEncrypted);
byte[] rightDecrypted = right.decrypt(leftEncrypted);
Assert.assertArrayEquals(testData, leftDecrypted);
Assert.assertArrayEquals(testData, rightDecrypted);
Assert.assertArrayEquals(this.testData, leftDecrypted);
Assert.assertArrayEquals(this.testData, rightDecrypted);
}
}
@@ -133,12 +134,12 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
}
public int getKeyLength() {
return keyLength;
return this.keyLength;
}
public byte[] generateKey() {
byte[] bytes = new byte[keyLength];
random.nextBytes(bytes);
byte[] bytes = new byte[this.keyLength];
this.random.nextBytes(bytes);
return bytes;
}

View File

@@ -38,44 +38,44 @@ public class BouncyCastleAesBytesEncryptorTests {
public void setup() {
// generate random password, salt, and test data
SecureRandom secureRandom = new SecureRandom();
password = UUID.randomUUID().toString();
this.password = UUID.randomUUID().toString();
byte[] saltBytes = new byte[16];
secureRandom.nextBytes(saltBytes);
salt = new String(Hex.encode(saltBytes));
testData = new byte[1024 * 1024];
secureRandom.nextBytes(testData);
this.salt = new String(Hex.encode(saltBytes));
this.testData = new byte[1024 * 1024];
secureRandom.nextBytes(this.testData);
}
@Test
public void bcCbcWithSecureIvGeneratesDifferentMessages() {
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt);
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt);
generatesDifferentCipherTexts(bcEncryptor);
}
@Test
public void bcGcmWithSecureIvGeneratesDifferentMessages() {
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt);
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt);
generatesDifferentCipherTexts(bcEncryptor);
}
private void generatesDifferentCipherTexts(BytesEncryptor bcEncryptor) {
byte[] encrypted1 = bcEncryptor.encrypt(testData);
byte[] encrypted2 = bcEncryptor.encrypt(testData);
byte[] encrypted1 = bcEncryptor.encrypt(this.testData);
byte[] encrypted2 = bcEncryptor.encrypt(this.testData);
Assert.assertFalse(Arrays.areEqual(encrypted1, encrypted2));
byte[] decrypted1 = bcEncryptor.decrypt(encrypted1);
byte[] decrypted2 = bcEncryptor.decrypt(encrypted2);
Assert.assertArrayEquals(testData, decrypted1);
Assert.assertArrayEquals(testData, decrypted2);
Assert.assertArrayEquals(this.testData, decrypted1);
Assert.assertArrayEquals(this.testData, decrypted2);
}
@Test(expected = IllegalArgumentException.class)
public void bcCbcWithWrongLengthIv() {
new BouncyCastleAesCbcBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(8));
}
@Test(expected = IllegalArgumentException.class)
public void bcGcmWithWrongLengthIv() {
new BouncyCastleAesGcmBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(8));
}
}

View File

@@ -220,7 +220,7 @@ public class DelegatingPasswordEncoderTests {
public void upgradeEncodingWhenSameIdAndEncoderFalseThenEncoderDecidesFalse() {
assertThat(this.passwordEncoder.upgradeEncoding(this.bcryptEncodedPassword)).isFalse();
verify(bcrypt).upgradeEncoding(this.encodedPassword);
verify(this.bcrypt).upgradeEncoding(this.encodedPassword);
}
@Test
@@ -229,14 +229,14 @@ public class DelegatingPasswordEncoderTests {
assertThat(this.passwordEncoder.upgradeEncoding(this.bcryptEncodedPassword)).isTrue();
verify(bcrypt).upgradeEncoding(this.encodedPassword);
verify(this.bcrypt).upgradeEncoding(this.encodedPassword);
}
@Test
public void upgradeEncodingWhenDifferentIdThenTrue() {
assertThat(this.passwordEncoder.upgradeEncoding(this.noopEncodedPassword)).isTrue();
verifyZeroInteractions(bcrypt);
verifyZeroInteractions(this.bcrypt);
}
}

View File

@@ -26,21 +26,21 @@ public class StandardPasswordEncoderTests {
@Test
public void matches() {
String result = encoder.encode("password");
String result = this.encoder.encode("password");
assertThat(result).isNotEqualTo("password");
assertThat(encoder.matches("password", result)).isTrue();
assertThat(this.encoder.matches("password", result)).isTrue();
}
@Test
public void matchesLengthChecked() {
String result = encoder.encode("password");
assertThat(encoder.matches("password", result.substring(0, result.length() - 2))).isFalse();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("password", result.substring(0, result.length() - 2))).isFalse();
}
@Test
public void notMatches() {
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("bogus", result)).isFalse();
}
}