Skip to content

Instantly share code, notes, and snippets.

@ieb
Created Nov 17, 2011
Embed
What would you like to do?
Dumb Simple Slow Luhn Test.
// A very simple Luhn check that only accepts sequence of digits as input.
// it scans the entire string with windows of 14,15,16 in width looking for a sequence that passes
// the luhn check. If it finds one it prints the original and the masked sequence, returning the
// masked sequence. If none are found, the original is returned.
// This is a dumb, slow, checker only intended to validate test sequences more quickly than doing
// manually. It outputs the progress to allow human validation.
public static String dumbSlowChecker(String test) {
char[] c = test.toCharArray();
if (c.length < 14) {
return test;
}
System.err.println("String is "+test.length());
for (int start = 0; start < c.length - 13; start++) {
for (int end = start + 14; end < (Math.min(start+16, c.length)+1); end++) {
int acc = 0;
System.err.println("From "+(end-1)+" to "+start+" (inclusive) length "+(end-start));
int n = 0;
for (int i = end - 1; i >= start; i--) {
int dv = c[i] - '0';
if (n % 2 == 1) {
dv = dv * 2;
acc += dv / 10;
acc += dv % 10;
System.err.println(i+" C = "+c[i]+" x2 "+dv/10+" "+dv%10+" Acc = "+acc);
} else {
acc += dv;
System.err.println(i+" C = "+c[i]+" x1 "+dv+" Acc = "+acc);
}
n++;
}
if (acc % 10 == 0) {
for (int i = start; i < end; i++) {
c[i] = 'X';
}
System.err.println(test);
System.err.println(new String(c));
return new String(c);
}
}
}
return test;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment