Code Refactor - Whitespace Refactor

This commit is contained in:
Xephi
2014-08-08 23:14:56 +02:00
parent ec7ac60340
commit afc1ea9111
114 changed files with 8277 additions and 7103 deletions
@@ -15,7 +15,6 @@ import fr.xephi.authme.security.crypts.BCRYPT;
import fr.xephi.authme.security.crypts.EncryptionMethod;
import fr.xephi.authme.settings.Settings;
public class PasswordSecurity {
private static SecureRandom rnd = new SecureRandom();
@@ -27,145 +26,157 @@ public class PasswordSecurity {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.reset();
byte[] digest = sha1.digest(msg);
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest)).substring(0, length);
return String.format("%0" + (digest.length << 1) + "x",
new BigInteger(1, digest)).substring(0, length);
}
public static String getHash(HashAlgorithm alg, String password, String playerName) throws NoSuchAlgorithmException {
EncryptionMethod method;
try {
if (alg != HashAlgorithm.CUSTOM)
method = (EncryptionMethod) alg.getclass().newInstance();
else method = null;
} catch (InstantiationException e) {
throw new NoSuchAlgorithmException("Problem with this hash algorithm");
} catch (IllegalAccessException e) {
throw new NoSuchAlgorithmException("Problem with this hash algorithm");
}
String salt = "";
switch (alg) {
case SHA256:
salt = createSalt(16);
break;
case MD5VB:
salt = createSalt(16);
break;
case XAUTH:
salt = createSalt(12);
break;
case MYBB:
salt = createSalt(8);
userSalt.put(playerName, salt);
break;
case IPB3:
salt = createSalt(5);
userSalt.put(playerName, salt);
break;
case PHPFUSION:
salt = createSalt(12);
userSalt.put(playerName, salt);
break;
case SALTED2MD5:
salt = createSalt(Settings.saltLength);
userSalt.put(playerName, salt);
break;
case JOOMLA:
salt = createSalt(32);
userSalt.put(playerName, salt);
break;
case BCRYPT:
salt = BCRYPT.gensalt(Settings.bCryptLog2Rounds);
userSalt.put(playerName, salt);
break;
case WBB3:
salt = createSalt(40);
userSalt.put(playerName, salt);
break;
case WBB4:
salt = BCRYPT.gensalt(8);
userSalt.put(playerName, salt);
break;
case PBKDF2:
salt = createSalt(12);
userSalt.put(playerName, salt);
break;
case SMF:
return method.getHash(password, null, playerName);
case PHPBB:
salt = createSalt(16);
userSalt.put(playerName, salt);
break;
case MD5:
case SHA1:
case WHIRLPOOL:
case PLAINTEXT:
case XENFORO:
case SHA512:
case ROYALAUTH:
case CRAZYCRYPT1:
case DOUBLEMD5:
case WORDPRESS:
case CUSTOM:
break;
default:
throw new NoSuchAlgorithmException("Unknown hash algorithm");
public static String getHash(HashAlgorithm alg, String password,
String playerName) throws NoSuchAlgorithmException {
EncryptionMethod method;
try {
if (alg != HashAlgorithm.CUSTOM) method = (EncryptionMethod) alg
.getclass().newInstance();
else method = null;
} catch (InstantiationException e) {
throw new NoSuchAlgorithmException(
"Problem with this hash algorithm");
} catch (IllegalAccessException e) {
throw new NoSuchAlgorithmException(
"Problem with this hash algorithm");
}
PasswordEncryptionEvent event = new PasswordEncryptionEvent(method, playerName);
String salt = "";
switch (alg) {
case SHA256:
salt = createSalt(16);
break;
case MD5VB:
salt = createSalt(16);
break;
case XAUTH:
salt = createSalt(12);
break;
case MYBB:
salt = createSalt(8);
userSalt.put(playerName, salt);
break;
case IPB3:
salt = createSalt(5);
userSalt.put(playerName, salt);
break;
case PHPFUSION:
salt = createSalt(12);
userSalt.put(playerName, salt);
break;
case SALTED2MD5:
salt = createSalt(Settings.saltLength);
userSalt.put(playerName, salt);
break;
case JOOMLA:
salt = createSalt(32);
userSalt.put(playerName, salt);
break;
case BCRYPT:
salt = BCRYPT.gensalt(Settings.bCryptLog2Rounds);
userSalt.put(playerName, salt);
break;
case WBB3:
salt = createSalt(40);
userSalt.put(playerName, salt);
break;
case WBB4:
salt = BCRYPT.gensalt(8);
userSalt.put(playerName, salt);
break;
case PBKDF2:
salt = createSalt(12);
userSalt.put(playerName, salt);
break;
case SMF:
return method.getHash(password, null, playerName);
case PHPBB:
salt = createSalt(16);
userSalt.put(playerName, salt);
break;
case MD5:
case SHA1:
case WHIRLPOOL:
case PLAINTEXT:
case XENFORO:
case SHA512:
case ROYALAUTH:
case CRAZYCRYPT1:
case DOUBLEMD5:
case WORDPRESS:
case CUSTOM:
break;
default:
throw new NoSuchAlgorithmException("Unknown hash algorithm");
}
PasswordEncryptionEvent event = new PasswordEncryptionEvent(method,
playerName);
Bukkit.getPluginManager().callEvent(event);
method = event.getMethod();
if (method == null)
throw new NoSuchAlgorithmException("Unknown hash algorithm");
if (method == null) throw new NoSuchAlgorithmException(
"Unknown hash algorithm");
return method.getHash(password, salt, playerName);
}
public static boolean comparePasswordWithHash(String password, String hash, String playerName) throws NoSuchAlgorithmException {
HashAlgorithm algo = Settings.getPasswordHash;
EncryptionMethod method;
try {
if (algo != HashAlgorithm.CUSTOM)
method = (EncryptionMethod) algo.getclass().newInstance();
else method = null;
} catch (InstantiationException e) {
throw new NoSuchAlgorithmException("Problem with this hash algorithm");
} catch (IllegalAccessException e) {
throw new NoSuchAlgorithmException("Problem with this hash algorithm");
}
PasswordEncryptionEvent event = new PasswordEncryptionEvent(method, playerName);
public static boolean comparePasswordWithHash(String password, String hash,
String playerName) throws NoSuchAlgorithmException {
HashAlgorithm algo = Settings.getPasswordHash;
EncryptionMethod method;
try {
if (algo != HashAlgorithm.CUSTOM) method = (EncryptionMethod) algo
.getclass().newInstance();
else method = null;
} catch (InstantiationException e) {
throw new NoSuchAlgorithmException(
"Problem with this hash algorithm");
} catch (IllegalAccessException e) {
throw new NoSuchAlgorithmException(
"Problem with this hash algorithm");
}
PasswordEncryptionEvent event = new PasswordEncryptionEvent(method,
playerName);
Bukkit.getPluginManager().callEvent(event);
method = event.getMethod();
if (method == null)
throw new NoSuchAlgorithmException("Unknown hash algorithm");
if (method == null) throw new NoSuchAlgorithmException(
"Unknown hash algorithm");
try {
if (method.comparePassword(hash, password, playerName))
return true;
if (method.comparePassword(hash, password, playerName)) return true;
} catch (Exception e) {
}
if (Settings.supportOldPassword) {
try {
if (compareWithAllEncryptionMethod(password, hash, playerName))
return true;
} catch (Exception e) {}
try {
if (compareWithAllEncryptionMethod(password, hash, playerName)) return true;
} catch (Exception e) {
}
}
return false;
}
private static boolean compareWithAllEncryptionMethod(String password, String hash, String playerName) throws NoSuchAlgorithmException {
for (HashAlgorithm algo : HashAlgorithm.values()) {
if (algo != HashAlgorithm.CUSTOM)
try {
EncryptionMethod method = (EncryptionMethod) algo.getclass().newInstance();
if (method.comparePassword(hash, password, playerName)) {
PlayerAuth nAuth = AuthMe.getInstance().database.getAuth(playerName);
if (nAuth != null) {
nAuth.setHash(getHash(Settings.getPasswordHash, password, playerName));
nAuth.setSalt(userSalt.get(playerName));
AuthMe.getInstance().database.updatePassword(nAuth);
AuthMe.getInstance().database.updateSalt(nAuth);
}
return true;
}
} catch (Exception e) {}
}
return false;
private static boolean compareWithAllEncryptionMethod(String password,
String hash, String playerName) throws NoSuchAlgorithmException {
for (HashAlgorithm algo : HashAlgorithm.values()) {
if (algo != HashAlgorithm.CUSTOM) try {
EncryptionMethod method = (EncryptionMethod) algo.getclass()
.newInstance();
if (method.comparePassword(hash, password, playerName)) {
PlayerAuth nAuth = AuthMe.getInstance().database
.getAuth(playerName);
if (nAuth != null) {
nAuth.setHash(getHash(Settings.getPasswordHash,
password, playerName));
nAuth.setSalt(userSalt.get(playerName));
AuthMe.getInstance().database.updatePassword(nAuth);
AuthMe.getInstance().database.updateSalt(nAuth);
}
return true;
}
} catch (Exception e) {
}
}
return false;
}
}
@@ -3,37 +3,34 @@ package fr.xephi.authme.security;
import java.util.Random;
/**
*
* @author Xephi59
*/
public class RandomString
{
*
* @author Xephi59
*/
public class RandomString {
private static final char[] chars = new char[36];
private static final char[] chars = new char[36];
static {
for (int idx = 0; idx < 10; ++idx)
chars[idx] = (char) ('0' + idx);
for (int idx = 10; idx < 36; ++idx)
chars[idx] = (char) ('a' + idx - 10);
}
static {
for (int idx = 0; idx < 10; ++idx)
chars[idx] = (char) ('0' + idx);
for (int idx = 10; idx < 36; ++idx)
chars[idx] = (char) ('a' + idx - 10);
}
private final Random random = new Random();
private final Random random = new Random();
private final char[] buf;
private final char[] buf;
public RandomString(int length)
{
if (length < 1)
throw new IllegalArgumentException("length < 1: " + length);
buf = new char[length];
}
public RandomString(int length) {
if (length < 1) throw new IllegalArgumentException("length < 1: "
+ length);
buf = new char[length];
}
public String nextString()
{
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = chars[random.nextInt(chars.length)];
return new String(buf);
}
public String nextString() {
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = chars[random.nextInt(chars.length)];
return new String(buf);
}
}
File diff suppressed because it is too large Load Diff
@@ -7,12 +7,14 @@ import java.security.NoSuchAlgorithmException;
public class CRAZYCRYPT1 implements EncryptionMethod {
protected final Charset charset = Charset.forName("UTF-8");
private static final char[] CRYPTCHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
private static final char[] CRYPTCHARS = new char[] { '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
final String text = "ÜÄaeut//&/=I " + password + "7421€547" + name + "__+IÄIH§%NK " + password;
final String text = "ÜÄaeut//&/=I " + password + "7421€547" + name
+ "__+IÄIH§%NK " + password;
try {
final MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(text.getBytes(charset), 0, text.length());
@@ -27,7 +29,7 @@ public class CRAZYCRYPT1 implements EncryptionMethod {
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, null, playerName));
}
public static String byteArrayToHexString(final byte... args) {
final char[] chars = new char[args.length * 2];
for (int i = 0; i < args.length; i++) {
@@ -36,4 +38,4 @@ public class CRAZYCRYPT1 implements EncryptionMethod {
}
return new String(chars);
}
}
}
@@ -5,28 +5,29 @@ import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.security.pbkdf2.PBKDF2Engine;
import fr.xephi.authme.security.pbkdf2.PBKDF2Parameters;
public class CryptPBKDF2 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
String result = "pbkdf2_sha256$10000$"+salt+"$";
PBKDF2Parameters params = new PBKDF2Parameters("HmacSHA256", "ASCII", salt.getBytes(), 10000);
PBKDF2Engine engine = new PBKDF2Engine(params);
return result + engine.deriveKey(password,64).toString();
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
String result = "pbkdf2_sha256$10000$" + salt + "$";
PBKDF2Parameters params = new PBKDF2Parameters("HmacSHA256", "ASCII",
salt.getBytes(), 10000);
PBKDF2Engine engine = new PBKDF2Engine(params);
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
String salt = line[2];
String derivedKey = line[3];
PBKDF2Parameters params = new PBKDF2Parameters("HmacSHA256", "ASCII", salt.getBytes(), 10000, derivedKey.getBytes());
PBKDF2Engine engine = new PBKDF2Engine(params);
return engine.verifyKey(password);
}
return result + engine.deriveKey(password, 64).toString();
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
String salt = line[2];
String derivedKey = line[3];
PBKDF2Parameters params = new PBKDF2Parameters("HmacSHA256", "ASCII",
salt.getBytes(), 10000, derivedKey.getBytes());
PBKDF2Engine engine = new PBKDF2Engine(params);
return engine.verifyKey(password);
}
}
@@ -6,24 +6,26 @@ import java.security.NoSuchAlgorithmException;
public class DOUBLEMD5 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password));
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
private static String getMD5(String message) throws NoSuchAlgorithmException {
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(message.getBytes());
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -3,28 +3,38 @@ package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
/**
* <p>Public interface for Custom Password encryption method</p>
* <p>The getHash function is called when we need to crypt the password (/register usually)</p>
* <p>The comparePassword is called when we need to match password (/login usually)</p>
* <p>
* Public interface for Custom Password encryption method
* </p>
* <p>
* The getHash function is called when we need to crypt the password (/register
* usually)
* </p>
* <p>
* The comparePassword is called when we need to match password (/login usually)
* </p>
*/
public interface EncryptionMethod {
/**
* @param password
* @param salt (can be an other data like playerName;salt , playerName, etc...
* for customs methods)
* @return Hashing password
* @throws NoSuchAlgorithmException
*/
String getHash(String password, String salt, String name) throws NoSuchAlgorithmException;
/**
* @param password
* @param salt
* (can be an other data like playerName;salt , playerName,
* etc... for customs methods)
* @return Hashing password
* @throws NoSuchAlgorithmException
*/
String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException;
/**
* @param hash
* @param password
* @param playerName
* @return true if password match, false else
* @throws NoSuchAlgorithmException
*/
boolean comparePassword(String hash, String password, String playerName) throws NoSuchAlgorithmException;
/**
* @param hash
* @param password
* @param playerName
* @return true if password match, false else
* @throws NoSuchAlgorithmException
*/
boolean comparePassword(String hash, String password, String playerName)
throws NoSuchAlgorithmException;
}
@@ -6,27 +6,29 @@ import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
public class IPB3 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt) + getMD5(password));
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt) + getMD5(password));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(getHash(password, salt, playerName));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName)
.getSalt();
return hash.equals(getHash(password, salt, playerName));
}
private static String getMD5(String message) throws NoSuchAlgorithmException {
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(message.getBytes());
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,24 +6,26 @@ import java.security.NoSuchAlgorithmException;
public class JOOMLA implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(password + salt) + ":" + salt;
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(password + salt) + ":" + salt;
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = hash.split(":")[1];
return hash.equals(getMD5(password + salt) + ":" + salt);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = hash.split(":")[1];
return hash.equals(getMD5(password + salt) + ":" + salt);
}
private static String getMD5(String message) throws NoSuchAlgorithmException {
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(message.getBytes());
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,21 +6,25 @@ import java.security.NoSuchAlgorithmException;
public class MD5 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name) throws NoSuchAlgorithmException {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(password);
}
}
@Override
public boolean comparePassword(String hash, String password, String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
private static String getMD5(String message) throws NoSuchAlgorithmException {
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(message.getBytes());
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,25 +6,27 @@ import java.security.NoSuchAlgorithmException;
public class MD5VB implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return "$MD5vb$" + salt + "$" + getMD5(getMD5(password) + salt);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return "$MD5vb$" + salt + "$" + getMD5(getMD5(password) + salt);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
return hash.equals(getHash(password, line[2], ""));
}
private static String getMD5(String message) throws NoSuchAlgorithmException {
}
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(message.getBytes());
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,27 +6,29 @@ import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
public class MYBB implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt)+ getMD5(password));
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt) + getMD5(password));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(getHash(password, salt, playerName));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName)
.getSalt();
return hash.equals(getHash(password, salt, playerName));
}
private static String getMD5(String message) throws NoSuchAlgorithmException {
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(message.getBytes());
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -1,6 +1,6 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
package fr.xephi.authme.security.crypts;
@@ -14,151 +14,138 @@ import java.security.NoSuchAlgorithmException;
* @author stefano
*/
public class PHPBB implements EncryptionMethod {
private static final int PHP_VERSION = 4;
private String itoa64 =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final int PHP_VERSION = 4;
private String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public String phpbb_hash(String password, String salt) {
String random_state = salt;
String random = "";
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);
public String phpbb_hash(String password, String salt) {
String random_state = salt;
String random = "";
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);
}
String hash = _hash_crypt_private(password,
_hash_gensalt_private(random, itoa64));
if (hash.length() == 34) return hash;
return md5(password);
}
String hash = _hash_crypt_private(
password, _hash_gensalt_private(random, itoa64));
if (hash.length() == 34)
return hash;
return md5(password);
}
private String _hash_gensalt_private(String input, String itoa64) {
return _hash_gensalt_private(input, itoa64, 6);
}
@SuppressWarnings("unused")
private String _hash_gensalt_private(
String input, String itoa64, int iteration_count_log2) {
if (iteration_count_log2 < 4 || iteration_count_log2 > 31) {
iteration_count_log2 = 8;
private String _hash_gensalt_private(String input, String itoa64) {
return _hash_gensalt_private(input, itoa64, 6);
}
String output = "$H$";
output += itoa64.charAt(
Math.min(iteration_count_log2 +
((PHP_VERSION >= 5) ? 5 : 3), 30));
output += _hash_encode64(input, 6);
return output;
}
/**
* Encode hash
*/
private String _hash_encode64(String input, int count) {
String output = "";
int i = 0;
do {
int value = input.charAt(i++);
output += itoa64.charAt(value & 0x3f);
if (i < count)
value |= input.charAt(i) << 8;
output += itoa64.charAt((value >> 6) & 0x3f);
if (i++ >= count)
break;
if (i < count)
value |= input.charAt(i) << 16;
output += itoa64.charAt((value >> 12) & 0x3f);
if (i++ >= count)
break;
output += itoa64.charAt((value >> 18) & 0x3f);
} while (i < count);
return output;
}
String _hash_crypt_private(String password, String setting) {
String output = "*";
if (!setting.substring(0, 3).equals("$H$"))
@SuppressWarnings("unused")
private String _hash_gensalt_private(String input, String itoa64,
int iteration_count_log2) {
if (iteration_count_log2 < 4 || iteration_count_log2 > 31) {
iteration_count_log2 = 8;
}
String output = "$H$";
output += itoa64.charAt(Math.min(iteration_count_log2
+ ((PHP_VERSION >= 5) ? 5 : 3), 30));
output += _hash_encode64(input, 6);
return output;
int count_log2 = itoa64.indexOf(setting.charAt(3));
if (count_log2 < 7 || count_log2 > 30)
}
/**
* Encode hash
*/
private String _hash_encode64(String input, int count) {
String output = "";
int i = 0;
do {
int value = input.charAt(i++);
output += itoa64.charAt(value & 0x3f);
if (i < count) value |= input.charAt(i) << 8;
output += itoa64.charAt((value >> 6) & 0x3f);
if (i++ >= count) break;
if (i < count) value |= input.charAt(i) << 16;
output += itoa64.charAt((value >> 12) & 0x3f);
if (i++ >= count) break;
output += itoa64.charAt((value >> 18) & 0x3f);
} while (i < count);
return output;
int count = 1 << count_log2;
String salt = setting.substring(4, 12);
if (salt.length() != 8)
}
String _hash_crypt_private(String password, String setting) {
String output = "*";
if (!setting.substring(0, 3).equals("$H$")) return output;
int count_log2 = itoa64.indexOf(setting.charAt(3));
if (count_log2 < 7 || count_log2 > 30) return output;
int count = 1 << count_log2;
String salt = setting.substring(4, 12);
if (salt.length() != 8) return output;
String m1 = md5(salt + password);
String hash = pack(m1);
do {
hash = pack(md5(hash + password));
} while (--count > 0);
output = setting.substring(0, 12);
output += _hash_encode64(hash, 16);
return output;
String m1 = md5(salt + password);
String hash = pack(m1);
do {
hash = pack(md5(hash + password));
} while (--count > 0);
output = setting.substring(0, 12);
output += _hash_encode64(hash, 16);
return output;
}
public boolean phpbb_check_hash( String password, String hash) {
if (hash.length() == 34)
return _hash_crypt_private(password, hash).equals(hash);
else
return md5(password).equals(hash);
}
public static String md5(String data) {
try {
byte[] bytes = data.getBytes("ISO-8859-1");
MessageDigest md5er = MessageDigest.getInstance("MD5");
byte[] hash = md5er.digest(bytes);
return bytes2hex(hash);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
static int hexToInt(char ch) {
if(ch >= '0' && ch <= '9')
return ch - '0';
ch = Character.toUpperCase(ch);
if(ch >= 'A' && ch <= 'F')
return ch - 'A' + 0xA;
throw new IllegalArgumentException("Not a hex character: " + ch);
}
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);
if (x.length() < 2)
r.append("0");
r.append(x);
public boolean phpbb_check_hash(String password, String hash) {
if (hash.length() == 34) return _hash_crypt_private(password, hash)
.equals(hash);
else return md5(password).equals(hash);
}
return r.toString();
}
static String pack(String hex) {
StringBuffer buf = new StringBuffer();
for(int i = 0; i < hex.length(); i += 2) {
char c1 = hex.charAt(i);
char c2 = hex.charAt(i+1);
char packed = (char) (hexToInt(c1) * 16 + hexToInt(c2));
buf.append(packed);
public static String md5(String data) {
try {
byte[] bytes = data.getBytes("ISO-8859-1");
MessageDigest md5er = MessageDigest.getInstance("MD5");
byte[] hash = md5er.digest(bytes);
return bytes2hex(hash);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
return buf.toString();
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return phpbb_hash(password, salt);
}
static int hexToInt(char ch) {
if (ch >= '0' && ch <= '9') return ch - '0';
ch = Character.toUpperCase(ch);
if (ch >= 'A' && ch <= 'F') return ch - 'A' + 0xA;
throw new IllegalArgumentException("Not a hex character: " + ch);
}
@Override
public boolean comparePassword(String hash, String password, String playerName)
throws NoSuchAlgorithmException {
return phpbb_check_hash(password, hash);
}
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);
if (x.length() < 2) r.append("0");
r.append(x);
}
return r.toString();
}
static String pack(String hex) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < hex.length(); i += 2) {
char c1 = hex.charAt(i);
char c2 = hex.charAt(i + 1);
char packed = (char) (hexToInt(c1) * 16 + hexToInt(c2));
buf.append(packed);
}
return buf.toString();
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return phpbb_hash(password, salt);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return phpbb_check_hash(password, hash);
}
}
@@ -11,49 +11,52 @@ import javax.crypto.spec.SecretKeySpec;
import fr.xephi.authme.AuthMe;
public class PHPFUSION implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
String digest = null;
String algo = "HmacSHA256";
String keyString = getSHA1(salt);
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(password.getBytes("ASCII"));
StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
SecretKeySpec key = new SecretKeySpec(
(keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(password.getBytes("ASCII"));
StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
hash.append(hex);
}
digest = hash.toString();
digest = hash.toString();
} catch (UnsupportedEncodingException e) {
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
}
return digest;
}
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(getHash(password, salt, ""));
}
private static String getSHA1(String message) throws NoSuchAlgorithmException {
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName)
.getSalt();
return hash.equals(getHash(password, salt, ""));
}
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.reset();
sha1.update(message.getBytes());
byte[] digest = sha1.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -4,16 +4,16 @@ import java.security.NoSuchAlgorithmException;
public class PLAINTEXT implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return password;
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return password;
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(password);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(password);
}
}
@@ -5,26 +5,30 @@ import java.security.NoSuchAlgorithmException;
public class ROYALAUTH implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name) throws NoSuchAlgorithmException {
for (int i = 0; i < 25; i++)
password = hash(password, salt);
return password;
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
for (int i = 0; i < 25; i++)
password = hash(password, salt);
return password;
}
public String hash(String password, String salt) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(password.getBytes());
byte byteData[] = md.digest();
StringBuilder sb = new StringBuilder();
for (byte aByteData : byteData)
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
return sb.toString();
}
public String hash(String password, String salt)
throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(password.getBytes());
byte byteData[] = md.digest();
StringBuilder sb = new StringBuilder();
for (byte aByteData : byteData)
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16)
.substring(1));
return sb.toString();
}
@Override
public boolean comparePassword(String hash, String password, String playerName) throws NoSuchAlgorithmException {
return hash.equalsIgnoreCase(getHash(password, "", ""));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equalsIgnoreCase(getHash(password, "", ""));
}
}
@@ -6,27 +6,29 @@ import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
public class SALTED2MD5 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password) + salt);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password) + salt);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(getMD5(getMD5(password) + salt));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName)
.getSalt();
return hash.equals(getMD5(getMD5(password) + salt));
}
private static String getMD5(String message) throws NoSuchAlgorithmException {
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(message.getBytes());
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,23 +6,26 @@ import java.security.NoSuchAlgorithmException;
public class SHA1 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name) throws NoSuchAlgorithmException {
return getSHA1(password);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(password);
}
@Override
public boolean comparePassword(String hash, String password, String playerName)
throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
private static String getSHA1(String message) throws NoSuchAlgorithmException {
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.reset();
sha1.update(message.getBytes());
byte[] digest = sha1.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,24 +6,27 @@ import java.security.NoSuchAlgorithmException;
public class SHA256 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name) throws NoSuchAlgorithmException {
return "$SHA$" + salt + "$" + getSHA256(getSHA256(password) + salt);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return "$SHA$" + salt + "$" + getSHA256(getSHA256(password) + salt);
}
@Override
public boolean comparePassword(String hash, String password, String playerName)
throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
return hash.equals(getHash(password, line[2], ""));
}
private static String getSHA256(String message) throws NoSuchAlgorithmException {
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
return hash.equals(getHash(password, line[2], ""));
}
private static String getSHA256(String message)
throws NoSuchAlgorithmException {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
sha256.reset();
sha256.update(message.getBytes());
byte[] digest = sha256.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,23 +6,25 @@ import java.security.NoSuchAlgorithmException;
public class SHA512 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA512(password);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA512(password);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
private static String getSHA512(String message) throws NoSuchAlgorithmException {
private static String getSHA512(String message)
throws NoSuchAlgorithmException {
MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
sha512.reset();
sha512.update(message.getBytes());
byte[] digest = sha512.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,23 +6,25 @@ import java.security.NoSuchAlgorithmException;
public class SMF implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(name.toLowerCase() + password);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(name.toLowerCase() + password);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, null, playerName));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, null, playerName));
}
private static String getSHA1(String message) throws NoSuchAlgorithmException {
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.reset();
sha1.update(message.getBytes());
byte[] digest = sha1.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -6,27 +6,29 @@ import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
public class WBB3 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(salt.concat(getSHA1(salt.concat(getSHA1(password)))));
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(salt.concat(getSHA1(salt.concat(getSHA1(password)))));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(getHash(password, salt, ""));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName)
.getSalt();
return hash.equals(getHash(password, salt, ""));
}
private static String getSHA1(String message) throws NoSuchAlgorithmException {
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.reset();
sha1.update(message.getBytes());
byte[] digest = sha1.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1,digest));
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(
1, digest));
}
}
@@ -4,16 +4,16 @@ import java.security.NoSuchAlgorithmException;
public class WBB4 implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return BCRYPT.getDoubleHash(password, salt);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return BCRYPT.getDoubleHash(password, salt);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return BCRYPT.checkpw(password, hash, 2);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return BCRYPT.checkpw(password, hash, 2);
}
}
@@ -7,53 +7,57 @@ package fr.xephi.authme.security.crypts;
* <b>References</b>
*
* <P>
* The Whirlpool algorithm was developed by
* <a href="mailto:pbarreto@scopus.com.br">Paulo S. L. M. Barreto</a> and
* <a href="mailto:vincent.rijmen@cryptomathic.com">Vincent Rijmen</a>.
* The Whirlpool algorithm was developed by <a
* href="mailto:pbarreto@scopus.com.br">Paulo S. L. M. Barreto</a> and <a
* href="mailto:vincent.rijmen@cryptomathic.com">Vincent Rijmen</a>.
*
* See
* P.S.L.M. Barreto, V. Rijmen,
* ``The Whirlpool hashing function,''
* First NESSIE workshop, 2000 (tweaked version, 2003),
* <https://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/whirlpool.zip>
* See P.S.L.M. Barreto, V. Rijmen, ``The Whirlpool hashing function,'' First
* NESSIE workshop, 2000 (tweaked version, 2003),
* <https://www.cosic.esat.kuleuven
* .ac.be/nessie/workshop/submissions/whirlpool.zip>
*
* @author Paulo S.L.M. Barreto
* @author Vincent Rijmen.
* @author Paulo S.L.M. Barreto
* @author Vincent Rijmen.
*
* @version 3.0 (2003.03.12)
*
* =============================================================================
* ====================================================================
* =========
*
* Differences from version 2.1:
* Differences from version 2.1:
*
* - Suboptimal diffusion matrix replaced by cir(1, 1, 4, 1, 8, 5, 2, 9).
* - Suboptimal diffusion matrix replaced by cir(1, 1, 4, 1, 8, 5, 2,
* 9).
*
* =============================================================================
* ====================================================================
* =========
*
* Differences from version 2.0:
* Differences from version 2.0:
*
* - Generation of ISO/IEC 10118-3 test vectors.
* - Bug fix: nonzero carry was ignored when tallying the data length
* (this bug apparently only manifested itself when feeding data
* in pieces rather than in a single chunk at once).
* - Generation of ISO/IEC 10118-3 test vectors. - Bug fix: nonzero
* carry was ignored when tallying the data length (this bug apparently
* only manifested itself when feeding data in pieces rather than in a
* single chunk at once).
*
* Differences from version 1.0:
* Differences from version 1.0:
*
* - Original S-box replaced by the tweaked, hardware-efficient version.
* - Original S-box replaced by the tweaked, hardware-efficient
* version.
*
* =============================================================================
* ====================================================================
* =========
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
@@ -80,30 +84,29 @@ public class WHIRLPOOL implements EncryptionMethod {
/**
* The substitution box.
*/
private static final String sbox =
"\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152" +
"\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57" +
"\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85" +
"\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8" +
"\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333" +
"\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0" +
"\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE" +
"\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d" +
"\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF" +
"\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A" +
"\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c" +
"\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04" +
"\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB" +
"\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9" +
"\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1" +
"\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886";
private static final String sbox = "\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152"
+ "\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57"
+ "\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85"
+ "\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8"
+ "\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333"
+ "\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0"
+ "\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE"
+ "\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d"
+ "\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF"
+ "\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A"
+ "\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c"
+ "\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04"
+ "\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB"
+ "\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9"
+ "\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1"
+ "\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886";
private static long[][] C = new long[8][256];
private static long[] rc = new long[R + 1];
private static long[] rc = new long[R + 1];
static {
for (int x = 0; x < 256; x++) {
char c = sbox.charAt(x/2);
char c = sbox.charAt(x / 2);
long v1 = ((x & 1) == 0) ? c >>> 8 : c & 0xff;
long v2 = v1 << 1;
if (v2 >= 0x100L) {
@@ -120,11 +123,11 @@ public class WHIRLPOOL implements EncryptionMethod {
}
long v9 = v8 ^ v1;
/*
* build the circulant table C[0][x] = S[x].[1, 1, 4, 1, 8, 5, 2, 9]:
* build the circulant table C[0][x] = S[x].[1, 1, 4, 1, 8, 5, 2,
* 9]:
*/
C[0][x] =
(v1 << 56) | (v1 << 48) | (v4 << 40) | (v1 << 32) |
(v8 << 24) | (v5 << 16) | (v2 << 8) | (v9 );
C[0][x] = (v1 << 56) | (v1 << 48) | (v4 << 40) | (v1 << 32)
| (v8 << 24) | (v5 << 16) | (v2 << 8) | (v9);
/*
* build the remaining circulant tables C[t][x] = C[0][x] rotr t
*/
@@ -135,18 +138,20 @@ public class WHIRLPOOL implements EncryptionMethod {
/*
* build the round constants:
*/
rc[0] = 0L; /* not used (assigment kept only to properly initialize all variables) */
rc[0] = 0L; /*
* not used (assigment kept only to properly initialize all
* variables)
*/
for (int r = 1; r <= R; r++) {
int i = 8*(r - 1);
rc[r] =
(C[0][i ] & 0xff00000000000000L) ^
(C[1][i + 1] & 0x00ff000000000000L) ^
(C[2][i + 2] & 0x0000ff0000000000L) ^
(C[3][i + 3] & 0x000000ff00000000L) ^
(C[4][i + 4] & 0x00000000ff000000L) ^
(C[5][i + 5] & 0x0000000000ff0000L) ^
(C[6][i + 6] & 0x000000000000ff00L) ^
(C[7][i + 7] & 0x00000000000000ffL);
int i = 8 * (r - 1);
rc[r] = (C[0][i] & 0xff00000000000000L)
^ (C[1][i + 1] & 0x00ff000000000000L)
^ (C[2][i + 2] & 0x0000ff0000000000L)
^ (C[3][i + 3] & 0x000000ff00000000L)
^ (C[4][i + 4] & 0x00000000ff000000L)
^ (C[5][i + 5] & 0x0000000000ff0000L)
^ (C[6][i + 6] & 0x000000000000ff00L)
^ (C[7][i + 7] & 0x00000000000000ffL);
}
}
@@ -173,9 +178,9 @@ public class WHIRLPOOL implements EncryptionMethod {
/**
* The hashing state.
*/
protected long[] hash = new long[8];
protected long[] K = new long[8];
protected long[] L = new long[8];
protected long[] hash = new long[8];
protected long[] K = new long[8];
protected long[] L = new long[8];
protected long[] block = new long[8];
protected long[] state = new long[8];
@@ -190,15 +195,14 @@ public class WHIRLPOOL implements EncryptionMethod {
* map the buffer to a block:
*/
for (int i = 0, j = 0; i < 8; i++, j += 8) {
block[i] =
(((long)buffer[j ] ) << 56) ^
(((long)buffer[j + 1] & 0xffL) << 48) ^
(((long)buffer[j + 2] & 0xffL) << 40) ^
(((long)buffer[j + 3] & 0xffL) << 32) ^
(((long)buffer[j + 4] & 0xffL) << 24) ^
(((long)buffer[j + 5] & 0xffL) << 16) ^
(((long)buffer[j + 6] & 0xffL) << 8) ^
(((long)buffer[j + 7] & 0xffL) );
block[i] = (((long) buffer[j]) << 56)
^ (((long) buffer[j + 1] & 0xffL) << 48)
^ (((long) buffer[j + 2] & 0xffL) << 40)
^ (((long) buffer[j + 3] & 0xffL) << 32)
^ (((long) buffer[j + 4] & 0xffL) << 24)
^ (((long) buffer[j + 5] & 0xffL) << 16)
^ (((long) buffer[j + 6] & 0xffL) << 8)
^ (((long) buffer[j + 7] & 0xffL));
}
/*
* compute and apply K^0 to the cipher state:
@@ -216,7 +220,7 @@ public class WHIRLPOOL implements EncryptionMethod {
for (int i = 0; i < 8; i++) {
L[i] = 0L;
for (int t = 0, s = 56; t < 8; t++, s -= 8) {
L[i] ^= C[t][(int)(K[(i - t) & 7] >>> s) & 0xff];
L[i] ^= C[t][(int) (K[(i - t) & 7] >>> s) & 0xff];
}
}
for (int i = 0; i < 8; i++) {
@@ -229,7 +233,7 @@ public class WHIRLPOOL implements EncryptionMethod {
for (int i = 0; i < 8; i++) {
L[i] = K[i];
for (int t = 0, s = 56; t < 8; t++, s -= 8) {
L[i] ^= C[t][(int)(state[(i - t) & 7] >>> s) & 0xff];
L[i] ^= C[t][(int) (state[(i - t) & 7] >>> s) & 0xff];
}
}
for (int i = 0; i < 8; i++) {
@@ -248,7 +252,7 @@ public class WHIRLPOOL implements EncryptionMethod {
* Initialize the hashing state.
*/
public void NESSIEinit() {
Arrays.fill(bitLength, (byte)0);
Arrays.fill(bitLength, (byte) 0);
bufferBits = bufferPos = 0;
buffer[0] = 0;
Arrays.fill(hash, 0L);
@@ -257,41 +261,41 @@ public class WHIRLPOOL implements EncryptionMethod {
/**
* Delivers input data to the hashing algorithm.
*
* @param source plaintext data to hash.
* @param sourceBits how many bits of plaintext to process.
* @param source
* plaintext data to hash.
* @param sourceBits
* how many bits of plaintext to process.
*
* This method maintains the invariant: bufferBits < 512
* This method maintains the invariant: bufferBits < 512
*/
public void NESSIEadd(byte[] source, long sourceBits) {
/*
sourcePos
|
+-------+-------+-------
||||||||||||||||||||| source
+-------+-------+-------
+-------+-------+-------+-------+-------+-------
|||||||||||||||||||||| buffer
+-------+-------+-------+-------+-------+-------
|
bufferPos
*/
int sourcePos = 0; // index of leftmost source byte containing data (1 to 8 bits).
int sourceGap = (8 - ((int)sourceBits & 7)) & 7; // space on source[sourcePos].
* sourcePos | +-------+-------+------- ||||||||||||||||||||| source
* +-------+-------+-------
* +-------+-------+-------+-------+-------+-------
* |||||||||||||||||||||| buffer
* +-------+-------+-------+-------+-------+------- | bufferPos
*/
int sourcePos = 0; // index of leftmost source byte containing data (1
// to 8 bits).
int sourceGap = (8 - ((int) sourceBits & 7)) & 7; // space on
// source[sourcePos].
int bufferRem = bufferBits & 7; // occupied bits on buffer[bufferPos].
int b;
// tally the length of the added data:
long value = sourceBits;
for (int i = 31, carry = 0; i >= 0; i--) {
carry += (bitLength[i] & 0xff) + ((int)value & 0xff);
bitLength[i] = (byte)carry;
carry += (bitLength[i] & 0xff) + ((int) value & 0xff);
bitLength[i] = (byte) carry;
carry >>>= 8;
value >>>= 8;
}
// process data in chunks of 8 bits:
while (sourceBits > 8) { // at least source[sourcePos] and source[sourcePos+1] contain data.
while (sourceBits > 8) { // at least source[sourcePos] and
// source[sourcePos+1] contain data.
// take a byte from the source:
b = ((source[sourcePos] << sourceGap) & 0xff) |
((source[sourcePos + 1] & 0xff) >>> (8 - sourceGap));
b = ((source[sourcePos] << sourceGap) & 0xff)
| ((source[sourcePos + 1] & 0xff) >>> (8 - sourceGap));
if (b < 0 || b >= 256) {
throw new RuntimeException("LOGIC ERROR");
}
@@ -304,7 +308,7 @@ public class WHIRLPOOL implements EncryptionMethod {
// reset buffer:
bufferBits = bufferPos = 0;
}
buffer[bufferPos] = (byte)((b << (8 - bufferRem)) & 0xff);
buffer[bufferPos] = (byte) ((b << (8 - bufferRem)) & 0xff);
bufferBits += bufferRem;
// proceed to remaining data:
sourceBits -= 8;
@@ -313,29 +317,32 @@ public class WHIRLPOOL implements EncryptionMethod {
// now 0 <= sourceBits <= 8;
// furthermore, all data (if any is left) is in source[sourcePos].
if (sourceBits > 0) {
b = (source[sourcePos] << sourceGap) & 0xff; // bits are left-justified on b.
b = (source[sourcePos] << sourceGap) & 0xff; // bits are
// left-justified on b.
// process the remaining bits:
buffer[bufferPos] |= b >>> bufferRem;
} else {
b = 0;
}
if (bufferRem + sourceBits < 8) {
// all remaining data fits on buffer[bufferPos], and there still remains some space.
// all remaining data fits on buffer[bufferPos], and there still
// remains some space.
bufferBits += sourceBits;
} else {
// buffer[bufferPos] is full:
bufferPos++;
bufferBits += 8 - bufferRem; // bufferBits = 8*bufferPos;
sourceBits -= 8 - bufferRem;
// now 0 <= sourceBits < 8; furthermore, all data is in source[sourcePos].
// now 0 <= sourceBits < 8; furthermore, all data is in
// source[sourcePos].
if (bufferBits == 512) {
// process data block:
processBuffer();
// reset buffer:
bufferBits = bufferPos = 0;
}
buffer[bufferPos] = (byte)((b << (8 - bufferRem)) & 0xff);
bufferBits += (int)sourceBits;
buffer[bufferPos] = (byte) ((b << (8 - bufferRem)) & 0xff);
bufferBits += (int) sourceBits;
}
}
@@ -368,58 +375,59 @@ public class WHIRLPOOL implements EncryptionMethod {
// return the completed message digest:
for (int i = 0, j = 0; i < 8; i++, j += 8) {
long h = hash[i];
digest[j ] = (byte)(h >>> 56);
digest[j + 1] = (byte)(h >>> 48);
digest[j + 2] = (byte)(h >>> 40);
digest[j + 3] = (byte)(h >>> 32);
digest[j + 4] = (byte)(h >>> 24);
digest[j + 5] = (byte)(h >>> 16);
digest[j + 6] = (byte)(h >>> 8);
digest[j + 7] = (byte)(h );
digest[j] = (byte) (h >>> 56);
digest[j + 1] = (byte) (h >>> 48);
digest[j + 2] = (byte) (h >>> 40);
digest[j + 3] = (byte) (h >>> 32);
digest[j + 4] = (byte) (h >>> 24);
digest[j + 5] = (byte) (h >>> 16);
digest[j + 6] = (byte) (h >>> 8);
digest[j + 7] = (byte) (h);
}
}
/**
* Delivers string input data to the hashing algorithm.
*
* @param source plaintext data to hash (ASCII text string).
* @param source
* plaintext data to hash (ASCII text string).
*
* This method maintains the invariant: bufferBits < 512
* This method maintains the invariant: bufferBits < 512
*/
public void NESSIEadd(String source) {
if (source.length() > 0) {
byte[] data = new byte[source.length()];
for (int i = 0; i < source.length(); i++) {
data[i] = (byte)source.charAt(i);
data[i] = (byte) source.charAt(i);
}
NESSIEadd(data, 8*data.length);
NESSIEadd(data, 8 * data.length);
}
}
protected static String display(byte[] array) {
char[] val = new char[2*array.length];
char[] val = new char[2 * array.length];
String hex = "0123456789ABCDEF";
for (int i = 0; i < array.length; i++) {
int b = array[i] & 0xff;
val[2*i] = hex.charAt(b >>> 4);
val[2*i + 1] = hex.charAt(b & 15);
val[2 * i] = hex.charAt(b >>> 4);
val[2 * i + 1] = hex.charAt(b & 15);
}
return String.valueOf(val);
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
byte[] digest = new byte[DIGESTBYTES];
NESSIEinit();
NESSIEadd(password);
NESSIEfinalize(digest);
return display(digest);
}
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
}
@@ -47,7 +47,8 @@ public class WORDPRESS implements EncryptionMethod {
private String crypt(String password, String setting) {
String output = "*0";
if (((setting.length() < 2) ? setting : setting.substring(0, 2)).equalsIgnoreCase(output)) {
if (((setting.length() < 2) ? setting : setting.substring(0, 2))
.equalsIgnoreCase(output)) {
output = "*1";
}
String id = (setting.length() < 3) ? setting : setting.substring(0, 3);
@@ -94,22 +95,24 @@ public class WORDPRESS implements EncryptionMethod {
try {
return string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UnsupportedOperationException("This system doesn't support UTF-8!", e);
throw new UnsupportedOperationException(
"This system doesn't support UTF-8!", e);
}
}
@Override
public String getHash(String password, String salt, String name) throws NoSuchAlgorithmException {
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
byte random[] = new byte[6];
this.randomGen.nextBytes(random);
return crypt(password, gensaltPrivate(stringToUtf8(new String(random))));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String comparedHash = crypt(password, hash);
return comparedHash.equals(hash);
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String comparedHash = crypt(password, hash);
return comparedHash.equals(hash);
}
}
@@ -4,29 +4,31 @@ import java.security.NoSuchAlgorithmException;
public class XAUTH implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
String hash = getWhirlpool(salt + password).toLowerCase();
int saltPos = (password.length() >= hash.length() ? hash.length() - 1 : password.length());
int saltPos = (password.length() >= hash.length() ? hash.length() - 1
: password.length());
return hash.substring(0, saltPos) + salt + hash.substring(saltPos);
}
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
int saltPos = (password.length() >= hash.length() ? hash.length() - 1 : password.length());
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
int saltPos = (password.length() >= hash.length() ? hash.length() - 1
: password.length());
String salt = hash.substring(saltPos, saltPos + 12);
return hash.equals(getHash(password, salt, ""));
}
}
public static String getWhirlpool(String message) {
public static String getWhirlpool(String message) {
WHIRLPOOL w = new WHIRLPOOL();
byte[] digest = new byte[WHIRLPOOL.DIGESTBYTES];
w.NESSIEinit();
w.NESSIEadd(message);
w.NESSIEfinalize(digest);
return WHIRLPOOL.display(digest);
}
}
}
@@ -9,51 +9,48 @@ import java.util.regex.Pattern;
import fr.xephi.authme.AuthMe;
public class XF implements EncryptionMethod {
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA256(getSHA256(password) + regmatch("\"salt\";.:..:\"(.*)\";.:.:\"hashFunc\"", salt));
}
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA256(getSHA256(password)
+ regmatch("\"salt\";.:..:\"(.*)\";.:.:\"hashFunc\"", salt));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(regmatch("\"hash\";.:..:\"(.*)\";.:.:\"salt\"", salt));
}
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName)
.getSalt();
return hash
.equals(regmatch("\"hash\";.:..:\"(.*)\";.:.:\"salt\"", salt));
}
public String getSHA256(String password) throws NoSuchAlgorithmException
{
public String getSHA256(String password) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(password.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for (byte element : byteData)
{
sb.append(Integer.toString((element & 0xff) + 0x100, 16).substring(1));
for (byte element : byteData) {
sb.append(Integer.toString((element & 0xff) + 0x100, 16).substring(
1));
}
StringBuffer hexString = new StringBuffer();
for (byte element : byteData)
{
for (byte element : byteData) {
String hex = Integer.toHexString(0xff & element);
if (hex.length() == 1)
{
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
public String regmatch(String pattern, String line)
{
public String regmatch(String pattern, String line) {
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile(pattern).matcher(line);
while (m.find())
{
while (m.find()) {
allMatches.add(m.group(1));
}
return allMatches.get(0);
@@ -23,14 +23,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class BinTools
{
public class BinTools {
public static final String hex = "0123456789ABCDEF";
/**
@@ -41,15 +41,12 @@ public class BinTools
* @return Hexadecimal representation of b. Uppercase A-F, two characters
* per byte. Empty string on <code>null</code> input.
*/
public static String bin2hex(final byte[] b)
{
if (b == null)
{
public static String bin2hex(final byte[] b) {
if (b == null) {
return "";
}
StringBuffer sb = new StringBuffer(2 * b.length);
for (int i = 0; i < b.length; i++)
{
for (int i = 0; i < b.length; i++) {
int v = (256 + b[i]) % 256;
sb.append(hex.charAt((v / 16) & 15));
sb.append(hex.charAt((v % 16) & 15));
@@ -61,28 +58,23 @@ public class BinTools
* Convert hex string to array of bytes.
*
* @param s
* String containing hexadecimal digits. May be <code>null</code>.
* On odd length leading zero will be assumed.
* String containing hexadecimal digits. May be <code>null</code>
* . On odd length leading zero will be assumed.
* @return Array on bytes, non-<code>null</code>.
* @throws IllegalArgumentException
* when string contains non-hex character
*/
public static byte[] hex2bin(final String s)
{
public static byte[] hex2bin(final String s) {
String m = s;
if (s == null)
{
if (s == null) {
// Allow empty input string.
m = "";
}
else if (s.length() % 2 != 0)
{
} else if (s.length() % 2 != 0) {
// Assume leading zero for odd string length
m = "0" + s;
}
byte r[] = new byte[m.length() / 2];
for (int i = 0, n = 0; i < m.length(); n++)
{
for (int i = 0, n = 0; i < m.length(); n++) {
char h = m.charAt(i++);
char l = m.charAt(i++);
r[n] = (byte) (hex2bin(h) * 16 + hex2bin(l));
@@ -99,18 +91,14 @@ public class BinTools
* @throws IllegalArgumentException
* on non-hex character
*/
public static int hex2bin(char c)
{
if (c >= '0' && c <= '9')
{
public static int hex2bin(char c) {
if (c >= '0' && c <= '9') {
return (c - '0');
}
if (c >= 'A' && c <= 'F')
{
if (c >= 'A' && c <= 'F') {
return (c - 'A' + 10);
}
if (c >= 'a' && c <= 'f')
{
if (c >= 'a' && c <= 'f') {
return (c - 'a' + 10);
}
throw new IllegalArgumentException(
@@ -118,19 +106,16 @@ public class BinTools
+ "'");
}
public static void main(String[] args)
{
public static void main(String[] args) {
byte b[] = new byte[256];
byte bb = 0;
for (int i = 0; i < 256; i++)
{
for (int i = 0; i < 256; i++) {
b[i] = bb++;
}
String s = bin2hex(b);
byte c[] = hex2bin(s);
String t = bin2hex(c);
if (!s.equals(t))
{
if (!s.equals(t)) {
throw new AssertionError("Mismatch");
}
}
@@ -34,14 +34,14 @@ import javax.crypto.spec.SecretKeySpec;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class MacBasedPRF implements PRF
{
public class MacBasedPRF implements PRF {
protected Mac mac;
protected int hLen;
@@ -54,57 +54,41 @@ public class MacBasedPRF implements PRF
* @param macAlgorithm
* Mac algorithm to use, i.e. HMacSHA1 or HMacMD5.
*/
public MacBasedPRF(String macAlgorithm)
{
public MacBasedPRF(String macAlgorithm) {
this.macAlgorithm = macAlgorithm;
try
{
try {
mac = Mac.getInstance(macAlgorithm);
hLen = mac.getMacLength();
}
catch (NoSuchAlgorithmException e)
{
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public MacBasedPRF(String macAlgorithm, String provider)
{
public MacBasedPRF(String macAlgorithm, String provider) {
this.macAlgorithm = macAlgorithm;
try
{
try {
mac = Mac.getInstance(macAlgorithm, provider);
hLen = mac.getMacLength();
}
catch (NoSuchAlgorithmException e)
{
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
catch (NoSuchProviderException e)
{
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
}
}
public byte[] doFinal(byte[] M)
{
public byte[] doFinal(byte[] M) {
byte[] r = mac.doFinal(M);
return r;
}
public int getHLen()
{
public int getHLen() {
return hLen;
}
public void init(byte[] P)
{
try
{
public void init(byte[] P) {
try {
mac.init(new SecretKeySpec(P, macAlgorithm));
}
catch (InvalidKeyException e)
{
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
}
@@ -24,14 +24,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public interface PBKDF2
{
public interface PBKDF2 {
/**
* Convert String-based input to internal byte array, then invoke PBKDF2.
* Desired key length defaults to Pseudo Random Function block size.
@@ -60,8 +60,8 @@ public interface PBKDF2
*
* @param inputPassword
* Candidate password to compute the derived key for.
* @return <code>true</code> password match; <code>false</code>
* incorrect password
* @return <code>true</code> password match; <code>false</code> incorrect
* password
*/
public abstract boolean verifyKey(String inputPassword);
@@ -61,15 +61,15 @@ import java.security.SecureRandom;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898</a>
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class PBKDF2Engine implements PBKDF2
{
public class PBKDF2Engine implements PBKDF2 {
protected PBKDF2Parameters parameters;
protected PRF prf;
@@ -78,8 +78,7 @@ public class PBKDF2Engine implements PBKDF2
* Constructor for PBKDF2 implementation object. PBKDF2 parameters must be
* passed later.
*/
public PBKDF2Engine()
{
public PBKDF2Engine() {
this.parameters = null;
prf = null;
}
@@ -92,8 +91,7 @@ public class PBKDF2Engine implements PBKDF2
* @param parameters
* Data holder for iteration count, method to use et cetera.
*/
public PBKDF2Engine(PBKDF2Parameters parameters)
{
public PBKDF2Engine(PBKDF2Parameters parameters) {
this.parameters = parameters;
prf = null;
}
@@ -108,44 +106,33 @@ public class PBKDF2Engine implements PBKDF2
* @param prf
* Supply customer Pseudo Random Function.
*/
public PBKDF2Engine(PBKDF2Parameters parameters, PRF prf)
{
public PBKDF2Engine(PBKDF2Parameters parameters, PRF prf) {
this.parameters = parameters;
this.prf = prf;
}
public byte[] deriveKey(String inputPassword)
{
public byte[] deriveKey(String inputPassword) {
return deriveKey(inputPassword, 0);
}
public byte[] deriveKey(String inputPassword, int dkLen)
{
public byte[] deriveKey(String inputPassword, int dkLen) {
byte[] r = null;
byte P[] = null;
String charset = parameters.getHashCharset();
if (inputPassword == null)
{
if (inputPassword == null) {
inputPassword = "";
}
try
{
if (charset == null)
{
try {
if (charset == null) {
P = inputPassword.getBytes();
}
else
{
} else {
P = inputPassword.getBytes(charset);
}
}
catch (UnsupportedEncodingException e)
{
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
assertPRF(P);
if (dkLen == 0)
{
if (dkLen == 0) {
dkLen = prf.getHLen();
}
r = PBKDF2(prf, parameters.getSalt(), parameters.getIterationCount(),
@@ -153,23 +140,18 @@ public class PBKDF2Engine implements PBKDF2
return r;
}
public boolean verifyKey(String inputPassword)
{
public boolean verifyKey(String inputPassword) {
byte[] referenceKey = getParameters().getDerivedKey();
if (referenceKey == null || referenceKey.length == 0)
{
if (referenceKey == null || referenceKey.length == 0) {
return false;
}
byte[] inputKey = deriveKey(inputPassword, referenceKey.length);
if (inputKey == null || inputKey.length != referenceKey.length)
{
if (inputKey == null || inputKey.length != referenceKey.length) {
return false;
}
for (int i = 0; i < inputKey.length; i++)
{
if (inputKey[i] != referenceKey[i])
{
for (int i = 0; i < inputKey.length; i++) {
if (inputKey[i] != referenceKey[i]) {
return false;
}
}
@@ -183,17 +165,14 @@ public class PBKDF2Engine implements PBKDF2
* @param P
* User-supplied candidate password as array of bytes.
*/
protected void assertPRF(byte[] P)
{
if (prf == null)
{
protected void assertPRF(byte[] P) {
if (prf == null) {
prf = new MacBasedPRF(parameters.getHashAlgorithm());
}
prf.init(P);
}
public PRF getPseudoRandomFunction()
{
public PRF getPseudoRandomFunction() {
return prf;
}
@@ -211,10 +190,8 @@ public class PBKDF2Engine implements PBKDF2
* desired length of derived key.
* @return internal byte array
*/
protected byte[] PBKDF2(PRF prf, byte[] S, int c, int dkLen)
{
if (S == null)
{
protected byte[] PBKDF2(PRF prf, byte[] S, int c, int dkLen) {
if (S == null) {
S = new byte[0];
}
int hLen = prf.getHLen();
@@ -222,13 +199,11 @@ public class PBKDF2Engine implements PBKDF2
int r = dkLen - (l - 1) * hLen;
byte T[] = new byte[l * hLen];
int ti_offset = 0;
for (int i = 1; i <= l; i++)
{
for (int i = 1; i <= l; i++) {
_F(T, ti_offset, prf, S, c, i);
ti_offset += hLen;
}
if (r < hLen)
{
if (r < hLen) {
// Incomplete last block
byte DK[] = new byte[dkLen];
System.arraycopy(T, 0, DK, 0, dkLen);
@@ -240,16 +215,15 @@ public class PBKDF2Engine implements PBKDF2
/**
* Integer division with ceiling function.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step 2.</a>
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 2.</a>
* @param a
* @param b
* @return ceil(a/b)
*/
protected int ceil(int a, int b)
{
protected int ceil(int a, int b) {
int m = 0;
if (a % b > 0)
{
if (a % b > 0) {
m = 1;
}
return a / b + m;
@@ -258,7 +232,8 @@ public class PBKDF2Engine implements PBKDF2
/**
* Function F.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step 3.</a>
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
* @param dest
* Destination byte buffer
* @param offset
@@ -272,8 +247,7 @@ public class PBKDF2Engine implements PBKDF2
* @param blockIndex
*/
protected void _F(byte[] dest, int offset, PRF prf, byte[] S, int c,
int blockIndex)
{
int blockIndex) {
int hLen = prf.getHLen();
byte U_r[] = new byte[hLen];
@@ -282,8 +256,7 @@ public class PBKDF2Engine implements PBKDF2
System.arraycopy(S, 0, U_i, 0, S.length);
INT(U_i, S.length, blockIndex);
for (int i = 0; i < c; i++)
{
for (int i = 0; i < c; i++) {
U_i = prf.doFinal(U_i);
xor(U_r, U_i);
}
@@ -297,10 +270,8 @@ public class PBKDF2Engine implements PBKDF2
* @param dest
* @param src
*/
protected void xor(byte[] dest, byte[] src)
{
for (int i = 0; i < dest.length; i++)
{
protected void xor(byte[] dest, byte[] src) {
for (int i = 0; i < dest.length; i++) {
dest[i] ^= src[i];
}
}
@@ -308,31 +279,28 @@ public class PBKDF2Engine implements PBKDF2
/**
* Four-octet encoding of the integer i, most significant octet first.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step 3.</a>
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
* @param dest
* @param offset
* @param i
*/
protected void INT(byte[] dest, int offset, int i)
{
protected void INT(byte[] dest, int offset, int i) {
dest[offset + 0] = (byte) (i / (256 * 256 * 256));
dest[offset + 1] = (byte) (i / (256 * 256));
dest[offset + 2] = (byte) (i / (256));
dest[offset + 3] = (byte) (i);
}
public PBKDF2Parameters getParameters()
{
public PBKDF2Parameters getParameters() {
return parameters;
}
public void setParameters(PBKDF2Parameters parameters)
{
public void setParameters(PBKDF2Parameters parameters) {
this.parameters = parameters;
}
public void setPseudoRandomFunction(PRF prf)
{
public void setPseudoRandomFunction(PRF prf) {
this.prf = prf;
}
@@ -352,22 +320,18 @@ public class PBKDF2Engine implements PBKDF2
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws IOException,
NoSuchAlgorithmException
{
NoSuchAlgorithmException {
String password = "password";
String candidate = null;
PBKDF2Formatter formatter = new PBKDF2HexFormatter();
if (args.length >= 1)
{
if (args.length >= 1) {
password = args[0];
}
if (args.length >= 2)
{
if (args.length >= 2) {
candidate = args[1];
}
if (candidate == null)
{
if (candidate == null) {
// Creation mode
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[8];
@@ -379,15 +343,12 @@ public class PBKDF2Engine implements PBKDF2
p.setDerivedKey(e.deriveKey(password));
candidate = formatter.toString(p);
System.out.println(candidate);
}
else
{
} else {
// Verification mode
PBKDF2Parameters p = new PBKDF2Parameters();
p.setHashAlgorithm("HmacSHA1");
p.setHashCharset("ISO-8859-1");
if (formatter.fromString(p, candidate))
{
if (formatter.fromString(p, candidate)) {
throw new IllegalArgumentException(
"Candidate data does not have correct format (\""
+ candidate + "\")");
@@ -24,14 +24,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public interface PBKDF2Formatter
{
public interface PBKDF2Formatter {
/**
* Convert parameters to String.
*
@@ -24,24 +24,21 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class PBKDF2HexFormatter implements PBKDF2Formatter
{
public boolean fromString(PBKDF2Parameters p, String s)
{
if (p == null || s == null)
{
public class PBKDF2HexFormatter implements PBKDF2Formatter {
public boolean fromString(PBKDF2Parameters p, String s) {
if (p == null || s == null) {
return true;
}
String[] p123 = s.split(":");
if (p123 == null || p123.length != 3)
{
if (p123 == null || p123.length != 3) {
return true;
}
@@ -55,8 +52,7 @@ public class PBKDF2HexFormatter implements PBKDF2Formatter
return false;
}
public String toString(PBKDF2Parameters p)
{
public String toString(PBKDF2Parameters p) {
String s = BinTools.bin2hex(p.getSalt()) + ":"
+ String.valueOf(p.getIterationCount()) + ":"
+ BinTools.bin2hex(p.getDerivedKey());
@@ -29,14 +29,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class PBKDF2Parameters
{
public class PBKDF2Parameters {
protected byte[] salt;
protected int iterationCount;
@@ -56,8 +56,7 @@ public class PBKDF2Parameters
* character set and 1000 for iteration count.
*
*/
public PBKDF2Parameters()
{
public PBKDF2Parameters() {
this.hashAlgorithm = null;
this.hashCharset = "UTF-8";
this.salt = null;
@@ -73,14 +72,12 @@ public class PBKDF2Parameters
* @param hashCharset
* for example UTF-8
* @param salt
* Salt as byte array, may be <code>null</code> (not
* recommended)
* Salt as byte array, may be <code>null</code> (not recommended)
* @param iterationCount
* Number of iterations to execute. Recommended value 1000.
*/
public PBKDF2Parameters(String hashAlgorithm, String hashCharset,
byte[] salt, int iterationCount)
{
byte[] salt, int iterationCount) {
this.hashAlgorithm = hashAlgorithm;
this.hashCharset = hashCharset;
this.salt = salt;
@@ -96,16 +93,14 @@ public class PBKDF2Parameters
* @param hashCharset
* for example UTF-8
* @param salt
* Salt as byte array, may be <code>null</code> (not
* recommended)
* Salt as byte array, may be <code>null</code> (not recommended)
* @param iterationCount
* Number of iterations to execute. Recommended value 1000.
* @param derivedKey
* Convenience data holder, not used during computation.
*/
public PBKDF2Parameters(String hashAlgorithm, String hashCharset,
byte[] salt, int iterationCount, byte[] derivedKey)
{
byte[] salt, int iterationCount, byte[] derivedKey) {
this.hashAlgorithm = hashAlgorithm;
this.hashCharset = hashCharset;
this.salt = salt;
@@ -113,53 +108,43 @@ public class PBKDF2Parameters
this.derivedKey = derivedKey;
}
public int getIterationCount()
{
public int getIterationCount() {
return iterationCount;
}
public void setIterationCount(int iterationCount)
{
public void setIterationCount(int iterationCount) {
this.iterationCount = iterationCount;
}
public byte[] getSalt()
{
public byte[] getSalt() {
return salt;
}
public void setSalt(byte[] salt)
{
public void setSalt(byte[] salt) {
this.salt = salt;
}
public byte[] getDerivedKey()
{
public byte[] getDerivedKey() {
return derivedKey;
}
public void setDerivedKey(byte[] derivedKey)
{
public void setDerivedKey(byte[] derivedKey) {
this.derivedKey = derivedKey;
}
public String getHashAlgorithm()
{
public String getHashAlgorithm() {
return hashAlgorithm;
}
public void setHashAlgorithm(String hashAlgorithm)
{
public void setHashAlgorithm(String hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
public String getHashCharset()
{
public String getHashCharset() {
return hashCharset;
}
public void setHashCharset(String hashCharset)
{
public void setHashCharset(String hashCharset) {
this.hashCharset = hashCharset;
}
}
@@ -24,14 +24,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public interface PRF
{
public interface PRF {
/**
* Initialize this instance with the user-supplied password.
*