Code Refactor - Whitespace Refactor

This commit is contained in:
Xephi
2014-08-08 23:14:56 +02:00
parent ec7ac60340
commit afc1ea9111
114 changed files with 8277 additions and 7103 deletions
@@ -23,14 +23,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class BinTools
{
public class BinTools {
public static final String hex = "0123456789ABCDEF";
/**
@@ -41,15 +41,12 @@ public class BinTools
* @return Hexadecimal representation of b. Uppercase A-F, two characters
* per byte. Empty string on <code>null</code> input.
*/
public static String bin2hex(final byte[] b)
{
if (b == null)
{
public static String bin2hex(final byte[] b) {
if (b == null) {
return "";
}
StringBuffer sb = new StringBuffer(2 * b.length);
for (int i = 0; i < b.length; i++)
{
for (int i = 0; i < b.length; i++) {
int v = (256 + b[i]) % 256;
sb.append(hex.charAt((v / 16) & 15));
sb.append(hex.charAt((v % 16) & 15));
@@ -61,28 +58,23 @@ public class BinTools
* Convert hex string to array of bytes.
*
* @param s
* String containing hexadecimal digits. May be <code>null</code>.
* On odd length leading zero will be assumed.
* String containing hexadecimal digits. May be <code>null</code>
* . On odd length leading zero will be assumed.
* @return Array on bytes, non-<code>null</code>.
* @throws IllegalArgumentException
* when string contains non-hex character
*/
public static byte[] hex2bin(final String s)
{
public static byte[] hex2bin(final String s) {
String m = s;
if (s == null)
{
if (s == null) {
// Allow empty input string.
m = "";
}
else if (s.length() % 2 != 0)
{
} else if (s.length() % 2 != 0) {
// Assume leading zero for odd string length
m = "0" + s;
}
byte r[] = new byte[m.length() / 2];
for (int i = 0, n = 0; i < m.length(); n++)
{
for (int i = 0, n = 0; i < m.length(); n++) {
char h = m.charAt(i++);
char l = m.charAt(i++);
r[n] = (byte) (hex2bin(h) * 16 + hex2bin(l));
@@ -99,18 +91,14 @@ public class BinTools
* @throws IllegalArgumentException
* on non-hex character
*/
public static int hex2bin(char c)
{
if (c >= '0' && c <= '9')
{
public static int hex2bin(char c) {
if (c >= '0' && c <= '9') {
return (c - '0');
}
if (c >= 'A' && c <= 'F')
{
if (c >= 'A' && c <= 'F') {
return (c - 'A' + 10);
}
if (c >= 'a' && c <= 'f')
{
if (c >= 'a' && c <= 'f') {
return (c - 'a' + 10);
}
throw new IllegalArgumentException(
@@ -118,19 +106,16 @@ public class BinTools
+ "'");
}
public static void main(String[] args)
{
public static void main(String[] args) {
byte b[] = new byte[256];
byte bb = 0;
for (int i = 0; i < 256; i++)
{
for (int i = 0; i < 256; i++) {
b[i] = bb++;
}
String s = bin2hex(b);
byte c[] = hex2bin(s);
String t = bin2hex(c);
if (!s.equals(t))
{
if (!s.equals(t)) {
throw new AssertionError("Mismatch");
}
}
@@ -34,14 +34,14 @@ import javax.crypto.spec.SecretKeySpec;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class MacBasedPRF implements PRF
{
public class MacBasedPRF implements PRF {
protected Mac mac;
protected int hLen;
@@ -54,57 +54,41 @@ public class MacBasedPRF implements PRF
* @param macAlgorithm
* Mac algorithm to use, i.e. HMacSHA1 or HMacMD5.
*/
public MacBasedPRF(String macAlgorithm)
{
public MacBasedPRF(String macAlgorithm) {
this.macAlgorithm = macAlgorithm;
try
{
try {
mac = Mac.getInstance(macAlgorithm);
hLen = mac.getMacLength();
}
catch (NoSuchAlgorithmException e)
{
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public MacBasedPRF(String macAlgorithm, String provider)
{
public MacBasedPRF(String macAlgorithm, String provider) {
this.macAlgorithm = macAlgorithm;
try
{
try {
mac = Mac.getInstance(macAlgorithm, provider);
hLen = mac.getMacLength();
}
catch (NoSuchAlgorithmException e)
{
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
catch (NoSuchProviderException e)
{
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
}
}
public byte[] doFinal(byte[] M)
{
public byte[] doFinal(byte[] M) {
byte[] r = mac.doFinal(M);
return r;
}
public int getHLen()
{
public int getHLen() {
return hLen;
}
public void init(byte[] P)
{
try
{
public void init(byte[] P) {
try {
mac.init(new SecretKeySpec(P, macAlgorithm));
}
catch (InvalidKeyException e)
{
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
}
@@ -24,14 +24,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public interface PBKDF2
{
public interface PBKDF2 {
/**
* Convert String-based input to internal byte array, then invoke PBKDF2.
* Desired key length defaults to Pseudo Random Function block size.
@@ -60,8 +60,8 @@ public interface PBKDF2
*
* @param inputPassword
* Candidate password to compute the derived key for.
* @return <code>true</code> password match; <code>false</code>
* incorrect password
* @return <code>true</code> password match; <code>false</code> incorrect
* password
*/
public abstract boolean verifyKey(String inputPassword);
@@ -61,15 +61,15 @@ import java.security.SecureRandom;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898</a>
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class PBKDF2Engine implements PBKDF2
{
public class PBKDF2Engine implements PBKDF2 {
protected PBKDF2Parameters parameters;
protected PRF prf;
@@ -78,8 +78,7 @@ public class PBKDF2Engine implements PBKDF2
* Constructor for PBKDF2 implementation object. PBKDF2 parameters must be
* passed later.
*/
public PBKDF2Engine()
{
public PBKDF2Engine() {
this.parameters = null;
prf = null;
}
@@ -92,8 +91,7 @@ public class PBKDF2Engine implements PBKDF2
* @param parameters
* Data holder for iteration count, method to use et cetera.
*/
public PBKDF2Engine(PBKDF2Parameters parameters)
{
public PBKDF2Engine(PBKDF2Parameters parameters) {
this.parameters = parameters;
prf = null;
}
@@ -108,44 +106,33 @@ public class PBKDF2Engine implements PBKDF2
* @param prf
* Supply customer Pseudo Random Function.
*/
public PBKDF2Engine(PBKDF2Parameters parameters, PRF prf)
{
public PBKDF2Engine(PBKDF2Parameters parameters, PRF prf) {
this.parameters = parameters;
this.prf = prf;
}
public byte[] deriveKey(String inputPassword)
{
public byte[] deriveKey(String inputPassword) {
return deriveKey(inputPassword, 0);
}
public byte[] deriveKey(String inputPassword, int dkLen)
{
public byte[] deriveKey(String inputPassword, int dkLen) {
byte[] r = null;
byte P[] = null;
String charset = parameters.getHashCharset();
if (inputPassword == null)
{
if (inputPassword == null) {
inputPassword = "";
}
try
{
if (charset == null)
{
try {
if (charset == null) {
P = inputPassword.getBytes();
}
else
{
} else {
P = inputPassword.getBytes(charset);
}
}
catch (UnsupportedEncodingException e)
{
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
assertPRF(P);
if (dkLen == 0)
{
if (dkLen == 0) {
dkLen = prf.getHLen();
}
r = PBKDF2(prf, parameters.getSalt(), parameters.getIterationCount(),
@@ -153,23 +140,18 @@ public class PBKDF2Engine implements PBKDF2
return r;
}
public boolean verifyKey(String inputPassword)
{
public boolean verifyKey(String inputPassword) {
byte[] referenceKey = getParameters().getDerivedKey();
if (referenceKey == null || referenceKey.length == 0)
{
if (referenceKey == null || referenceKey.length == 0) {
return false;
}
byte[] inputKey = deriveKey(inputPassword, referenceKey.length);
if (inputKey == null || inputKey.length != referenceKey.length)
{
if (inputKey == null || inputKey.length != referenceKey.length) {
return false;
}
for (int i = 0; i < inputKey.length; i++)
{
if (inputKey[i] != referenceKey[i])
{
for (int i = 0; i < inputKey.length; i++) {
if (inputKey[i] != referenceKey[i]) {
return false;
}
}
@@ -183,17 +165,14 @@ public class PBKDF2Engine implements PBKDF2
* @param P
* User-supplied candidate password as array of bytes.
*/
protected void assertPRF(byte[] P)
{
if (prf == null)
{
protected void assertPRF(byte[] P) {
if (prf == null) {
prf = new MacBasedPRF(parameters.getHashAlgorithm());
}
prf.init(P);
}
public PRF getPseudoRandomFunction()
{
public PRF getPseudoRandomFunction() {
return prf;
}
@@ -211,10 +190,8 @@ public class PBKDF2Engine implements PBKDF2
* desired length of derived key.
* @return internal byte array
*/
protected byte[] PBKDF2(PRF prf, byte[] S, int c, int dkLen)
{
if (S == null)
{
protected byte[] PBKDF2(PRF prf, byte[] S, int c, int dkLen) {
if (S == null) {
S = new byte[0];
}
int hLen = prf.getHLen();
@@ -222,13 +199,11 @@ public class PBKDF2Engine implements PBKDF2
int r = dkLen - (l - 1) * hLen;
byte T[] = new byte[l * hLen];
int ti_offset = 0;
for (int i = 1; i <= l; i++)
{
for (int i = 1; i <= l; i++) {
_F(T, ti_offset, prf, S, c, i);
ti_offset += hLen;
}
if (r < hLen)
{
if (r < hLen) {
// Incomplete last block
byte DK[] = new byte[dkLen];
System.arraycopy(T, 0, DK, 0, dkLen);
@@ -240,16 +215,15 @@ public class PBKDF2Engine implements PBKDF2
/**
* Integer division with ceiling function.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step 2.</a>
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 2.</a>
* @param a
* @param b
* @return ceil(a/b)
*/
protected int ceil(int a, int b)
{
protected int ceil(int a, int b) {
int m = 0;
if (a % b > 0)
{
if (a % b > 0) {
m = 1;
}
return a / b + m;
@@ -258,7 +232,8 @@ public class PBKDF2Engine implements PBKDF2
/**
* Function F.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step 3.</a>
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
* @param dest
* Destination byte buffer
* @param offset
@@ -272,8 +247,7 @@ public class PBKDF2Engine implements PBKDF2
* @param blockIndex
*/
protected void _F(byte[] dest, int offset, PRF prf, byte[] S, int c,
int blockIndex)
{
int blockIndex) {
int hLen = prf.getHLen();
byte U_r[] = new byte[hLen];
@@ -282,8 +256,7 @@ public class PBKDF2Engine implements PBKDF2
System.arraycopy(S, 0, U_i, 0, S.length);
INT(U_i, S.length, blockIndex);
for (int i = 0; i < c; i++)
{
for (int i = 0; i < c; i++) {
U_i = prf.doFinal(U_i);
xor(U_r, U_i);
}
@@ -297,10 +270,8 @@ public class PBKDF2Engine implements PBKDF2
* @param dest
* @param src
*/
protected void xor(byte[] dest, byte[] src)
{
for (int i = 0; i < dest.length; i++)
{
protected void xor(byte[] dest, byte[] src) {
for (int i = 0; i < dest.length; i++) {
dest[i] ^= src[i];
}
}
@@ -308,31 +279,28 @@ public class PBKDF2Engine implements PBKDF2
/**
* Four-octet encoding of the integer i, most significant octet first.
*
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step 3.</a>
* @see <a href="http://tools.ietf.org/html/rfc2898">RFC 2898 5.2 Step
* 3.</a>
* @param dest
* @param offset
* @param i
*/
protected void INT(byte[] dest, int offset, int i)
{
protected void INT(byte[] dest, int offset, int i) {
dest[offset + 0] = (byte) (i / (256 * 256 * 256));
dest[offset + 1] = (byte) (i / (256 * 256));
dest[offset + 2] = (byte) (i / (256));
dest[offset + 3] = (byte) (i);
}
public PBKDF2Parameters getParameters()
{
public PBKDF2Parameters getParameters() {
return parameters;
}
public void setParameters(PBKDF2Parameters parameters)
{
public void setParameters(PBKDF2Parameters parameters) {
this.parameters = parameters;
}
public void setPseudoRandomFunction(PRF prf)
{
public void setPseudoRandomFunction(PRF prf) {
this.prf = prf;
}
@@ -352,22 +320,18 @@ public class PBKDF2Engine implements PBKDF2
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws IOException,
NoSuchAlgorithmException
{
NoSuchAlgorithmException {
String password = "password";
String candidate = null;
PBKDF2Formatter formatter = new PBKDF2HexFormatter();
if (args.length >= 1)
{
if (args.length >= 1) {
password = args[0];
}
if (args.length >= 2)
{
if (args.length >= 2) {
candidate = args[1];
}
if (candidate == null)
{
if (candidate == null) {
// Creation mode
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[8];
@@ -379,15 +343,12 @@ public class PBKDF2Engine implements PBKDF2
p.setDerivedKey(e.deriveKey(password));
candidate = formatter.toString(p);
System.out.println(candidate);
}
else
{
} else {
// Verification mode
PBKDF2Parameters p = new PBKDF2Parameters();
p.setHashAlgorithm("HmacSHA1");
p.setHashCharset("ISO-8859-1");
if (formatter.fromString(p, candidate))
{
if (formatter.fromString(p, candidate)) {
throw new IllegalArgumentException(
"Candidate data does not have correct format (\""
+ candidate + "\")");
@@ -24,14 +24,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public interface PBKDF2Formatter
{
public interface PBKDF2Formatter {
/**
* Convert parameters to String.
*
@@ -24,24 +24,21 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class PBKDF2HexFormatter implements PBKDF2Formatter
{
public boolean fromString(PBKDF2Parameters p, String s)
{
if (p == null || s == null)
{
public class PBKDF2HexFormatter implements PBKDF2Formatter {
public boolean fromString(PBKDF2Parameters p, String s) {
if (p == null || s == null) {
return true;
}
String[] p123 = s.split(":");
if (p123 == null || p123.length != 3)
{
if (p123 == null || p123.length != 3) {
return true;
}
@@ -55,8 +52,7 @@ public class PBKDF2HexFormatter implements PBKDF2Formatter
return false;
}
public String toString(PBKDF2Parameters p)
{
public String toString(PBKDF2Parameters p) {
String s = BinTools.bin2hex(p.getSalt()) + ":"
+ String.valueOf(p.getIterationCount()) + ":"
+ BinTools.bin2hex(p.getDerivedKey());
@@ -29,14 +29,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class PBKDF2Parameters
{
public class PBKDF2Parameters {
protected byte[] salt;
protected int iterationCount;
@@ -56,8 +56,7 @@ public class PBKDF2Parameters
* character set and 1000 for iteration count.
*
*/
public PBKDF2Parameters()
{
public PBKDF2Parameters() {
this.hashAlgorithm = null;
this.hashCharset = "UTF-8";
this.salt = null;
@@ -73,14 +72,12 @@ public class PBKDF2Parameters
* @param hashCharset
* for example UTF-8
* @param salt
* Salt as byte array, may be <code>null</code> (not
* recommended)
* Salt as byte array, may be <code>null</code> (not recommended)
* @param iterationCount
* Number of iterations to execute. Recommended value 1000.
*/
public PBKDF2Parameters(String hashAlgorithm, String hashCharset,
byte[] salt, int iterationCount)
{
byte[] salt, int iterationCount) {
this.hashAlgorithm = hashAlgorithm;
this.hashCharset = hashCharset;
this.salt = salt;
@@ -96,16 +93,14 @@ public class PBKDF2Parameters
* @param hashCharset
* for example UTF-8
* @param salt
* Salt as byte array, may be <code>null</code> (not
* recommended)
* Salt as byte array, may be <code>null</code> (not recommended)
* @param iterationCount
* Number of iterations to execute. Recommended value 1000.
* @param derivedKey
* Convenience data holder, not used during computation.
*/
public PBKDF2Parameters(String hashAlgorithm, String hashCharset,
byte[] salt, int iterationCount, byte[] derivedKey)
{
byte[] salt, int iterationCount, byte[] derivedKey) {
this.hashAlgorithm = hashAlgorithm;
this.hashCharset = hashCharset;
this.salt = salt;
@@ -113,53 +108,43 @@ public class PBKDF2Parameters
this.derivedKey = derivedKey;
}
public int getIterationCount()
{
public int getIterationCount() {
return iterationCount;
}
public void setIterationCount(int iterationCount)
{
public void setIterationCount(int iterationCount) {
this.iterationCount = iterationCount;
}
public byte[] getSalt()
{
public byte[] getSalt() {
return salt;
}
public void setSalt(byte[] salt)
{
public void setSalt(byte[] salt) {
this.salt = salt;
}
public byte[] getDerivedKey()
{
public byte[] getDerivedKey() {
return derivedKey;
}
public void setDerivedKey(byte[] derivedKey)
{
public void setDerivedKey(byte[] derivedKey) {
this.derivedKey = derivedKey;
}
public String getHashAlgorithm()
{
public String getHashAlgorithm() {
return hashAlgorithm;
}
public void setHashAlgorithm(String hashAlgorithm)
{
public void setHashAlgorithm(String hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
public String getHashCharset()
{
public String getHashCharset() {
return hashCharset;
}
public void setHashCharset(String hashCharset)
{
public void setHashCharset(String hashCharset) {
this.hashCharset = hashCharset;
}
}
@@ -24,14 +24,14 @@ package fr.xephi.authme.security.pbkdf2;
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public interface PRF
{
public interface PRF {
/**
* Initialize this instance with the user-supplied password.
*