Skip to content

Instantly share code, notes, and snippets.

@ieb
Created November 17, 2011 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ieb/1374825 to your computer and use it in GitHub Desktop.
Save ieb/1374825 to your computer and use it in GitHub Desktop.
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