mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 07:16:17 -04:00
Tidied up Luhn checksum op
This commit is contained in:
parent
0d7874bac1
commit
0eacab5ddc
2 changed files with 11 additions and 15 deletions
|
@ -36,7 +36,6 @@ class LuhnChecksum extends Operation {
|
||||||
checksum(inputStr) {
|
checksum(inputStr) {
|
||||||
let even = false;
|
let even = false;
|
||||||
return inputStr.split("").reverse().reduce((acc, elem) => {
|
return inputStr.split("").reverse().reduce((acc, elem) => {
|
||||||
|
|
||||||
// Convert element to integer.
|
// Convert element to integer.
|
||||||
let temp = parseInt(elem, 10);
|
let temp = parseInt(elem, 10);
|
||||||
|
|
||||||
|
@ -46,7 +45,6 @@ class LuhnChecksum extends Operation {
|
||||||
|
|
||||||
// If element is in an even position
|
// If element is in an even position
|
||||||
if (even) {
|
if (even) {
|
||||||
|
|
||||||
// Double the element and add the quotient and remainder together.
|
// Double the element and add the quotient and remainder together.
|
||||||
temp = 2 * elem;
|
temp = 2 * elem;
|
||||||
temp = Math.floor(temp/10) + (temp % 10);
|
temp = Math.floor(temp/10) + (temp % 10);
|
||||||
|
@ -54,7 +52,6 @@ class LuhnChecksum extends Operation {
|
||||||
|
|
||||||
even = !even;
|
even = !even;
|
||||||
return acc + temp;
|
return acc + temp;
|
||||||
|
|
||||||
}, 0) % 10;
|
}, 0) % 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,16 +61,15 @@ class LuhnChecksum extends Operation {
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
|
if (!input) return "";
|
||||||
|
|
||||||
if (!(input)) return "0";
|
const checkSum = this.checksum(input);
|
||||||
|
let checkDigit = this.checksum(input + "0");
|
||||||
|
checkDigit = checkDigit === 0 ? 0 : (10-checkDigit);
|
||||||
|
|
||||||
const checkSum = this.checksum(input).toString();
|
return `Checksum: ${checkSum}
|
||||||
|
Checkdigit: ${checkDigit}
|
||||||
let checkDigit = this.checksum(input+"0");
|
Luhn Validated String: ${input + "" + checkDigit}`;
|
||||||
|
|
||||||
checkDigit = (checkDigit === 0 ? 0 : (10-checkDigit)).toString();
|
|
||||||
|
|
||||||
return "Checksum: " + checkSum + "\n\nCheckdigit: " + checkDigit + "\n\nLuhn Validated String: "+ input + checkDigit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ TestRegister.addTests([
|
||||||
{
|
{
|
||||||
name: "Luhn Checksum on standard data",
|
name: "Luhn Checksum on standard data",
|
||||||
input: "35641709012469",
|
input: "35641709012469",
|
||||||
expectedOutput: "Checksum: 7\n\nCheckdigit: 0\n\nLuhn Validated String: 356417090124690",
|
expectedOutput: "Checksum: 7\nCheckdigit: 0\nLuhn Validated String: 356417090124690",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "Luhn Checksum",
|
op: "Luhn Checksum",
|
||||||
|
@ -22,7 +22,7 @@ TestRegister.addTests([
|
||||||
{
|
{
|
||||||
name: "Luhn Checksum on standard data 2",
|
name: "Luhn Checksum on standard data 2",
|
||||||
input: "896101950123440000",
|
input: "896101950123440000",
|
||||||
expectedOutput: "Checksum: 5\n\nCheckdigit: 1\n\nLuhn Validated String: 8961019501234400001",
|
expectedOutput: "Checksum: 5\nCheckdigit: 1\nLuhn Validated String: 8961019501234400001",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "Luhn Checksum",
|
op: "Luhn Checksum",
|
||||||
|
@ -33,7 +33,7 @@ TestRegister.addTests([
|
||||||
{
|
{
|
||||||
name: "Luhn Checksum on standard data 3",
|
name: "Luhn Checksum on standard data 3",
|
||||||
input: "35726908971331",
|
input: "35726908971331",
|
||||||
expectedOutput: "Checksum: 6\n\nCheckdigit: 7\n\nLuhn Validated String: 357269089713317",
|
expectedOutput: "Checksum: 6\nCheckdigit: 7\nLuhn Validated String: 357269089713317",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "Luhn Checksum",
|
op: "Luhn Checksum",
|
||||||
|
@ -55,7 +55,7 @@ TestRegister.addTests([
|
||||||
{
|
{
|
||||||
name: "Luhn Checksum on empty data",
|
name: "Luhn Checksum on empty data",
|
||||||
input: "",
|
input: "",
|
||||||
expectedOutput: "0",
|
expectedOutput: "",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "Luhn Checksum",
|
op: "Luhn Checksum",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue