fix string concatenates performance
This commit is contained in:
@@ -10,7 +10,6 @@ import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author stefano
|
||||
*/
|
||||
public class PHPBB implements EncryptionMethod {
|
||||
@@ -20,19 +19,16 @@ public class PHPBB implements EncryptionMethod {
|
||||
|
||||
public String phpbb_hash(String password, String salt) {
|
||||
String random_state = salt;
|
||||
String random = "";
|
||||
StringBuilder random = new StringBuilder();
|
||||
int count = 6;
|
||||
if (random.length() < count) {
|
||||
random = "";
|
||||
for (int i = 0; i < count; i += 16) {
|
||||
random_state = md5(salt + random_state);
|
||||
random += pack(md5(random_state));
|
||||
}
|
||||
random = random.substring(0, count);
|
||||
for (int i = 0; i < count; i += 16) {
|
||||
random_state = md5(salt + random_state);
|
||||
random.append(pack(md5(random_state)));
|
||||
}
|
||||
String hash = _hash_crypt_private(password, _hash_gensalt_private(random, itoa64));
|
||||
if (hash.length() == 34)
|
||||
String hash = _hash_crypt_private(password, _hash_gensalt_private(random.substring(0, count), itoa64));
|
||||
if (hash.length() == 34) {
|
||||
return hash;
|
||||
}
|
||||
return md5(password);
|
||||
}
|
||||
|
||||
@@ -40,9 +36,8 @@ public class PHPBB implements EncryptionMethod {
|
||||
return _hash_gensalt_private(input, itoa64, 6);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private String _hash_gensalt_private(String input, String itoa64,
|
||||
int iteration_count_log2) {
|
||||
int iteration_count_log2) {
|
||||
if (iteration_count_log2 < 4 || iteration_count_log2 > 31) {
|
||||
iteration_count_log2 = 8;
|
||||
}
|
||||
@@ -109,9 +104,7 @@ public class PHPBB implements EncryptionMethod {
|
||||
MessageDigest md5er = MessageDigest.getInstance("MD5");
|
||||
byte[] hash = md5er.digest(bytes);
|
||||
return bytes2hex(hash);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
} catch (GeneralSecurityException | UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -126,9 +119,9 @@ public class PHPBB implements EncryptionMethod {
|
||||
}
|
||||
|
||||
private static String bytes2hex(byte[] bytes) {
|
||||
StringBuffer r = new StringBuffer(32);
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
String x = Integer.toHexString(bytes[i] & 0xff);
|
||||
StringBuilder r = new StringBuilder(32);
|
||||
for (byte b : bytes) {
|
||||
String x = Integer.toHexString(b & 0xff);
|
||||
if (x.length() < 2)
|
||||
r.append("0");
|
||||
r.append(x);
|
||||
@@ -137,7 +130,7 @@ public class PHPBB implements EncryptionMethod {
|
||||
}
|
||||
|
||||
static String pack(String hex) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (int i = 0; i < hex.length(); i += 2) {
|
||||
char c1 = hex.charAt(i);
|
||||
char c2 = hex.charAt(i + 1);
|
||||
@@ -155,7 +148,7 @@ public class PHPBB implements EncryptionMethod {
|
||||
|
||||
@Override
|
||||
public boolean comparePassword(String hash, String password,
|
||||
String playerName) throws NoSuchAlgorithmException {
|
||||
String playerName) throws NoSuchAlgorithmException {
|
||||
return phpbb_check_hash(password, hash);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,12 @@ import java.util.Arrays;
|
||||
|
||||
public class WORDPRESS implements EncryptionMethod {
|
||||
|
||||
private static String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
private int iterationCountLog2 = 8;
|
||||
private static final String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
private SecureRandom randomGen = new SecureRandom();
|
||||
|
||||
private String encode64(byte[] src, int count) {
|
||||
int i, value;
|
||||
String output = "";
|
||||
StringBuilder output = new StringBuilder();
|
||||
i = 0;
|
||||
|
||||
if (src.length < count) {
|
||||
@@ -26,24 +25,24 @@ public class WORDPRESS implements EncryptionMethod {
|
||||
do {
|
||||
value = src[i] + (src[i] < 0 ? 256 : 0);
|
||||
++i;
|
||||
output += itoa64.charAt(value & 63);
|
||||
output.append(itoa64.charAt(value & 63));
|
||||
if (i < count) {
|
||||
value |= (src[i] + (src[i] < 0 ? 256 : 0)) << 8;
|
||||
}
|
||||
output += itoa64.charAt((value >> 6) & 63);
|
||||
output.append(itoa64.charAt((value >> 6) & 63));
|
||||
if (i++ >= count) {
|
||||
break;
|
||||
}
|
||||
if (i < count) {
|
||||
value |= (src[i] + (src[i] < 0 ? 256 : 0)) << 16;
|
||||
}
|
||||
output += itoa64.charAt((value >> 12) & 63);
|
||||
output.append(itoa64.charAt((value >> 12) & 63));
|
||||
if (i++ >= count) {
|
||||
break;
|
||||
}
|
||||
output += itoa64.charAt((value >> 18) & 63);
|
||||
output.append(itoa64.charAt((value >> 18) & 63));
|
||||
} while (i < count);
|
||||
return output;
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
private String crypt(String password, String setting) {
|
||||
@@ -86,7 +85,8 @@ public class WORDPRESS implements EncryptionMethod {
|
||||
|
||||
private String gensaltPrivate(byte[] input) {
|
||||
String output = "$P$";
|
||||
output += itoa64.charAt(Math.min(this.iterationCountLog2 + 5, 30));
|
||||
int iterationCountLog2 = 8;
|
||||
output += itoa64.charAt(Math.min(iterationCountLog2 + 5, 30));
|
||||
output += encode64(input, 6);
|
||||
return output;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user