added Luhn algorithm / validator
This commit is contained in:
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* File: $$RCSfile$$
|
||||
*
|
||||
* Copyright (c) 2011 by Wincor Nixdorf,
|
||||
* Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
|
||||
*
|
||||
* This software is the confidential and proprietary information
|
||||
* of Wincor Nixdorf.
|
||||
*
|
||||
* You shall not disclose such confidential information and shall
|
||||
* use it only in accordance with the terms of the license agreement
|
||||
* you entered into with Wincor Nixdorf.
|
||||
*/
|
||||
package de.muehlencord.shared.security;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class Luhn {
|
||||
|
||||
public static boolean validateNumber(final String numberStr) {
|
||||
return (computeCheckDigit(numberStr, true) == 0);
|
||||
}
|
||||
|
||||
public static int computeCheckDigit(final String numberStr, final boolean isCheckDigitAttached) {
|
||||
boolean doubleNextDigit = !isCheckDigitAttached;
|
||||
int sum = 0;
|
||||
|
||||
// iterate from right digit to left
|
||||
for (int currentDigitPos = numberStr.length() - 1; currentDigitPos >= 0; currentDigitPos--) {
|
||||
int currentDigit = Integer.parseInt(String.valueOf(numberStr.charAt(currentDigitPos)));
|
||||
if (doubleNextDigit) {
|
||||
currentDigit = currentDigit * 2;
|
||||
}
|
||||
sum += singleDigitSum(currentDigit);
|
||||
doubleNextDigit = !doubleNextDigit;
|
||||
}
|
||||
|
||||
if ((sum % 10) > 0) {
|
||||
return (10 - (sum % 10));
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static int singleDigitSum(final int value) {
|
||||
if (value < 10) {
|
||||
return value;
|
||||
} else {
|
||||
return singleDigitSum((value / 10) + (value % 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* History:
|
||||
*
|
||||
* $$Log$$
|
||||
*
|
||||
*/
|
||||
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* File: $$RCSfile$$
|
||||
*
|
||||
* Copyright (c) 2011 by Wincor Nixdorf,
|
||||
* Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
|
||||
*
|
||||
* This software is the confidential and proprietary information
|
||||
* of Wincor Nixdorf.
|
||||
*
|
||||
* You shall not disclose such confidential information and shall
|
||||
* use it only in accordance with the terms of the license agreement
|
||||
* you entered into with Wincor Nixdorf.
|
||||
*/
|
||||
package de.muehlencord.shared.security;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class LuhnTest {
|
||||
|
||||
public LuhnTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputeCheckDigit() {
|
||||
String testString = "7992739871";
|
||||
int checkNumber = Luhn.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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* History:
|
||||
*
|
||||
* $$Log$$
|
||||
*
|
||||
*/
|
||||
Reference in New Issue
Block a user