Skip to content

Commit 53e6dca

Browse files
daiaziusbrianchandotcom
authored andcommitted
LPD-43475 Implement the new method in all encryptors
1 parent 8199c56 commit 53e6dca

File tree

7 files changed

+84
-0
lines changed

7 files changed

+84
-0
lines changed

modules/apps/portal-security/portal-security-password-encryptor-impl/src/main/java/com/liferay/portal/security/password/encryptor/internal/BCryptPasswordEncryptor.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package com.liferay.portal.security.password.encryptor.internal;
77

8+
import com.liferay.petra.string.CharPool;
9+
import com.liferay.petra.string.StringBundler;
810
import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
911
import com.liferay.portal.kernel.util.GetterUtil;
1012
import com.liferay.portal.kernel.util.Validator;
@@ -55,8 +57,26 @@ public String encrypt(
5557
return BCrypt.hashpw(plainTextPassword, salt);
5658
}
5759

60+
@Override
61+
public String getFullAlgorithmConfiguration(String encryptedPassword) {
62+
String rounds = String.valueOf(_ROUNDS);
63+
64+
Matcher matcher = _encryptedPasswordPattern.matcher(encryptedPassword);
65+
66+
if (matcher.find()) {
67+
rounds = matcher.group(1);
68+
}
69+
70+
String algorithm = encryptedPassword.substring(
71+
1, encryptedPassword.indexOf(CharPool.CLOSE_CURLY_BRACE));
72+
73+
return StringBundler.concat(algorithm, CharPool.FORWARD_SLASH, rounds);
74+
}
75+
5876
private static final int _ROUNDS = 10;
5977

78+
private static final Pattern _encryptedPasswordPattern = Pattern.compile(
79+
"\\{BCrypt}\\$2a\\$(\\d+)\\$", Pattern.CASE_INSENSITIVE);
6080
private static final Pattern _pattern = Pattern.compile(
6181
"^BCrypt/([0-9]+)$", Pattern.CASE_INSENSITIVE);
6282

modules/apps/portal-security/portal-security-password-encryptor-impl/src/main/java/com/liferay/portal/security/password/encryptor/internal/CryptPasswordEncryptor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.liferay.portal.security.password.encryptor.internal;
77

8+
import com.liferay.petra.string.CharPool;
89
import com.liferay.portal.kernel.exception.PwdEncryptorException;
910
import com.liferay.portal.kernel.security.SecureRandom;
1011
import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
@@ -53,6 +54,12 @@ public String encrypt(
5354
}
5455
}
5556

57+
@Override
58+
public String getFullAlgorithmConfiguration(String encryptedPassword) {
59+
return encryptedPassword.substring(
60+
1, encryptedPassword.indexOf(CharPool.CLOSE_CURLY_BRACE));
61+
}
62+
5663
protected byte[] getSalt(String encryptedPassword)
5764
throws PwdEncryptorException {
5865

modules/apps/portal-security/portal-security-password-encryptor-impl/src/main/java/com/liferay/portal/security/password/encryptor/internal/DefaultPasswordEncryptor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.liferay.portal.security.password.encryptor.internal;
77

8+
import com.liferay.petra.string.CharPool;
89
import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
910
import com.liferay.portal.kernel.util.DigesterUtil;
1011

@@ -28,4 +29,10 @@ public String encrypt(
2829
return DigesterUtil.digest(algorithm, plainTextPassword);
2930
}
3031

32+
@Override
33+
public String getFullAlgorithmConfiguration(String encryptedPassword) {
34+
return encryptedPassword.substring(
35+
1, encryptedPassword.indexOf(CharPool.CLOSE_CURLY_BRACE));
36+
}
37+
3138
}

modules/apps/portal-security/portal-security-password-encryptor-impl/src/main/java/com/liferay/portal/security/password/encryptor/internal/NullPasswordEncryptor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.liferay.portal.security.password.encryptor.internal;
77

8+
import com.liferay.petra.string.CharPool;
89
import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
910

1011
import org.osgi.service.component.annotations.Component;
@@ -27,4 +28,10 @@ public String encrypt(
2728
return plainTextPassword;
2829
}
2930

31+
@Override
32+
public String getFullAlgorithmConfiguration(String encryptedPassword) {
33+
return encryptedPassword.substring(
34+
1, encryptedPassword.indexOf(CharPool.CLOSE_CURLY_BRACE));
35+
}
36+
3037
}

modules/apps/portal-security/portal-security-password-encryptor-impl/src/main/java/com/liferay/portal/security/password/encryptor/internal/PBKDF2PasswordEncryptor.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
package com.liferay.portal.security.password.encryptor.internal;
77

8+
import com.liferay.petra.string.CharPool;
9+
import com.liferay.petra.string.StringBundler;
810
import com.liferay.petra.string.StringPool;
911
import com.liferay.portal.kernel.exception.PwdEncryptorException;
1012
import com.liferay.portal.kernel.io.BigEndianCodec;
13+
import com.liferay.portal.kernel.log.Log;
14+
import com.liferay.portal.kernel.log.LogFactoryUtil;
1115
import com.liferay.portal.kernel.security.SecureRandomUtil;
1216
import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
1317
import com.liferay.portal.kernel.util.Base64;
@@ -89,12 +93,39 @@ public String encrypt(
8993
}
9094
}
9195

96+
@Override
97+
public String getFullAlgorithmConfiguration(String encryptedPassword) {
98+
PBKDF2EncryptionConfiguration pbkdf2EncryptionConfiguration =
99+
new PBKDF2EncryptionConfiguration();
100+
101+
int index = encryptedPassword.indexOf(CharPool.CLOSE_CURLY_BRACE);
102+
103+
try {
104+
pbkdf2EncryptionConfiguration.configure(
105+
StringPool.BLANK, encryptedPassword.substring(index + 1));
106+
}
107+
catch (Exception exception) {
108+
_log.error(exception);
109+
}
110+
111+
String algorithm = encryptedPassword.substring(1, index);
112+
113+
return StringBundler.concat(
114+
algorithm, StringPool.FORWARD_SLASH,
115+
pbkdf2EncryptionConfiguration.getKeySize(),
116+
StringPool.FORWARD_SLASH,
117+
pbkdf2EncryptionConfiguration.getRounds());
118+
}
119+
92120
private static final int _KEY_SIZE = 160;
93121

94122
private static final int _ROUNDS = 1300000;
95123

96124
private static final int _SALT_BYTES_LENGTH = 16;
97125

126+
private static final Log _log = LogFactoryUtil.getLog(
127+
PBKDF2PasswordEncryptor.class);
128+
98129
private static final Pattern _pattern = Pattern.compile(
99130
"^.*/?([0-9]+)?/([0-9]+)$");
100131

modules/apps/portal-security/portal-security-password-encryptor-impl/src/main/java/com/liferay/portal/security/password/encryptor/internal/SSHAPasswordEncryptor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.liferay.portal.security.password.encryptor.internal;
77

8+
import com.liferay.petra.string.CharPool;
89
import com.liferay.portal.kernel.exception.PwdEncryptorException;
910
import com.liferay.portal.kernel.io.BigEndianCodec;
1011
import com.liferay.portal.kernel.security.SecureRandomUtil;
@@ -67,6 +68,12 @@ public String encrypt(
6768
}
6869
}
6970

71+
@Override
72+
public String getFullAlgorithmConfiguration(String encryptedPassword) {
73+
return encryptedPassword.substring(
74+
1, encryptedPassword.indexOf(CharPool.CLOSE_CURLY_BRACE));
75+
}
76+
7077
protected byte[] getSaltBytes(String encryptedPassword)
7178
throws PwdEncryptorException {
7279

modules/apps/portal-security/portal-security-password-encryptor-impl/src/test/java/com/liferay/portal/security/password/encryptor/internal/PasswordEncryptorUtilTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ public String encrypt(
344344
algorithm.substring(algorithm.indexOf('/') + 1);
345345
}
346346

347+
@Override
348+
public String getFullAlgorithmConfiguration(String encryptedPassword) {
349+
return null;
350+
}
351+
347352
}
348353

349354
}

0 commit comments

Comments
 (0)