migrated to Java 8
This commit is contained in:
@ -13,6 +13,9 @@
|
||||
*/
|
||||
package de.muehlencord.shared.security;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
import static java.lang.String.valueOf;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
@ -29,7 +32,7 @@ public class Luhn {
|
||||
|
||||
// iterate from right digit to left
|
||||
for (int currentDigitPos = numberStr.length() - 1; currentDigitPos >= 0; currentDigitPos--) {
|
||||
int currentDigit = Integer.parseInt(String.valueOf(numberStr.charAt(currentDigitPos)));
|
||||
int currentDigit = parseInt(valueOf(numberStr.charAt(currentDigitPos)));
|
||||
if (doubleNextDigit) {
|
||||
currentDigit = currentDigit * 2;
|
||||
}
|
||||
|
||||
@ -4,14 +4,15 @@
|
||||
*/
|
||||
package de.muehlencord.shared.security;
|
||||
|
||||
import com.lambdaworks.crypto.SCryptUtil;
|
||||
import static com.lambdaworks.crypto.SCryptUtil.check;
|
||||
import static com.lambdaworks.crypto.SCryptUtil.scrypt;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import static org.apache.log4j.Logger.getLogger;
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
@ -19,7 +20,7 @@ import org.apache.log4j.Logger;
|
||||
public abstract class PasswordUtil {
|
||||
|
||||
/** logging object */
|
||||
private final static Logger logger = Logger.getLogger(PasswordUtil.class);
|
||||
private final static Logger logger = getLogger(PasswordUtil.class);
|
||||
|
||||
|
||||
/** SCrypt CPU cost parameter */
|
||||
@ -229,7 +230,7 @@ public abstract class PasswordUtil {
|
||||
* @return the crypted password string
|
||||
*/
|
||||
public static String getScryptHash(String plainPassword) {
|
||||
return SCryptUtil.scrypt(plainPassword, scryptCpuCostParameter, scryptMemCostParameter, scryptParallelizationParameter);
|
||||
return scrypt(plainPassword, scryptCpuCostParameter, scryptMemCostParameter, scryptParallelizationParameter);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -240,6 +241,6 @@ public abstract class PasswordUtil {
|
||||
* @return true, if the encrypted string of the given plain password matches the provided crypted password
|
||||
*/
|
||||
public static boolean validateScryptHash(String plainPassword, String hashedPassword) {
|
||||
return SCryptUtil.check(plainPassword, hashedPassword);
|
||||
return check(plainPassword, hashedPassword);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,6 +13,8 @@
|
||||
*/
|
||||
package de.muehlencord.shared.security;
|
||||
|
||||
import static de.muehlencord.shared.security.Luhn.computeCheckDigit;
|
||||
import static de.muehlencord.shared.security.Luhn.validateNumber;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@ -28,23 +30,23 @@ public class LuhnTest {
|
||||
@Test
|
||||
public void testComputeCheckDigit() {
|
||||
String testString = "7992739871";
|
||||
int checkNumber = Luhn.computeCheckDigit(testString, false);
|
||||
int checkNumber = computeCheckDigit(testString, false);
|
||||
System.out.println(checkNumber);
|
||||
assertTrue(checkNumber == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateNumber() {
|
||||
assertFalse(Luhn.validateNumber("79927398710"));
|
||||
assertFalse(Luhn.validateNumber("79927398711"));
|
||||
assertFalse(Luhn.validateNumber("79927398712"));
|
||||
assertTrue(Luhn.validateNumber("79927398713"));
|
||||
assertFalse(Luhn.validateNumber("79927398714"));
|
||||
assertFalse(Luhn.validateNumber("79927398715"));
|
||||
assertFalse(Luhn.validateNumber("79927398716"));
|
||||
assertFalse(Luhn.validateNumber("79927398717"));
|
||||
assertFalse(Luhn.validateNumber("79927398718"));
|
||||
assertFalse(Luhn.validateNumber("79927398719"));
|
||||
assertFalse(validateNumber("79927398710"));
|
||||
assertFalse(validateNumber("79927398711"));
|
||||
assertFalse(validateNumber("79927398712"));
|
||||
assertTrue(validateNumber("79927398713"));
|
||||
assertFalse(validateNumber("79927398714"));
|
||||
assertFalse(validateNumber("79927398715"));
|
||||
assertFalse(validateNumber("79927398716"));
|
||||
assertFalse(validateNumber("79927398717"));
|
||||
assertFalse(validateNumber("79927398718"));
|
||||
assertFalse(validateNumber("79927398719"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5,6 +5,8 @@
|
||||
|
||||
package de.muehlencord.shared.security;
|
||||
|
||||
import static de.muehlencord.shared.security.PasswordUtil.getScryptHash;
|
||||
import static de.muehlencord.shared.security.PasswordUtil.validateScryptHash;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
@ -133,8 +135,8 @@ public class PasswordUtilTest {
|
||||
*/
|
||||
@Test
|
||||
public void testGetScryptHash() {
|
||||
String hash1 = PasswordUtil.getScryptHash("secret");
|
||||
String hash2 = PasswordUtil.getScryptHash("secret");
|
||||
String hash1 = getScryptHash("secret");
|
||||
String hash2 = getScryptHash("secret");
|
||||
System.out.println (hash1);
|
||||
System.out.println (hash2);
|
||||
assertNotNull (hash1);
|
||||
@ -151,11 +153,11 @@ public class PasswordUtilTest {
|
||||
*/
|
||||
@Test
|
||||
public void testValidateScryptHash() {
|
||||
String hash1 = PasswordUtil.getScryptHash("secret");
|
||||
String hash2 = PasswordUtil.getScryptHash("secret");
|
||||
assertTrue ("hash must match if correct password is given",PasswordUtil.validateScryptHash("secret", hash1));
|
||||
assertTrue ("hash must match if correct password is given", PasswordUtil.validateScryptHash("secret", hash2));
|
||||
assertFalse ("hash must not match if wrong password is given", PasswordUtil.validateScryptHash("secret2", hash1));
|
||||
String hash1 = getScryptHash("secret");
|
||||
String hash2 = getScryptHash("secret");
|
||||
assertTrue ("hash must match if correct password is given",validateScryptHash("secret", hash1));
|
||||
assertTrue ("hash must match if correct password is given", validateScryptHash("secret", hash2));
|
||||
assertFalse ("hash must not match if wrong password is given", validateScryptHash("secret2", hash1));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user