Prepare the project for javadocs

This commit is contained in:
Gabriele C
2015-11-21 01:27:06 +01:00
parent adcd70b91d
commit 118c79401a
168 changed files with 4375 additions and 422 deletions
@@ -2,6 +2,8 @@ package fr.xephi.authme.security;
import org.apache.commons.lang.ObjectUtils.Null;
/**
*/
public enum HashAlgorithm {
MD5(fr.xephi.authme.security.crypts.MD5.class),
@@ -35,10 +37,18 @@ public enum HashAlgorithm {
Class<?> classe;
/**
* Constructor for HashAlgorithm.
* @param classe Class<?>
*/
HashAlgorithm(Class<?> classe) {
this.classe = classe;
}
/**
* Method getclasse.
* @return Class<?>
*/
public Class<?> getclasse() {
return classe;
}
@@ -15,11 +15,19 @@ import fr.xephi.authme.security.crypts.BCRYPT;
import fr.xephi.authme.security.crypts.EncryptionMethod;
import fr.xephi.authme.settings.Settings;
/**
*/
public class PasswordSecurity {
private static SecureRandom rnd = new SecureRandom();
public static HashMap<String, String> userSalt = new HashMap<>();
/**
* Method createSalt.
* @param length int
* @return String
* @throws NoSuchAlgorithmException
*/
public static String createSalt(int length)
throws NoSuchAlgorithmException {
byte[] msg = new byte[40];
@@ -30,6 +38,14 @@ public class PasswordSecurity {
return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest)).substring(0, length);
}
/**
* Method getHash.
* @param alg HashAlgorithm
* @param password String
* @param playerName String
* @return String
* @throws NoSuchAlgorithmException
*/
public static String getHash(HashAlgorithm alg, String password,
String playerName) throws NoSuchAlgorithmException {
EncryptionMethod method;
@@ -125,6 +141,14 @@ public class PasswordSecurity {
return method.getHash(password, salt, playerName);
}
/**
* Method comparePasswordWithHash.
* @param password String
* @param hash String
* @param playerName String
* @return boolean
* @throws NoSuchAlgorithmException
*/
public static boolean comparePasswordWithHash(String password, String hash,
String playerName) throws NoSuchAlgorithmException {
HashAlgorithm algo = Settings.getPasswordHash;
@@ -155,6 +179,14 @@ public class PasswordSecurity {
return false;
}
/**
* Method compareWithAllEncryptionMethod.
* @param password String
* @param hash String
* @param playerName String
* @return boolean
* @throws NoSuchAlgorithmException
*/
private static boolean compareWithAllEncryptionMethod(String password,
String hash, String playerName) throws NoSuchAlgorithmException {
for (HashAlgorithm algo : HashAlgorithm.values()) {
@@ -6,6 +6,7 @@ import java.util.Random;
/**
*
* @author Xephi59
* @version $Revision: 1.0 $
*/
public class RandomString {
@@ -22,6 +23,10 @@ public class RandomString {
private final char[] buf;
/**
* Constructor for RandomString.
* @param length int
*/
public RandomString(int length) {
if (length < 1)
throw new IllegalArgumentException("length < 1: " + length);
@@ -29,6 +34,10 @@ public class RandomString {
random.setSeed(Calendar.getInstance().getTimeInMillis());
}
/**
* Method nextString.
* @return String
*/
public String nextString() {
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = chars[random.nextInt(chars.length)];
@@ -92,9 +92,9 @@ 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
*/
* @return base64-encoded string * @throws IllegalArgumentException if the length is invalid */
private static String encode_base64(byte d[], int len)
throws IllegalArgumentException {
int off = 0;
@@ -133,8 +133,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;
@@ -148,9 +148,9 @@ 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
*/
* @return an array containing the decoded bytes * @throws IllegalArgumentException if maxolen is invalid */
private static byte[] decode_base64(String s, int maxolen)
throws IllegalArgumentException {
StringBuffer rs = new StringBuffer();
@@ -227,8 +227,8 @@ public class BCRYPT implements EncryptionMethod {
* @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
*/
* @return the next word of material from data */
private static int streamtoword(byte data[], int offp[]) {
int i;
int word = 0;
@@ -319,8 +319,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();
@@ -360,8 +360,8 @@ public class BCRYPT implements EncryptionMethod {
*
* @param password the password to hash
* @param salt the salt to hash with (perhaps generated using BCrypt.gensalt)
* @return the hashed password
*/
* @return the hashed password */
public static String hashpw(String password, String salt) {
BCRYPT B;
String real_salt;
@@ -417,8 +417,8 @@ public class BCRYPT implements EncryptionMethod {
* @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
*/
* @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];
@@ -439,8 +439,8 @@ public class BCRYPT implements EncryptionMethod {
*
* @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
*/
* @return an encoded salt value */
public static String gensalt(int log_rounds) {
return gensalt(log_rounds, new SecureRandom());
}
@@ -449,8 +449,8 @@ public class BCRYPT implements EncryptionMethod {
* 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
*/
* @return an encoded salt value */
public static String gensalt() {
return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS);
}
@@ -460,8 +460,8 @@ public class BCRYPT implements EncryptionMethod {
*
* @param plaintext the plaintext password to verify
* @param hashed the previously-hashed password
* @return true if the passwords match, false otherwise
*/
* @return true if the passwords match, false otherwise */
public static boolean checkpw(String plaintext, String hashed) {
return (hashed.compareTo(hashpw(plaintext, hashed)) == 0);
}
@@ -473,7 +473,8 @@ public class BCRYPT implements EncryptionMethod {
* @param text plaintext or hashed text
* @param hashed the previously-hashed password
* @param rounds number of rounds to hash the password
* @return
* @return boolean
*/
public static boolean checkpw(String text, String hashed, int rounds) {
boolean matched = false;
@@ -493,18 +494,42 @@ public class BCRYPT implements EncryptionMethod {
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return hashpw(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 {
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);
@@ -2,8 +2,19 @@ package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
/**
*/
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -12,6 +23,15 @@ public class BCRYPT2Y implements EncryptionMethod {
return (BCRYPT.hashpw(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 {
@@ -4,11 +4,22 @@ import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*/
public class CRAZYCRYPT1 implements EncryptionMethod {
protected final Charset charset = Charset.forName("UTF-8");
private static final char[] CRYPTCHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/**
* 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 {
@@ -21,13 +32,27 @@ public class CRAZYCRYPT1 implements EncryptionMethod {
return null;
}
}
/**
* 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 byteArrayToHexString.
* @param args byte[]
* @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++) {
@@ -5,8 +5,19 @@ import java.security.NoSuchAlgorithmException;
import fr.xephi.authme.security.pbkdf2.PBKDF2Engine;
import fr.xephi.authme.security.pbkdf2.PBKDF2Parameters;
/**
*/
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -17,6 +28,15 @@ public class CryptPBKDF2 implements EncryptionMethod {
return result + String.valueOf(engine.deriveKey(password, 64));
}
/**
* 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 {
@@ -7,8 +7,19 @@ import javax.xml.bind.DatatypeConverter;
import fr.xephi.authme.security.pbkdf2.PBKDF2Engine;
import fr.xephi.authme.security.pbkdf2.PBKDF2Parameters;
/**
*/
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -19,6 +30,15 @@ public class CryptPBKDF2Django implements EncryptionMethod {
return result + String.valueOf(DatatypeConverter.printBase64Binary(engine.deriveKey(password, 32)));
}
/**
* 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 {
@@ -4,20 +4,46 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -13,6 +13,8 @@ import java.security.NoSuchAlgorithmException;
* <p>
* The comparePassword is called when we need to match password (/login usually)
* </p>
* @author Gabriele
* @version $Revision: 1.0 $
*/
public interface EncryptionMethod {
@@ -21,9 +23,10 @@ public interface EncryptionMethod {
* @param salt
* (can be an other data like playerName;salt , playerName,
* etc... for customs methods)
* @return Hashing password
* @throws NoSuchAlgorithmException
*/
* @param name String
* @return Hashing password * @throws NoSuchAlgorithmException */
String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException;
@@ -31,9 +34,9 @@ public interface EncryptionMethod {
* @param hash
* @param password
* @param playerName
* @return true if password match, false else
* @throws NoSuchAlgorithmException
*/
* @return true if password match, false else * @throws NoSuchAlgorithmException */
boolean comparePassword(String hash, String password, String playerName)
throws NoSuchAlgorithmException;
@@ -6,14 +6,34 @@ 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 {
@@ -21,6 +41,12 @@ public class IPB3 implements EncryptionMethod {
return hash.equals(getHash(password, salt, playerName));
}
/**
* Method getMD5.
* @param message String
* @return String
* @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -4,14 +4,34 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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 {
@@ -19,6 +39,12 @@ public class JOOMLA implements EncryptionMethod {
return hash.equals(getMD5(password + salt) + ":" + salt);
}
/**
* Method getMD5.
* @param message String
* @return String
* @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -4,20 +4,46 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -4,14 +4,34 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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 {
@@ -19,6 +39,12 @@ public class MD5VB implements EncryptionMethod {
return hash.equals(getHash(password, line[2], ""));
}
/**
* Method getMD5.
* @param message String
* @return String
* @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -6,14 +6,34 @@ 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 {
@@ -21,6 +41,12 @@ public class MYBB implements EncryptionMethod {
return hash.equals(getHash(password, salt, playerName));
}
/**
* Method getMD5.
* @param message String
* @return String
* @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -11,11 +11,18 @@ import java.security.NoSuchAlgorithmException;
/**
* @author stefano
* @version $Revision: 1.0 $
*/
public class PHPBB implements EncryptionMethod {
private String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
/**
* Method phpbb_hash.
* @param password String
* @param salt String
* @return String
*/
public String phpbb_hash(String password, String salt) {
String random_state = salt;
StringBuilder random = new StringBuilder();
@@ -31,10 +38,23 @@ public class PHPBB implements EncryptionMethod {
return md5(password);
}
/**
* Method _hash_gensalt_private.
* @param input String
* @param itoa64 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 iteration_count_log2 int
* @return String
*/
private String _hash_gensalt_private(String input, String itoa64,
int iteration_count_log2) {
if (iteration_count_log2 < 4 || iteration_count_log2 > 31) {
@@ -48,6 +68,9 @@ public class PHPBB implements EncryptionMethod {
/**
* Encode hash
* @param input String
* @param count int
* @return String
*/
private String _hash_encode64(String input, int count) {
StringBuilder output = new StringBuilder();
@@ -70,6 +93,12 @@ public class PHPBB implements EncryptionMethod {
return output.toString();
}
/**
* Method _hash_crypt_private.
* @param password String
* @param setting String
* @return String
*/
String _hash_crypt_private(String password, String setting) {
String output = "*";
if (!setting.substring(0, 3).equals("$H$"))
@@ -91,12 +120,23 @@ public class PHPBB implements EncryptionMethod {
return output;
}
/**
* Method phpbb_check_hash.
* @param password String
* @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");
@@ -108,6 +148,11 @@ public class PHPBB implements EncryptionMethod {
}
}
/**
* Method hexToInt.
* @param ch char
* @return int
*/
static int hexToInt(char ch) {
if (ch >= '0' && ch <= '9')
return ch - '0';
@@ -117,6 +162,11 @@ public class PHPBB implements EncryptionMethod {
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) {
@@ -128,6 +178,11 @@ public class PHPBB implements EncryptionMethod {
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) {
@@ -139,12 +194,30 @@ public class PHPBB implements EncryptionMethod {
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return phpbb_hash(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 {
@@ -11,8 +11,19 @@ import javax.crypto.spec.SecretKeySpec;
import fr.xephi.authme.AuthMe;
/**
*/
public class PHPFUSION 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 {
@@ -40,6 +51,15 @@ public class PHPFUSION implements EncryptionMethod {
return digest;
}
/**
* 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 {
@@ -47,6 +67,12 @@ public class PHPFUSION implements EncryptionMethod {
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");
@@ -2,14 +2,34 @@ package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
/**
*/
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return 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 {
@@ -3,8 +3,19 @@ package fr.xephi.authme.security.crypts;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*/
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -13,6 +24,13 @@ public class ROYALAUTH implements EncryptionMethod {
return password;
}
/**
* Method hash.
* @param password String
* @param salt String
* @return String
* @throws NoSuchAlgorithmException
*/
public String hash(String password, String salt)
throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-512");
@@ -24,6 +42,15 @@ public class ROYALAUTH implements EncryptionMethod {
return sb.toString();
}
/**
* 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 {
@@ -6,14 +6,34 @@ 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 {
@@ -21,6 +41,12 @@ public class SALTED2MD5 implements EncryptionMethod {
return hash.equals(getMD5(getMD5(password) + salt));
}
/**
* Method getMD5.
* @param message String
* @return String
* @throws NoSuchAlgorithmException
*/
private static String getMD5(String message)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
@@ -6,14 +6,34 @@ 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 {
@@ -21,6 +41,12 @@ public class SALTEDSHA512 implements EncryptionMethod {
return hash.equals(getHash(password, salt, ""));
}
/**
* Method getSHA512.
* @param message String
* @return String
* @throws NoSuchAlgorithmException
*/
private static String getSHA512(String message)
throws NoSuchAlgorithmException {
MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
@@ -4,20 +4,46 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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
*/
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
@@ -4,14 +4,34 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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 {
@@ -19,6 +39,12 @@ public class SHA256 implements EncryptionMethod {
return hash.equals(getHash(password, line[2], ""));
}
/**
* Method getSHA256.
* @param message String
* @return String
* @throws NoSuchAlgorithmException
*/
private static String getSHA256(String message)
throws NoSuchAlgorithmException {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
@@ -4,20 +4,46 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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
*/
private static String getSHA512(String message)
throws NoSuchAlgorithmException {
MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
@@ -4,20 +4,46 @@ import java.math.BigInteger;
import java.security.MessageDigest;
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
*/
private static String getSHA1(String message)
throws NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
@@ -6,14 +6,34 @@ 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 {
@@ -21,6 +41,12 @@ public class WBB3 implements EncryptionMethod {
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");
@@ -2,14 +2,34 @@ package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
/**
*/
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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return BCRYPT.getDoubleHash(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 {
@@ -64,6 +64,8 @@ package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/**
*/
public class WHIRLPOOL implements EncryptionMethod {
/**
@@ -319,6 +321,7 @@ public class WHIRLPOOL implements EncryptionMethod {
* Get the hash value from the hashing state.
*
* This method uses the invariant: bufferBits < 512
* @param digest byte[]
*/
public void NESSIEfinalize(byte[] digest) {
// append a '1'-bit:
@@ -373,6 +376,11 @@ 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";
@@ -384,6 +392,15 @@ public class WHIRLPOOL implements EncryptionMethod {
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)
throws NoSuchAlgorithmException {
@@ -394,6 +411,15 @@ public class WHIRLPOOL implements EncryptionMethod {
return display(digest);
}
/**
* 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 {
@@ -6,11 +6,19 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
/**
*/
public class WORDPRESS implements EncryptionMethod {
private static final String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private SecureRandom randomGen = new SecureRandom();
/**
* Method encode64.
* @param src byte[]
* @param count int
* @return String
*/
private String encode64(byte[] src, int count) {
int i, value;
StringBuilder output = new StringBuilder();
@@ -45,6 +53,12 @@ public class WORDPRESS implements EncryptionMethod {
return output.toString();
}
/**
* Method crypt.
* @param password 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)) {
@@ -83,6 +97,11 @@ public class WORDPRESS implements EncryptionMethod {
return output;
}
/**
* Method gensaltPrivate.
* @param input byte[]
* @return String
*/
private String gensaltPrivate(byte[] input) {
String output = "$P$";
int iterationCountLog2 = 8;
@@ -91,6 +110,11 @@ public class WORDPRESS implements EncryptionMethod {
return output;
}
/**
* Method stringToUtf8.
* @param string String
* @return byte[]
*/
private byte[] stringToUtf8(String string) {
try {
return string.getBytes("UTF-8");
@@ -99,6 +123,15 @@ 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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
@@ -107,6 +140,15 @@ public class WORDPRESS implements EncryptionMethod {
return crypt(password, gensaltPrivate(stringToUtf8(new String(random))));
}
/**
* 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 {
@@ -2,8 +2,19 @@ package fr.xephi.authme.security.crypts;
import java.security.NoSuchAlgorithmException;
/**
*/
public class XAUTH 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 {
@@ -12,6 +23,15 @@ public class XAUTH implements EncryptionMethod {
return hash.substring(0, saltPos) + salt + hash.substring(saltPos);
}
/**
* 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 {
@@ -20,6 +40,11 @@ public class XAUTH implements EncryptionMethod {
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];
@@ -9,14 +9,34 @@ 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)
*/
@Override
public String getHash(String password, String salt, String name)
throws NoSuchAlgorithmException {
return getSHA256(getSHA256(password) + regmatch("\"salt\";.:..:\"(.*)\";.:.:\"hashFunc\"", 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 {
@@ -24,6 +44,12 @@ public class XF implements EncryptionMethod {
return hash.equals(regmatch("\"hash\";.:..:\"(.*)\";.:.:\"salt\"", salt));
}
/**
* Method getSHA256.
* @param password String
* @return String
* @throws NoSuchAlgorithmException
*/
public String getSHA256(String password) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(password.getBytes());
@@ -43,6 +69,12 @@ public class XF implements EncryptionMethod {
return hexString.toString();
}
/**
* Method regmatch.
* @param pattern 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);
@@ -39,9 +39,9 @@ public class BinTools {
*
* @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 "";
@@ -61,10 +61,10 @@ public class BinTools {
* @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
*/
* @return Array on bytes, non-<code>null</code>. * @throws IllegalArgumentException
* when string contains non-hex character */
public static byte[] hex2bin(final String s) {
String m = s;
if (s == null) {
@@ -88,10 +88,10 @@ public class BinTools {
*
* @param c
* 0-9, a-f, A-F allowd.
* @return 0-15
* @throws IllegalArgumentException
* on non-hex character
*/
* @return 0-15 * @throws IllegalArgumentException
* on non-hex character */
public static int hex2bin(char c) {
if (c >= '0' && c <= '9') {
return (c - '0');
@@ -105,6 +105,10 @@ public class BinTools {
throw new IllegalArgumentException("Input string may only contain hex digits, but found '" + c + "'");
}
/**
* Method main.
* @param args String[]
*/
public static void main(String[] args) {
byte b[] = new byte[256];
byte bb = 0;
@@ -65,6 +65,11 @@ public class MacBasedPRF implements PRF {
}
}
/**
* Constructor for MacBasedPRF.
* @param macAlgorithm String
* @param provider String
*/
public MacBasedPRF(String macAlgorithm, String provider) {
this.macAlgorithm = macAlgorithm;
try {
@@ -75,15 +80,31 @@ public class MacBasedPRF implements PRF {
}
}
/**
* Method doFinal.
* @param M byte[]
* @return byte[]
* @see fr.xephi.authme.security.pbkdf2.PRF#doFinal(byte[])
*/
public byte[] doFinal(byte[] M) {
byte[] r = mac.doFinal(M);
return r;
}
/**
* Method 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[])
*/
public void init(byte[] P) {
try {
mac.init(new SecretKeySpec(P, macAlgorithm));
@@ -39,8 +39,8 @@ public interface PBKDF2 {
*
* @param inputPassword
* Candidate password to compute the derived key for.
* @return internal byte array
*/
* @return internal byte array */
public abstract byte[] deriveKey(String inputPassword);
/**
@@ -50,8 +50,8 @@ public interface PBKDF2 {
* Candidate password to compute the derived key for.
* @param dkLen
* Specify desired key length
* @return internal byte array
*/
* @return internal byte array */
public abstract byte[] deriveKey(String inputPassword, int dkLen);
/**
@@ -61,16 +61,16 @@ public interface PBKDF2 {
*
* @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();
/**
@@ -83,8 +83,8 @@ public interface PBKDF2 {
/**
* Get currently set Pseudo Random Function.
*
* @return Currently set Pseudo Random Function
*/
* @return Currently set Pseudo Random Function */
public abstract PRF getPseudoRandomFunction();
/**
@@ -112,10 +112,23 @@ public class PBKDF2Engine implements PBKDF2 {
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;
@@ -140,6 +153,12 @@ public class PBKDF2Engine implements PBKDF2 {
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) {
@@ -172,6 +191,11 @@ public class PBKDF2Engine implements PBKDF2 {
prf.init(P);
}
/**
* Method getPseudoRandomFunction.
* @return PRF
* @see fr.xephi.authme.security.pbkdf2.PBKDF2#getPseudoRandomFunction()
*/
public PRF getPseudoRandomFunction() {
return prf;
}
@@ -179,7 +203,7 @@ public class PBKDF2Engine implements PBKDF2 {
/**
* Core Password Based Key Derivation Function 2.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2</a>
* @param prf
* Pseudo Random Function (i.e. HmacSHA1)
* @param S
@@ -188,8 +212,8 @@ public class PBKDF2Engine implements PBKDF2 {
* Iteration count (see RFC 2898 4.2)
* @param dkLen
* desired length of derived key.
* @return internal byte array
*/
* @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];
@@ -215,12 +239,12 @@ public class PBKDF2Engine implements PBKDF2 {
/**
* Integer division with ceiling function.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 2.</a>
* @param a
* @param b
* @return ceil(a/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) {
@@ -232,8 +256,7 @@ public class PBKDF2Engine implements PBKDF2 {
/**
* Function F.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
* @param dest
* Destination byte buffer
* @param offset
@@ -245,7 +268,8 @@ public class PBKDF2Engine implements PBKDF2 {
* @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();
@@ -279,12 +303,12 @@ public class PBKDF2Engine implements PBKDF2 {
/**
* Four-octet encoding of the integer i, most significant octet first.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
* @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));
@@ -292,14 +316,29 @@ public class PBKDF2Engine implements PBKDF2 {
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;
}
@@ -316,9 +355,9 @@ public class PBKDF2Engine implements PBKDF2 {
*
* @param args
* Supply the password as argument.
* @throws IOException
* @throws NoSuchAlgorithmException
*/
* @throws IOException * @throws NoSuchAlgorithmException */
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException {
String password = "password";
@@ -38,8 +38,8 @@ public interface PBKDF2Formatter {
*
* @param p
* Parameters object to output.
* @return String representation
*/
* @return String representation */
public abstract String toString(PBKDF2Parameters p);
/**
@@ -48,8 +48,9 @@ public interface PBKDF2Formatter {
*
* @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);
}
@@ -33,6 +33,13 @@ package fr.xephi.authme.security.pbkdf2;
*/
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)
*/
public boolean fromString(PBKDF2Parameters p, String s) {
if (p == null || s == null) {
return true;
@@ -53,6 +60,12 @@ public class PBKDF2HexFormatter implements PBKDF2Formatter {
return false;
}
/**
* Method toString.
* @param p 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;
@@ -109,42 +109,82 @@ public class PBKDF2Parameters {
this.derivedKey = derivedKey;
}
/**
* Method getIterationCount.
* @return int
*/
public int getIterationCount() {
return iterationCount;
}
/**
* Method setIterationCount.
* @param iterationCount int
*/
public void setIterationCount(int iterationCount) {
this.iterationCount = iterationCount;
}
/**
* Method getSalt.
* @return byte[]
*/
public byte[] getSalt() {
return salt;
}
/**
* Method setSalt.
* @param salt byte[]
*/
public void setSalt(byte[] salt) {
this.salt = salt;
}
/**
* Method getDerivedKey.
* @return byte[]
*/
public byte[] getDerivedKey() {
return derivedKey;
}
/**
* Method setDerivedKey.
* @param derivedKey byte[]
*/
public void setDerivedKey(byte[] derivedKey) {
this.derivedKey = derivedKey;
}
/**
* Method getHashAlgorithm.
* @return String
*/
public String getHashAlgorithm() {
return hashAlgorithm;
}
/**
* Method setHashAlgorithm.
* @param hashAlgorithm String
*/
public void setHashAlgorithm(String hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
/**
* Method getHashCharset.
* @return String
*/
public String getHashCharset() {
return hashCharset;
}
/**
* Method setHashCharset.
* @param hashCharset String
*/
public void setHashCharset(String hashCharset) {
this.hashCharset = hashCharset;
}
@@ -48,14 +48,14 @@ public interface PRF {
* @param M
* Input data/message etc. Together with any data supplied during
* initilization.
* @return Random bytes of hLen length.
*/
* @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();
}