Stuff from the common floobits workspace

Author:    AuthMe-Team <AuthMeTeam@123NoEmail.com>
This commit is contained in:
AuthMe-Team
2015-11-23 20:20:42 +01:00
committed by Gabriele C
parent 69a09aec17
commit 9ec2d6d059
169 changed files with 5161 additions and 4806 deletions
@@ -39,6 +39,7 @@ public enum HashAlgorithm {
/**
* Constructor for HashAlgorithm.
*
* @param classe Class<?>
*/
HashAlgorithm(Class<?> classe) {
@@ -47,8 +48,9 @@ public enum HashAlgorithm {
/**
* Method getclasse.
* @return Class<?> */
*
* @return Class<?>
*/
public Class<?> getclasse() {
return classe;
}
@@ -1,33 +1,32 @@
package fr.xephi.authme.security;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import org.bukkit.Bukkit;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.events.PasswordEncryptionEvent;
import fr.xephi.authme.security.crypts.BCRYPT;
import fr.xephi.authme.security.crypts.EncryptionMethod;
import fr.xephi.authme.settings.Settings;
import org.bukkit.Bukkit;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
/**
*/
public class PasswordSecurity {
private static SecureRandom rnd = new SecureRandom();
public static HashMap<String, String> userSalt = new HashMap<>();
private static SecureRandom rnd = new SecureRandom();
/**
* Method createSalt.
*
* @param length int
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
public static String createSalt(int length)
throws NoSuchAlgorithmException {
byte[] msg = new byte[40];
@@ -40,12 +39,12 @@ public class PasswordSecurity {
/**
* Method getHash.
* @param alg HashAlgorithm
* @param password String
*
* @param alg HashAlgorithm
* @param password String
* @param playerName String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
public static String getHash(HashAlgorithm alg, String password,
String playerName) throws NoSuchAlgorithmException {
EncryptionMethod method;
@@ -143,12 +142,12 @@ public class PasswordSecurity {
/**
* Method comparePasswordWithHash.
* @param password String
* @param hash String
*
* @param password String
* @param hash String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException */
* @return boolean * @throws NoSuchAlgorithmException
*/
public static boolean comparePasswordWithHash(String password, String hash,
String playerName) throws NoSuchAlgorithmException {
HashAlgorithm algo = Settings.getPasswordHash;
@@ -181,12 +180,12 @@ public class PasswordSecurity {
/**
* Method compareWithAllEncryptionMethod.
* @param password String
* @param hash String
*
* @param password String
* @param hash String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException */
* @return boolean * @throws NoSuchAlgorithmException
*/
private static boolean compareWithAllEncryptionMethod(String password,
String hash, String playerName) throws NoSuchAlgorithmException {
for (HashAlgorithm algo : HashAlgorithm.values()) {
@@ -4,7 +4,6 @@ import java.util.Calendar;
import java.util.Random;
/**
*
* @author Xephi59
* @version $Revision: 1.0 $
*/
@@ -25,6 +24,7 @@ public class RandomString {
/**
* Constructor for RandomString.
*
* @param length int
*/
public RandomString(int length) {
@@ -36,8 +36,9 @@ public class RandomString {
/**
* Method nextString.
* @return String */
*
* @return String
*/
public String nextString() {
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = chars[random.nextInt(chars.length)];
@@ -92,8 +92,6 @@ public class BCRYPT implements EncryptionMethod {
*
* @param d the byte array to encode
* @param len the number of bytes to encode
* @return base64-encoded string * @throws IllegalArgumentException if the length is invalid * @throws IllegalArgumentException
*/
private static String encode_base64(byte d[], int len)
@@ -134,8 +132,8 @@ public class BCRYPT implements EncryptionMethod {
* range-checking againt conversion table
*
* @param x the base64-encoded value
* @return the decoded value of x */
* @return the decoded value of x
*/
private static byte char64(char x) {
if ((int) x > index_64.length)
return -1;
@@ -149,8 +147,6 @@ public class BCRYPT implements EncryptionMethod {
*
* @param s the string to decode
* @param maxolen the maximum number of bytes to decode
* @return an array containing the decoded bytes * @throws IllegalArgumentException if maxolen is invalid * @throws IllegalArgumentException
*/
private static byte[] decode_base64(String s, int maxolen)
@@ -194,6 +190,178 @@ public class BCRYPT implements EncryptionMethod {
return ret;
}
/**
* Cycically extract a word of key material
*
* @param data the string to extract the data from
* @param offp a "pointer" (as a one-entry array) to the current offset into
* data
* @return the next word of material from data
*/
private static int streamtoword(byte data[], int offp[]) {
int i;
int word = 0;
int off = offp[0];
for (i = 0; i < 4; i++) {
word = (word << 8) | (data[off] & 0xff);
off = (off + 1) % data.length;
}
offp[0] = off;
return word;
}
/**
* Hash a password using the OpenBSD bcrypt scheme
*
* @param password the password to hash
* @param salt the salt to hash with (perhaps generated using BCrypt.gensalt)
* @return the hashed password
*/
public static String hashpw(String password, String salt) {
BCRYPT B;
String real_salt;
byte passwordb[], saltb[], hashed[];
char minor = (char) 0;
int rounds, off = 0;
StringBuffer rs = new StringBuffer();
if (salt.charAt(0) != '$' || salt.charAt(1) != '2')
throw new IllegalArgumentException("Invalid salt version");
if (salt.charAt(2) == '$')
off = 3;
else {
minor = salt.charAt(2);
if (minor < 'a' || minor > 'z' || salt.charAt(3) != '$')
throw new IllegalArgumentException("Invalid salt revision");
off = 4;
}
// Extract number of rounds
if (salt.charAt(off + 2) > '$')
throw new IllegalArgumentException("Missing salt rounds");
rounds = Integer.parseInt(salt.substring(off, off + 2));
real_salt = salt.substring(off + 3, off + 25);
try {
passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new AssertionError("UTF-8 is not supported");
}
saltb = decode_base64(real_salt, BCRYPT_SALT_LEN);
B = new BCRYPT();
hashed = B.crypt_raw(passwordb, saltb, rounds);
rs.append("$2");
if (minor >= 'a')
rs.append(minor);
rs.append('$');
if (rounds < 10)
rs.append('0');
rs.append(Integer.toString(rounds));
rs.append('$');
rs.append(encode_base64(saltb, saltb.length));
rs.append(encode_base64(hashed, bf_crypt_ciphertext.length * 4 - 1));
return rs.toString();
}
/**
* Generate a salt for use with the BCrypt.hashpw() method
*
* @param log_rounds the log2 of the number of rounds of hashing to apply - the
* work factor therefore increases as 2**log_rounds.
* @param random an instance of SecureRandom to use
* @return an encoded salt value
*/
public static String gensalt(int log_rounds, SecureRandom random) {
StringBuffer rs = new StringBuffer();
byte rnd[] = new byte[BCRYPT_SALT_LEN];
random.nextBytes(rnd);
rs.append("$2a$");
if (log_rounds < 10)
rs.append('0');
rs.append(Integer.toString(log_rounds));
rs.append('$');
rs.append(encode_base64(rnd, rnd.length));
return rs.toString();
}
/**
* Generate a salt for use with the BCrypt.hashpw() method
*
* @param log_rounds the log2 of the number of rounds of hashing to apply - the
* work factor therefore increases as 2**log_rounds.
* @return an encoded salt value
*/
public static String gensalt(int log_rounds) {
return gensalt(log_rounds, new SecureRandom());
}
/**
* Generate a salt for use with the BCrypt.hashpw() method, selecting a
* reasonable default for the number of hashing rounds to apply
*
* @return an encoded salt value
*/
public static String gensalt() {
return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS);
}
/**
* Check that a plaintext password matches a previously hashed one
*
* @param plaintext the plaintext password to verify
* @param hashed the previously-hashed password
* @return true if the passwords match, false otherwise
*/
public static boolean checkpw(String plaintext, String hashed) {
return (hashed.compareTo(hashpw(plaintext, hashed)) == 0);
}
/**
* Check that a text password matches a previously hashed one with the
* specified number of rounds using recursion
*
* @param text plaintext or hashed text
* @param hashed the previously-hashed password
* @param rounds number of rounds to hash the password
* @return boolean
*/
public static boolean checkpw(String text, String hashed, int rounds) {
boolean matched = false;
if (rounds > 0) {
String hash = hashpw(text, hashed);
if (rounds > 1) {
matched = checkpw(hash, hashed, rounds - 1);
} else {
matched = hash.compareTo(hashed) == 0;
}
} else {
matched = text.compareTo(hashed) == 0;
}
return matched;
}
/**
* Method getDoubleHash.
*
* @param text String
* @param salt String
* @return String
*/
public static String getDoubleHash(String text, String salt) {
String hash = hashpw(text, salt);
return hashpw(text, hash);
}
/**
* Blowfish encipher a single 64-bit block encoded as two 32-bit halves
*
@@ -223,28 +391,6 @@ public class BCRYPT implements EncryptionMethod {
lr[off + 1] = l;
}
/**
* Cycically extract a word of key material
*
* @param data the string to extract the data from
* @param offp a "pointer" (as a one-entry array) to the current offset into
* data
* @return the next word of material from data */
private static int streamtoword(byte data[], int offp[]) {
int i;
int word = 0;
int off = offp[0];
for (i = 0; i < 4; i++) {
word = (word << 8) | (data[off] & 0xff);
off = (off + 1) % data.length;
}
offp[0] = off;
return word;
}
/**
* Initialise the Blowfish key schedule
*/
@@ -321,8 +467,8 @@ public class BCRYPT implements EncryptionMethod {
* @param salt the binary salt to hash with the password
* @param log_rounds the binary logarithm of the number of rounds of hashing to
* apply
* @return an array containing the binary hashed password */
* @return an array containing the binary hashed password
*/
private byte[] crypt_raw(byte password[], byte salt[], int log_rounds) {
int rounds, i, j;
int cdata[] = bf_crypt_ciphertext.clone();
@@ -357,154 +503,14 @@ public class BCRYPT implements EncryptionMethod {
return ret;
}
/**
* Hash a password using the OpenBSD bcrypt scheme
*
* @param password the password to hash
* @param salt the salt to hash with (perhaps generated using BCrypt.gensalt)
* @return the hashed password */
public static String hashpw(String password, String salt) {
BCRYPT B;
String real_salt;
byte passwordb[], saltb[], hashed[];
char minor = (char) 0;
int rounds, off = 0;
StringBuffer rs = new StringBuffer();
if (salt.charAt(0) != '$' || salt.charAt(1) != '2')
throw new IllegalArgumentException("Invalid salt version");
if (salt.charAt(2) == '$')
off = 3;
else {
minor = salt.charAt(2);
if (minor < 'a' || minor > 'z' || salt.charAt(3) != '$')
throw new IllegalArgumentException("Invalid salt revision");
off = 4;
}
// Extract number of rounds
if (salt.charAt(off + 2) > '$')
throw new IllegalArgumentException("Missing salt rounds");
rounds = Integer.parseInt(salt.substring(off, off + 2));
real_salt = salt.substring(off + 3, off + 25);
try {
passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new AssertionError("UTF-8 is not supported");
}
saltb = decode_base64(real_salt, BCRYPT_SALT_LEN);
B = new BCRYPT();
hashed = B.crypt_raw(passwordb, saltb, rounds);
rs.append("$2");
if (minor >= 'a')
rs.append(minor);
rs.append('$');
if (rounds < 10)
rs.append('0');
rs.append(Integer.toString(rounds));
rs.append('$');
rs.append(encode_base64(saltb, saltb.length));
rs.append(encode_base64(hashed, bf_crypt_ciphertext.length * 4 - 1));
return rs.toString();
}
/**
* Generate a salt for use with the BCrypt.hashpw() method
*
* @param log_rounds the log2 of the number of rounds of hashing to apply - the
* work factor therefore increases as 2**log_rounds.
* @param random an instance of SecureRandom to use
* @return an encoded salt value */
public static String gensalt(int log_rounds, SecureRandom random) {
StringBuffer rs = new StringBuffer();
byte rnd[] = new byte[BCRYPT_SALT_LEN];
random.nextBytes(rnd);
rs.append("$2a$");
if (log_rounds < 10)
rs.append('0');
rs.append(Integer.toString(log_rounds));
rs.append('$');
rs.append(encode_base64(rnd, rnd.length));
return rs.toString();
}
/**
* Generate a salt for use with the BCrypt.hashpw() method
*
* @param log_rounds the log2 of the number of rounds of hashing to apply - the
* work factor therefore increases as 2**log_rounds.
* @return an encoded salt value */
public static String gensalt(int log_rounds) {
return gensalt(log_rounds, new SecureRandom());
}
/**
* Generate a salt for use with the BCrypt.hashpw() method, selecting a
* reasonable default for the number of hashing rounds to apply
*
* @return an encoded salt value */
public static String gensalt() {
return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS);
}
/**
* Check that a plaintext password matches a previously hashed one
*
* @param plaintext the plaintext password to verify
* @param hashed the previously-hashed password
* @return true if the passwords match, false otherwise */
public static boolean checkpw(String plaintext, String hashed) {
return (hashed.compareTo(hashpw(plaintext, hashed)) == 0);
}
/**
* Check that a text password matches a previously hashed one with the
* specified number of rounds using recursion
*
* @param text plaintext or hashed text
* @param hashed the previously-hashed password
* @param rounds number of rounds to hash the password
* @return boolean */
public static boolean checkpw(String text, String hashed, int rounds) {
boolean matched = false;
if (rounds > 0) {
String hash = hashpw(text, hashed);
if (rounds > 1) {
matched = checkpw(hash, hashed, rounds - 1);
} else {
matched = hash.compareTo(hashed) == 0;
}
} else {
matched = text.compareTo(hashed) == 0;
}
return matched;
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -513,27 +519,15 @@ public class BCRYPT implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return checkpw(password, hash);
}
/**
* Method getDoubleHash.
* @param text String
* @param salt String
* @return String */
public static String getDoubleHash(String text, String salt) {
String hash = hashpw(text, salt);
return hashpw(text, hash);
}
}
@@ -8,13 +8,12 @@ public class BCRYPT2Y implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -25,16 +24,15 @@ public class BCRYPT2Y implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
String ok = hash.substring(0, 29);
if (ok.length() != 29)
return false;
@@ -8,18 +8,32 @@ import java.security.NoSuchAlgorithmException;
*/
public class CRAZYCRYPT1 implements EncryptionMethod {
private static final char[] CRYPTCHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
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' };
/**
* Method byteArrayToHexString.
*
* @param args byte[]String * @return String
*/
public static String byteArrayToHexString(final byte... args) {
final char[] chars = new char[args.length * 2];
for (int i = 0; i < args.length; i++) {
chars[i * 2] = CRYPTCHARS[(args[i] >> 4) & 0xF];
chars[i * 2 + 1] = CRYPTCHARS[(args[i]) & 0xF];
}
return new String(chars);
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -32,29 +46,18 @@ public class CRAZYCRYPT1 implements EncryptionMethod {
return null;
}
}
/**
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName Stringooleaneptiontring) * @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, null, playerName));
}
/**
* Method byteArrayToHexString.
* @param args byte[]String * @return String
*/
public static String byteArrayToHexString(final byte... args) {
final char[] chars = new char[args.length * 2];
for (int i = 0; i < args.length; i++) {
chars[i * 2] = CRYPTCHARS[(args[i] >> 4) & 0xF];
chars[i * 2 + 1] = CRYPTCHARS[(args[i]) & 0xF];
}
return new String(chars);
}
}
@@ -1,23 +1,22 @@
package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.security.pbkdf2.PBKDF2Engine;
import fr.xephi.authme.security.pbkdf2.PBKDF2Parameters;
import java.security.NoSuchAlgorithmException;
/**
*/
public class CryptPBKDF2 implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -30,16 +29,15 @@ public class CryptPBKDF2 implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
String salt = line[2];
String derivedKey = line[3];
@@ -1,25 +1,23 @@
package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
import fr.xephi.authme.security.pbkdf2.PBKDF2Engine;
import fr.xephi.authme.security.pbkdf2.PBKDF2Parameters;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;
/**
*/
public class CryptPBKDF2Django implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -32,13 +30,12 @@ public class CryptPBKDF2Django implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
@@ -8,42 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class DOUBLEMD5 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password));
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
/**
* Method getMD5.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -53,4 +23,32 @@ public class DOUBLEMD5 implements EncryptionMethod {
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password));
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
}
@@ -13,6 +13,7 @@ import java.security.NoSuchAlgorithmException;
* <p>
* The comparePassword is called when we need to match password (/login usually)
* </p>
*
* @author Gabriele
* @version $Revision: 1.0 $
*/
@@ -20,12 +21,9 @@ public interface EncryptionMethod {
/**
* @param password
* @param salt
* (can be an other data like playerName;salt , playerName,
* etc... for customs methods)
* @param name String
* @param salt (can be an other data like playerName;salt , playerName,
* etc... for customs methods)
* @param name String
* @return Hashing password * @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/
String getHash(String password, String salt, String name)
@@ -35,8 +33,6 @@ public interface EncryptionMethod {
* @param hash
* @param password
* @param playerName
* @return true if password match, false else * @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/
boolean comparePassword(String hash, String password, String playerName)
@@ -1,52 +1,21 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
/**
*/
public class IPB3 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt) + getMD5(password));
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@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));
}
/**
* Method getMD5.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -55,4 +24,33 @@ public class IPB3 implements EncryptionMethod {
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt) + getMD5(password));
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@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));
}
}
@@ -8,43 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class JOOMLA implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(password + salt) + ":" + salt;
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = hash.split(":")[1];
return hash.equals(getMD5(password + salt) + ":" + salt);
}
/**
* Method getMD5.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -53,4 +22,33 @@ public class JOOMLA implements EncryptionMethod {
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(password + salt) + ":" + salt;
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String salt = hash.split(":")[1];
return hash.equals(getMD5(password + salt) + ":" + salt);
}
}
@@ -8,42 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class MD5 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(password);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
/**
* Method getMD5.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -52,4 +22,32 @@ public class MD5 implements EncryptionMethod {
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(password);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
}
@@ -8,43 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class MD5VB implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return "$MD5vb$" + salt + "$" + getMD5(getMD5(password) + salt);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
return hash.equals(getHash(password, line[2], ""));
}
/**
* Method getMD5.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -54,4 +23,33 @@ public class MD5VB implements EncryptionMethod {
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return "$MD5vb$" + salt + "$" + getMD5(getMD5(password) + salt);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
return hash.equals(getHash(password, line[2], ""));
}
}
@@ -1,52 +1,21 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
/**
*/
public class MYBB implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt) + getMD5(password));
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@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));
}
/**
* Method getMD5.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -55,4 +24,33 @@ public class MYBB implements EncryptionMethod {
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(salt) + getMD5(password));
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@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));
}
}
@@ -17,12 +17,79 @@ public class PHPBB implements EncryptionMethod {
private String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
/**
* Method md5.
*
* @param data String
* @return String
*/
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 | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* Method hexToInt.
*
* @param ch char
* @return int
*/
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);
}
/**
* Method bytes2hex.
*
* @param bytes byte[]
* @return String
*/
private static String bytes2hex(byte[] bytes) {
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);
}
return r.toString();
}
/**
* Method pack.
*
* @param hex String
* @return String
*/
static String pack(String hex) {
StringBuilder buf = new StringBuilder();
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();
}
/**
* Method phpbb_hash.
*
* @param password String
* @param salt String
* @return String */
* @param salt String
* @return String
*/
public String phpbb_hash(String password, String salt) {
String random_state = salt;
StringBuilder random = new StringBuilder();
@@ -40,21 +107,23 @@ public class PHPBB implements EncryptionMethod {
/**
* Method _hash_gensalt_private.
* @param input String
*
* @param input String
* @param itoa64 String
* @return String */
* @return String
*/
private String _hash_gensalt_private(String input, String itoa64) {
return _hash_gensalt_private(input, itoa64, 6);
}
/**
* Method _hash_gensalt_private.
* @param input String
* @param itoa64 String
*
* @param input String
* @param itoa64 String
* @param iteration_count_log2 int
* @return String */
* @return String
*/
private String _hash_gensalt_private(String input, String itoa64,
int iteration_count_log2) {
if (iteration_count_log2 < 4 || iteration_count_log2 > 31) {
@@ -68,10 +137,11 @@ public class PHPBB implements EncryptionMethod {
/**
* Encode hash
*
* @param input String
* @param count int
* @return String */
* @return String
*/
private String _hash_encode64(String input, int count) {
StringBuilder output = new StringBuilder();
int i = 0;
@@ -95,10 +165,11 @@ public class PHPBB implements EncryptionMethod {
/**
* Method _hash_crypt_private.
*
* @param password String
* @param setting String
* @return String */
* @param setting String
* @return String
*/
String _hash_crypt_private(String password, String setting) {
String output = "*";
if (!setting.substring(0, 3).equals("$H$"))
@@ -122,87 +193,25 @@ public class PHPBB implements EncryptionMethod {
/**
* Method phpbb_check_hash.
*
* @param password String
* @param hash String
* @return boolean */
* @param hash String
* @return boolean
*/
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);
}
/**
* Method md5.
* @param data String
* @return String */
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 | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* Method hexToInt.
* @param ch char
* @return int */
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);
}
/**
* Method bytes2hex.
* @param bytes byte[]
* @return String */
private static String bytes2hex(byte[] bytes) {
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);
}
return r.toString();
}
/**
* Method pack.
* @param hex String
* @return String */
static String pack(String hex) {
StringBuilder buf = new StringBuilder();
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();
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -211,13 +220,12 @@ public class PHPBB implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
@@ -1,29 +1,42 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import fr.xephi.authme.AuthMe;
/**
*/
public class PHPFUSION implements EncryptionMethod {
/**
* Method getSHA1.
*
* @param message String
* @return String * @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));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -53,33 +66,17 @@ public class PHPFUSION implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(getHash(password, salt, ""));
}
/**
* Method getSHA1.
* @param message String
* @return String * @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));
}
}
@@ -8,13 +8,12 @@ public class PLAINTEXT implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -23,16 +22,15 @@ public class PLAINTEXT implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
return hash.equals(password);
}
@@ -9,13 +9,12 @@ public class ROYALAUTH implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -26,11 +25,11 @@ public class ROYALAUTH implements EncryptionMethod {
/**
* Method hash.
*
* @param password String
* @param salt String
* @return String * @throws NoSuchAlgorithmException */
* @param salt String
* @return String * @throws NoSuchAlgorithmException
*/
public String hash(String password, String salt)
throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-512");
@@ -44,16 +43,15 @@ public class ROYALAUTH implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
return hash.equalsIgnoreCase(getHash(password, "", ""));
}
@@ -1,52 +1,21 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
/**
*/
public class SALTED2MD5 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password) + salt);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@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));
}
/**
* Method getMD5.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -55,4 +24,33 @@ public class SALTED2MD5 implements EncryptionMethod {
byte[] digest = md5.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getMD5(getMD5(password) + salt);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@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));
}
}
@@ -1,52 +1,21 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
/**
*/
public class SALTEDSHA512 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA512(password + salt);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@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, ""));
}
/**
* Method getSHA512.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getSHA512(String message)
throws NoSuchAlgorithmException {
MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
@@ -55,4 +24,33 @@ public class SALTEDSHA512 implements EncryptionMethod {
byte[] digest = sha512.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA512(password + salt);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@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, ""));
}
}
@@ -8,42 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class SHA1 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(password);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
/**
* Method getSHA1.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
@@ -53,4 +23,32 @@ public class SHA1 implements EncryptionMethod {
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(password);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
}
@@ -8,43 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class SHA256 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return "$SHA$" + salt + "$" + getSHA256(getSHA256(password) + salt);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
return hash.equals(getHash(password, line[2], ""));
}
/**
* Method getSHA256.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getSHA256(String message)
throws NoSuchAlgorithmException {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
@@ -54,4 +23,33 @@ public class SHA256 implements EncryptionMethod {
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return "$SHA$" + salt + "$" + getSHA256(getSHA256(password) + salt);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String[] line = hash.split("\\$");
return hash.equals(getHash(password, line[2], ""));
}
}
@@ -8,42 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class SHA512 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA512(password);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
/**
* Method getSHA512.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getSHA512(String message)
throws NoSuchAlgorithmException {
MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
@@ -52,4 +22,32 @@ public class SHA512 implements EncryptionMethod {
byte[] digest = sha512.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA512(password);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
}
@@ -8,42 +8,12 @@ import java.security.NoSuchAlgorithmException;
*/
public class SMF implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(name.toLowerCase() + password);
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, null, playerName));
}
/**
* Method getSHA1.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
@@ -52,4 +22,32 @@ public class SMF implements EncryptionMethod {
byte[] digest = sha1.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(name.toLowerCase() + password);
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, null, playerName));
}
}
@@ -1,52 +1,21 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.AuthMe;
/**
*/
public class WBB3 implements EncryptionMethod {
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(salt.concat(getSHA1(salt.concat(getSHA1(password)))));
}
/**
* Method comparePassword.
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@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, ""));
}
/**
* Method getSHA1.
*
* @param message String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
@@ -55,4 +24,33 @@ public class WBB3 implements EncryptionMethod {
byte[] digest = sha1.digest();
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA1(salt.concat(getSHA1(salt.concat(getSHA1(password)))));
}
/**
* Method comparePassword.
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@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, ""));
}
}
@@ -8,13 +8,12 @@ public class WBB4 implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -23,16 +22,15 @@ public class WBB4 implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
return BCRYPT.checkpw(password, hash, 2);
}
@@ -2,63 +2,61 @@ package fr.xephi.authme.security.crypts;
/**
* The Whirlpool hashing function.
*
* <P>
* <p>
* <p>
* <b>References</b>
*
* <P>
* <p>
* <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>.
*
* <p>
* 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.
*
* @version 3.0 (2003.03.12)
*
* ====================================================================
* =========
*
* Differences from version 2.1:
*
* - Suboptimal diffusion matrix replaced by cir(1, 1, 4, 1, 8, 5, 2,
* 9).
*
* ====================================================================
* =========
*
* 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).
*
* Differences from version 1.0:
*
* - 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.
*
* <p>
* ====================================================================
* =========
* <p>
* Differences from version 2.1:
* <p>
* - Suboptimal diffusion matrix replaced by cir(1, 1, 4, 1, 8, 5, 2,
* 9).
* <p>
* ====================================================================
* =========
* <p>
* Differences from version 2.0:
* <p>
* - 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).
* <p>
* Differences from version 1.0:
* <p>
* - Original S-box replaced by the tweaked, hardware-efficient
* version.
* <p>
* ====================================================================
* =========
* <p>
* 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.
*/
import java.security.NoSuchAlgorithmException;
@@ -166,6 +164,22 @@ public class WHIRLPOOL implements EncryptionMethod {
public WHIRLPOOL() {
}
/**
* Method display.
* @param array byte[]
* @return String */
protected static String display(byte[] array) {
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);
}
return String.valueOf(val);
}
/**
* The core Whirlpool transform.
*/
@@ -232,12 +246,12 @@ 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.
*
*
* This method maintains the invariant: bufferBits < 512
*/
public void NESSIEadd(byte[] source, long sourceBits) {
@@ -249,9 +263,9 @@ public class WHIRLPOOL implements EncryptionMethod {
* +-------+-------+-------+-------+-------+------- | bufferPos
*/
int sourcePos = 0; // index of leftmost source byte containing data (1
// to 8 bits).
// to 8 bits).
int sourceGap = (8 - ((int) sourceBits & 7)) & 7; // space on
// source[sourcePos].
// source[sourcePos].
int bufferRem = bufferBits & 7; // occupied bits on buffer[bufferPos].
int b;
// tally the length of the added data:
@@ -264,7 +278,7 @@ public class WHIRLPOOL implements EncryptionMethod {
}
// process data in chunks of 8 bits:
while (sourceBits > 8) { // at least source[sourcePos] and
// source[sourcePos+1] contain data.
// source[sourcePos+1] contain data.
// take a byte from the source:
b = ((source[sourcePos] << sourceGap) & 0xff) | ((source[sourcePos + 1] & 0xff) >>> (8 - sourceGap));
if (b < 0 || b >= 256) {
@@ -289,7 +303,7 @@ public class WHIRLPOOL implements EncryptionMethod {
// 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.
// left-justified on b.
// process the remaining bits:
buffer[bufferPos] |= b >>> bufferRem;
} else {
@@ -319,7 +333,7 @@ public class WHIRLPOOL implements EncryptionMethod {
/**
* Get the hash value from the hashing state.
*
*
* This method uses the invariant: bufferBits < 512
* @param digest byte[]
*/
@@ -360,10 +374,10 @@ public class WHIRLPOOL implements EncryptionMethod {
/**
* Delivers string input data to the hashing algorithm.
*
*
* @param source
* plaintext data to hash (ASCII text string).
*
*
* This method maintains the invariant: bufferBits < 512
*/
public void NESSIEadd(String source) {
@@ -376,30 +390,14 @@ public class WHIRLPOOL implements EncryptionMethod {
}
}
/**
* Method display.
* @param array byte[]
* @return String */
protected static String display(byte[] array) {
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);
}
return String.valueOf(val);
}
/**
* Method getHash.
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
@Override
public String getHash(String password, String salt, String name)
@@ -416,13 +414,13 @@ public class WHIRLPOOL implements EncryptionMethod {
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
return hash.equals(getHash(password, "", ""));
}
}
@@ -15,10 +15,11 @@ public class WORDPRESS implements EncryptionMethod {
/**
* Method encode64.
* @param src byte[]
*
* @param src byte[]
* @param count int
* @return String */
* @return String
*/
private String encode64(byte[] src, int count) {
int i, value;
StringBuilder output = new StringBuilder();
@@ -55,10 +56,11 @@ public class WORDPRESS implements EncryptionMethod {
/**
* Method crypt.
*
* @param password String
* @param setting String
* @return String */
* @param setting String
* @return String
*/
private String crypt(String password, String setting) {
String output = "*0";
if (((setting.length() < 2) ? setting : setting.substring(0, 2)).equalsIgnoreCase(output)) {
@@ -99,9 +101,10 @@ public class WORDPRESS implements EncryptionMethod {
/**
* Method gensaltPrivate.
*
* @param input byte[]
* @return String */
* @return String
*/
private String gensaltPrivate(byte[] input) {
String output = "$P$";
int iterationCountLog2 = 8;
@@ -112,9 +115,10 @@ public class WORDPRESS implements EncryptionMethod {
/**
* Method stringToUtf8.
*
* @param string String
* @return byte[] */
* @return byte[]
*/
private byte[] stringToUtf8(String string) {
try {
return string.getBytes("UTF-8");
@@ -125,13 +129,12 @@ public class WORDPRESS implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -142,16 +145,15 @@ public class WORDPRESS implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
String comparedHash = crypt(password, hash);
return comparedHash.equals(hash);
}
@@ -6,15 +6,29 @@ import java.security.NoSuchAlgorithmException;
*/
public class XAUTH implements EncryptionMethod {
/**
* Method getWhirlpool.
*
* @param message String
* @return String
*/
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);
}
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -25,33 +39,18 @@ public class XAUTH implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
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, ""));
}
/**
* Method getWhirlpool.
* @param message String
* @return String */
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);
}
}
@@ -1,5 +1,7 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
@@ -7,21 +9,18 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.xephi.authme.AuthMe;
/**
*/
public class XF implements EncryptionMethod {
/**
* Method getHash.
*
* @param password String
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String) */
* @param salt String
* @param name String
* @return String * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#getHash(String, String, String)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -30,26 +29,25 @@ public class XF implements EncryptionMethod {
/**
* Method comparePassword.
* @param hash String
* @param password String
*
* @param hash String
* @param password String
* @param playerName String
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String) */
* @return boolean * @throws NoSuchAlgorithmException * @see fr.xephi.authme.security.crypts.EncryptionMethod#comparePassword(String, String, String)
*/
@Override
public boolean comparePassword(String hash, String password,
String playerName) throws NoSuchAlgorithmException {
String playerName) throws NoSuchAlgorithmException {
String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
return hash.equals(regmatch("\"hash\";.:..:\"(.*)\";.:.:\"salt\"", salt));
}
/**
* Method getSHA256.
*
* @param password String
* @return String * @throws NoSuchAlgorithmException */
* @return String * @throws NoSuchAlgorithmException
*/
public String getSHA256(String password) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(password.getBytes());
@@ -71,10 +69,11 @@ public class XF implements EncryptionMethod {
/**
* Method regmatch.
*
* @param pattern String
* @param line String
* @return String */
* @param line String
* @return String
*/
public String regmatch(String pattern, String line) {
List<String> allMatches = new ArrayList<>();
Matcher m = Pattern.compile(pattern).matcher(line);
@@ -26,7 +26,7 @@ package fr.xephi.authme.security.pbkdf2;
* 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
*/
@@ -36,12 +36,11 @@ public class BinTools {
/**
* Simple binary-to-hexadecimal conversion.
*
* @param b
* Input bytes. May be <code>null</code>.
*
* @param b Input bytes. May be <code>null</code>.
* @return Hexadecimal representation of b. Uppercase A-F, two characters
* per byte. Empty string on <code>null</code> input. */
* per byte. Empty string on <code>null</code> input.
*/
public static String bin2hex(final byte[] b) {
if (b == null) {
return "";
@@ -57,14 +56,12 @@ 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.
*
* @param s 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 */
* when string contains non-hex character
*/
public static byte[] hex2bin(final String s) {
String m = s;
if (s == null) {
@@ -85,13 +82,11 @@ public class BinTools {
/**
* Convert hex digit to numerical value.
*
* @param c
* 0-9, a-f, A-F allowd.
*
* @param c 0-9, a-f, A-F allowd.
* @return 0-15 * @throws IllegalArgumentException
* on non-hex character */
* on non-hex character
*/
public static int hex2bin(char c) {
if (c >= '0' && c <= '9') {
return (c - '0');
@@ -107,6 +102,7 @@ public class BinTools {
/**
* Method main.
*
* @param args String[]
*/
public static void main(String[] args) {
@@ -1,15 +1,14 @@
package fr.xephi.authme.security.pbkdf2;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
* Default PRF implementation based on standard javax.crypt.Mac mechanisms.
*
* <p>
* <hr />
* <p>
* A free Java implementation of Password Based Key Derivation Function 2 as
@@ -52,8 +51,7 @@ public class MacBasedPRF implements PRF {
/**
* Create Mac-based Pseudo Random Function.
*
* @param macAlgorithm
* Mac algorithm to use, i.e. HMacSHA1 or HMacMD5.
* @param macAlgorithm Mac algorithm to use, i.e. HMacSHA1 or HMacMD5.
*/
public MacBasedPRF(String macAlgorithm) {
this.macAlgorithm = macAlgorithm;
@@ -67,8 +65,9 @@ public class MacBasedPRF implements PRF {
/**
* Constructor for MacBasedPRF.
*
* @param macAlgorithm String
* @param provider String
* @param provider String
*/
public MacBasedPRF(String macAlgorithm, String provider) {
this.macAlgorithm = macAlgorithm;
@@ -82,10 +81,10 @@ public class MacBasedPRF implements PRF {
/**
* Method doFinal.
*
* @param M byte[]
* @return byte[] * @see fr.xephi.authme.security.pbkdf2.PRF#doFinal(byte[]) */
* @return byte[] * @see fr.xephi.authme.security.pbkdf2.PRF#doFinal(byte[])
*/
public byte[] doFinal(byte[] M) {
byte[] r = mac.doFinal(M);
return r;
@@ -93,18 +92,19 @@ public class MacBasedPRF implements PRF {
/**
* Method getHLen.
* @return int * @see fr.xephi.authme.security.pbkdf2.PRF#getHLen() */
*
* @return int * @see fr.xephi.authme.security.pbkdf2.PRF#getHLen()
*/
public int getHLen() {
return hLen;
}
/**
* Method init.
*
* @param P byte[]
* @see fr.xephi.authme.security.pbkdf2.PRF#init(byte[]) */
* @see fr.xephi.authme.security.pbkdf2.PRF#init(byte[])
*/
public void init(byte[] P) {
try {
mac.init(new SecretKeySpec(P, macAlgorithm));
@@ -27,7 +27,7 @@ package fr.xephi.authme.security.pbkdf2;
* 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
*/
@@ -36,64 +36,59 @@ public interface PBKDF2 {
/**
* Convert String-based input to internal byte array, then invoke PBKDF2.
* Desired key length defaults to Pseudo Random Function block size.
*
* @param inputPassword
* Candidate password to compute the derived key for.
* @return internal byte array */
*
* @param inputPassword Candidate password to compute the derived key for.
* @return internal byte array
*/
public abstract byte[] deriveKey(String inputPassword);
/**
* Convert String-based input to internal byte array, then invoke PBKDF2.
*
* @param inputPassword
* Candidate password to compute the derived key for.
* @param dkLen
* Specify desired key length
* @return internal byte array */
*
* @param inputPassword Candidate password to compute the derived key for.
* @param dkLen Specify desired key length
* @return internal byte array
*/
public abstract byte[] deriveKey(String inputPassword, int dkLen);
/**
* Convert String-based input to internal byte arrays, then invoke PBKDF2
* and verify result against the reference data that is supplied in the
* PBKDF2Parameters.
*
* @param inputPassword
* Candidate password to compute the derived key for.
*
* @param inputPassword Candidate password to compute the derived key for.
* @return <code>true</code> password match; <code>false</code> incorrect
* password */
* password
*/
public abstract boolean verifyKey(String inputPassword);
/**
* Allow reading of configured parameters.
*
* @return Currently set parameters. */
*
* @return Currently set parameters.
*/
public abstract PBKDF2Parameters getParameters();
/**
* Allow setting of configured parameters.
*
*
* @param parameters
*/
public abstract void setParameters(PBKDF2Parameters parameters);
/**
* Get currently set Pseudo Random Function.
*
* @return Currently set Pseudo Random Function */
*
* @return Currently set Pseudo Random Function
*/
public abstract PRF getPseudoRandomFunction();
/**
* Set the Pseudo Random Function to use. Note that deriveKeys/getPRF does
* init this object using the supplied candidate password. If this is
* undesired, one has to override getPRF.
*
* @param prf
* Pseudo Random Function to set.
*
* @param prf Pseudo Random Function to set.
*/
public abstract void setPseudoRandomFunction(PRF prf);
}
@@ -10,17 +10,17 @@ import java.security.SecureRandom;
* Request for Comments: 2898 PKCS #5: Password-Based Cryptography Specification
* <p>
* Version 2.0
*
* <p>
* <p>
* PBKDF2 (P, S, c, dkLen)
*
* <p>
* <p>
* Options:
* <ul>
* <li>PRF underlying pseudorandom function (hLen denotes the length in octets
* of the pseudorandom function output). PRF is pluggable.</li>
* </ul>
*
* <p>
* <p>
* Input:
* <ul>
@@ -30,13 +30,13 @@ import java.security.SecureRandom;
* <li>dkLen intended length in octets of the derived key, a positive integer,
* at most (2^32 - 1) * hLen</li>
* </ul>
*
* <p>
* <p>
* Output:
* <ul>
* <li>DK derived key, a dkLen-octet string</li>
* </ul>
*
* <p>
* <hr />
* <p>
* A free Java implementation of Password Based Key Derivation Function 2 as
@@ -64,10 +64,10 @@ import java.security.SecureRandom;
* <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
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898</a>
*/
public class PBKDF2Engine implements PBKDF2 {
@@ -88,9 +88,8 @@ public class PBKDF2Engine implements PBKDF2 {
* Constructor for PBKDF2 implementation object. PBKDF2 parameters are
* passed so that this implementation knows iteration count, method to use
* and String encoding.
*
* @param parameters
* Data holder for iteration count, method to use et cetera.
*
* @param parameters Data holder for iteration count, method to use et cetera.
*/
public PBKDF2Engine(PBKDF2Parameters parameters) {
this.parameters = parameters;
@@ -101,262 +100,26 @@ public class PBKDF2Engine implements PBKDF2 {
* Constructor for PBKDF2 implementation object. PBKDF2 parameters are
* passed so that this implementation knows iteration count, method to use
* and String encoding.
*
* @param parameters
* Data holder for iteration count, method to use et cetera.
* @param prf
* Supply customer Pseudo Random Function.
*
* @param parameters Data holder for iteration count, method to use et cetera.
* @param prf Supply customer Pseudo Random Function.
*/
public PBKDF2Engine(PBKDF2Parameters parameters, PRF prf) {
this.parameters = parameters;
this.prf = prf;
}
/**
* Method deriveKey.
* @param inputPassword String
* @return byte[] * @see fr.xephi.authme.security.pbkdf2.PBKDF2#deriveKey(String) */
public byte[] deriveKey(String inputPassword) {
return deriveKey(inputPassword, 0);
}
/**
* Method deriveKey.
* @param inputPassword String
* @param dkLen int
* @return byte[] * @see fr.xephi.authme.security.pbkdf2.PBKDF2#deriveKey(String, int) */
public byte[] deriveKey(String inputPassword, int dkLen) {
byte[] r = null;
byte P[] = null;
String charset = parameters.getHashCharset();
if (inputPassword == null) {
inputPassword = "";
}
try {
if (charset == null) {
P = inputPassword.getBytes();
} else {
P = inputPassword.getBytes(charset);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
assertPRF(P);
if (dkLen == 0) {
dkLen = prf.getHLen();
}
r = PBKDF2(prf, parameters.getSalt(), parameters.getIterationCount(), dkLen);
return r;
}
/**
* Method verifyKey.
* @param inputPassword String
* @return boolean * @see fr.xephi.authme.security.pbkdf2.PBKDF2#verifyKey(String) */
public boolean verifyKey(String inputPassword) {
byte[] referenceKey = getParameters().getDerivedKey();
if (referenceKey == null || referenceKey.length == 0) {
return false;
}
byte[] inputKey = deriveKey(inputPassword, referenceKey.length);
if (inputKey == null || inputKey.length != referenceKey.length) {
return false;
}
for (int i = 0; i < inputKey.length; i++) {
if (inputKey[i] != referenceKey[i]) {
return false;
}
}
return true;
}
/**
* Factory method. Default implementation is (H)MAC-based. To be overridden
* in derived classes.
*
* @param P
* User-supplied candidate password as array of bytes.
*/
protected void assertPRF(byte[] P) {
if (prf == null) {
prf = new MacBasedPRF(parameters.getHashAlgorithm());
}
prf.init(P);
}
/**
* Method getPseudoRandomFunction.
* @return PRF * @see fr.xephi.authme.security.pbkdf2.PBKDF2#getPseudoRandomFunction() */
public PRF getPseudoRandomFunction() {
return prf;
}
/**
* Core Password Based Key Derivation Function 2.
*
* @param prf
* Pseudo Random Function (i.e. HmacSHA1)
* @param S
* Salt as array of bytes. <code>null</code> means no salt.
* @param c
* Iteration count (see RFC 2898 4.2)
* @param dkLen
* desired length of derived key.
* @return internal byte array * @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2</a> */
protected byte[] PBKDF2(PRF prf, byte[] S, int c, int dkLen) {
if (S == null) {
S = new byte[0];
}
int hLen = prf.getHLen();
int l = ceil(dkLen, hLen);
int r = dkLen - (l - 1) * hLen;
byte T[] = new byte[l * hLen];
int ti_offset = 0;
for (int i = 1; i <= l; i++) {
_F(T, ti_offset, prf, S, c, i);
ti_offset += hLen;
}
if (r < hLen) {
// Incomplete last block
byte DK[] = new byte[dkLen];
System.arraycopy(T, 0, DK, 0, dkLen);
return DK;
}
return T;
}
/**
* Integer division with ceiling function.
*
* @param a
* @param b
* @return ceil(a/b) * @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 2.</a> */
protected int ceil(int a, int b) {
int m = 0;
if (a % b > 0) {
m = 1;
}
return a / b + m;
}
/**
* Function F.
*
* @param dest
* Destination byte buffer
* @param offset
* Offset into destination byte buffer
* @param prf
* Pseudo Random Function
* @param S
* Salt as array of bytes
* @param c
* Iteration count
* @param blockIndex
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a> */
protected void _F(byte[] dest, int offset, PRF prf, byte[] S, int c,
int blockIndex) {
int hLen = prf.getHLen();
byte U_r[] = new byte[hLen];
// U0 = S || INT (i);
byte U_i[] = new byte[S.length + 4];
System.arraycopy(S, 0, U_i, 0, S.length);
INT(U_i, S.length, blockIndex);
for (int i = 0; i < c; i++) {
U_i = prf.doFinal(U_i);
xor(U_r, U_i);
}
System.arraycopy(U_r, 0, dest, offset, hLen);
}
/**
* Block-Xor. Xor source bytes into destination byte buffer. Destination
* buffer must be same length or less than source buffer.
*
* @param dest
* @param src
*/
protected void xor(byte[] dest, byte[] src) {
for (int i = 0; i < dest.length; i++) {
dest[i] ^= src[i];
}
}
/**
* Four-octet encoding of the integer i, most significant octet first.
*
* @param dest
* @param offset
* @param i
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a> */
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);
}
/**
* Method getParameters.
* @return PBKDF2Parameters * @see fr.xephi.authme.security.pbkdf2.PBKDF2#getParameters() */
public PBKDF2Parameters getParameters() {
return parameters;
}
/**
* Method setParameters.
* @param parameters PBKDF2Parameters
* @see fr.xephi.authme.security.pbkdf2.PBKDF2#setParameters(PBKDF2Parameters) */
public void setParameters(PBKDF2Parameters parameters) {
this.parameters = parameters;
}
/**
* Method setPseudoRandomFunction.
* @param prf PRF
* @see fr.xephi.authme.security.pbkdf2.PBKDF2#setPseudoRandomFunction(PRF) */
public void setPseudoRandomFunction(PRF prf) {
this.prf = prf;
}
/**
* Convenience client function. Convert supplied password with random 8-byte
* salt and 1000 iterations using HMacSHA1. Assume that password is in
* ISO-8559-1 encoding. Output result as
* &quot;Salt:iteration-count:PBKDF2&quot; with binary data in hexadecimal
* encoding.
*
* <p>
* Example: Password &quot;password&quot; (without the quotes) leads to
* 48290A0B96C426C3:1000:973899B1D4AFEB3ED371060D0797E0EE0142BD04
*
* @param args
* Supply the password as argument.
*
* @param args Supply the password as argument.
* @throws IOException * @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/
public static void main(String[] args)
@@ -394,4 +157,225 @@ public class PBKDF2Engine implements PBKDF2 {
System.exit(verifyOK ? 0 : 1);
}
}
/**
* Method deriveKey.
*
* @param inputPassword String
* @return byte[] * @see fr.xephi.authme.security.pbkdf2.PBKDF2#deriveKey(String)
*/
public byte[] deriveKey(String inputPassword) {
return deriveKey(inputPassword, 0);
}
/**
* Method deriveKey.
*
* @param inputPassword String
* @param dkLen int
* @return byte[] * @see fr.xephi.authme.security.pbkdf2.PBKDF2#deriveKey(String, int)
*/
public byte[] deriveKey(String inputPassword, int dkLen) {
byte[] r = null;
byte P[] = null;
String charset = parameters.getHashCharset();
if (inputPassword == null) {
inputPassword = "";
}
try {
if (charset == null) {
P = inputPassword.getBytes();
} else {
P = inputPassword.getBytes(charset);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
assertPRF(P);
if (dkLen == 0) {
dkLen = prf.getHLen();
}
r = PBKDF2(prf, parameters.getSalt(), parameters.getIterationCount(), dkLen);
return r;
}
/**
* Method verifyKey.
*
* @param inputPassword String
* @return boolean * @see fr.xephi.authme.security.pbkdf2.PBKDF2#verifyKey(String)
*/
public boolean verifyKey(String inputPassword) {
byte[] referenceKey = getParameters().getDerivedKey();
if (referenceKey == null || referenceKey.length == 0) {
return false;
}
byte[] inputKey = deriveKey(inputPassword, referenceKey.length);
if (inputKey == null || inputKey.length != referenceKey.length) {
return false;
}
for (int i = 0; i < inputKey.length; i++) {
if (inputKey[i] != referenceKey[i]) {
return false;
}
}
return true;
}
/**
* Factory method. Default implementation is (H)MAC-based. To be overridden
* in derived classes.
*
* @param P User-supplied candidate password as array of bytes.
*/
protected void assertPRF(byte[] P) {
if (prf == null) {
prf = new MacBasedPRF(parameters.getHashAlgorithm());
}
prf.init(P);
}
/**
* Method getPseudoRandomFunction.
*
* @return PRF * @see fr.xephi.authme.security.pbkdf2.PBKDF2#getPseudoRandomFunction()
*/
public PRF getPseudoRandomFunction() {
return prf;
}
/**
* Method setPseudoRandomFunction.
*
* @param prf PRF
* @see fr.xephi.authme.security.pbkdf2.PBKDF2#setPseudoRandomFunction(PRF)
*/
public void setPseudoRandomFunction(PRF prf) {
this.prf = prf;
}
/**
* Core Password Based Key Derivation Function 2.
*
* @param prf Pseudo Random Function (i.e. HmacSHA1)
* @param S Salt as array of bytes. <code>null</code> means no salt.
* @param c Iteration count (see RFC 2898 4.2)
* @param dkLen desired length of derived key.
* @return internal byte array * @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2</a>
*/
protected byte[] PBKDF2(PRF prf, byte[] S, int c, int dkLen) {
if (S == null) {
S = new byte[0];
}
int hLen = prf.getHLen();
int l = ceil(dkLen, hLen);
int r = dkLen - (l - 1) * hLen;
byte T[] = new byte[l * hLen];
int ti_offset = 0;
for (int i = 1; i <= l; i++) {
_F(T, ti_offset, prf, S, c, i);
ti_offset += hLen;
}
if (r < hLen) {
// Incomplete last block
byte DK[] = new byte[dkLen];
System.arraycopy(T, 0, DK, 0, dkLen);
return DK;
}
return T;
}
/**
* Integer division with ceiling function.
*
* @param a
* @param b
* @return ceil(a/b) * @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 2.</a>
*/
protected int ceil(int a, int b) {
int m = 0;
if (a % b > 0) {
m = 1;
}
return a / b + m;
}
/**
* Function F.
*
* @param dest Destination byte buffer
* @param offset Offset into destination byte buffer
* @param prf Pseudo Random Function
* @param S Salt as array of bytes
* @param c Iteration count
* @param blockIndex
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
*/
protected void _F(byte[] dest, int offset, PRF prf, byte[] S, int c,
int blockIndex) {
int hLen = prf.getHLen();
byte U_r[] = new byte[hLen];
// U0 = S || INT (i);
byte U_i[] = new byte[S.length + 4];
System.arraycopy(S, 0, U_i, 0, S.length);
INT(U_i, S.length, blockIndex);
for (int i = 0; i < c; i++) {
U_i = prf.doFinal(U_i);
xor(U_r, U_i);
}
System.arraycopy(U_r, 0, dest, offset, hLen);
}
/**
* Block-Xor. Xor source bytes into destination byte buffer. Destination
* buffer must be same length or less than source buffer.
*
* @param dest
* @param src
*/
protected void xor(byte[] dest, byte[] src) {
for (int i = 0; i < dest.length; i++) {
dest[i] ^= src[i];
}
}
/**
* Four-octet encoding of the integer i, most significant octet first.
*
* @param dest
* @param offset
* @param i
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
*/
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);
}
/**
* Method getParameters.
*
* @return PBKDF2Parameters * @see fr.xephi.authme.security.pbkdf2.PBKDF2#getParameters()
*/
public PBKDF2Parameters getParameters() {
return parameters;
}
/**
* Method setParameters.
*
* @param parameters PBKDF2Parameters
* @see fr.xephi.authme.security.pbkdf2.PBKDF2#setParameters(PBKDF2Parameters)
*/
public void setParameters(PBKDF2Parameters parameters) {
this.parameters = parameters;
}
}
@@ -27,7 +27,7 @@ package fr.xephi.authme.security.pbkdf2;
* 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
*/
@@ -35,22 +35,20 @@ public interface PBKDF2Formatter {
/**
* Convert parameters to String.
*
* @param p
* Parameters object to output.
* @return String representation */
*
* @param p Parameters object to output.
* @return String representation
*/
public abstract String toString(PBKDF2Parameters p);
/**
* Convert String to parameters. Depending on actual implementation, it may
* be required to set further fields externally.
*
* @param s
* String representation of parameters to decode.
*
* @param s String representation of parameters to decode.
* @param p PBKDF2Parameters
* @return <code>false</code> syntax OK, <code>true</code> some syntax
* issue. */
* issue.
*/
public abstract boolean fromString(PBKDF2Parameters p, String s);
}
@@ -27,7 +27,7 @@ package fr.xephi.authme.security.pbkdf2;
* 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
*/
@@ -35,11 +35,11 @@ public class PBKDF2HexFormatter implements PBKDF2Formatter {
/**
* Method fromString.
*
* @param p PBKDF2Parameters
* @param s String
* @return boolean * @see fr.xephi.authme.security.pbkdf2.PBKDF2Formatter#fromString(PBKDF2Parameters, String) */
* @return boolean * @see fr.xephi.authme.security.pbkdf2.PBKDF2Formatter#fromString(PBKDF2Parameters, String)
*/
public boolean fromString(PBKDF2Parameters p, String s) {
if (p == null || s == null) {
return true;
@@ -62,10 +62,10 @@ public class PBKDF2HexFormatter implements PBKDF2Formatter {
/**
* Method toString.
*
* @param p PBKDF2Parameters
* @return String * @see fr.xephi.authme.security.pbkdf2.PBKDF2Formatter#toString(PBKDF2Parameters) */
* @return String * @see fr.xephi.authme.security.pbkdf2.PBKDF2Formatter#toString(PBKDF2Parameters)
*/
public String toString(PBKDF2Parameters p) {
String s = BinTools.bin2hex(p.getSalt()) + ":" + String.valueOf(p.getIterationCount()) + ":" + BinTools.bin2hex(p.getDerivedKey());
return s;
@@ -4,7 +4,7 @@ package fr.xephi.authme.security.pbkdf2;
* <p>
* Parameter data holder for PBKDF2 configuration.
* </p>
*
* <p>
* <hr />
* <p>
* A free Java implementation of Password Based Key Derivation Function 2 as
@@ -32,7 +32,7 @@ package fr.xephi.authme.security.pbkdf2;
* 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
*/
@@ -55,7 +55,6 @@ public class PBKDF2Parameters {
/**
* Constructor. Defaults to <code>null</code> for byte arrays, UTF-8 as
* character set and 1000 for iteration count.
*
*/
public PBKDF2Parameters() {
this.hashAlgorithm = null;
@@ -67,18 +66,14 @@ public class PBKDF2Parameters {
/**
* Constructor.
*
* @param hashAlgorithm
* for example HMacSHA1 or HMacMD5
* @param hashCharset
* for example UTF-8
* @param salt
* Salt as byte array, may be <code>null</code> (not recommended)
* @param iterationCount
* Number of iterations to execute. Recommended value 1000.
*
* @param hashAlgorithm for example HMacSHA1 or HMacMD5
* @param hashCharset for example UTF-8
* @param salt 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;
@@ -88,20 +83,15 @@ public class PBKDF2Parameters {
/**
* Constructor.
*
* @param hashAlgorithm
* for example HMacSHA1 or HMacMD5
* @param hashCharset
* for example UTF-8
* @param salt
* 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.
*
* @param hashAlgorithm for example HMacSHA1 or HMacMD5
* @param hashCharset for example UTF-8
* @param salt 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;
@@ -111,14 +101,16 @@ public class PBKDF2Parameters {
/**
* Method getIterationCount.
* @return int */
*
* @return int
*/
public int getIterationCount() {
return iterationCount;
}
/**
* Method setIterationCount.
*
* @param iterationCount int
*/
public void setIterationCount(int iterationCount) {
@@ -127,14 +119,16 @@ public class PBKDF2Parameters {
/**
* Method getSalt.
* @return byte[] */
*
* @return byte[]
*/
public byte[] getSalt() {
return salt;
}
/**
* Method setSalt.
*
* @param salt byte[]
*/
public void setSalt(byte[] salt) {
@@ -143,14 +137,16 @@ public class PBKDF2Parameters {
/**
* Method getDerivedKey.
* @return byte[] */
*
* @return byte[]
*/
public byte[] getDerivedKey() {
return derivedKey;
}
/**
* Method setDerivedKey.
*
* @param derivedKey byte[]
*/
public void setDerivedKey(byte[] derivedKey) {
@@ -159,14 +155,16 @@ public class PBKDF2Parameters {
/**
* Method getHashAlgorithm.
* @return String */
*
* @return String
*/
public String getHashAlgorithm() {
return hashAlgorithm;
}
/**
* Method setHashAlgorithm.
*
* @param hashAlgorithm String
*/
public void setHashAlgorithm(String hashAlgorithm) {
@@ -175,14 +173,16 @@ public class PBKDF2Parameters {
/**
* Method getHashCharset.
* @return String */
*
* @return String
*/
public String getHashCharset() {
return hashCharset;
}
/**
* Method setHashCharset.
*
* @param hashCharset String
*/
public void setHashCharset(String hashCharset) {
@@ -27,7 +27,7 @@ package fr.xephi.authme.security.pbkdf2;
* 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
*/
@@ -35,27 +35,25 @@ public interface PRF {
/**
* Initialize this instance with the user-supplied password.
*
* @param P
* The password supplied as array of bytes. It is the caller's
* task to convert String passwords to bytes as appropriate.
*
* @param P The password supplied as array of bytes. It is the caller's
* task to convert String passwords to bytes as appropriate.
*/
public void init(byte[] P);
/**
* Pseudo Random Function
*
* @param M
* Input data/message etc. Together with any data supplied during
* initilization.
* @return Random bytes of hLen length. */
*
* @param M Input data/message etc. Together with any data supplied during
* initilization.
* @return Random bytes of hLen length.
*/
public byte[] doFinal(byte[] M);
/**
* Query block size of underlying algorithm/mechanism.
*
* @return block size */
*
* @return block size
*/
public int getHLen();
}