mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
Updated luhn checksum to to work with base 2-36
This commit is contained in:
parent
d635cca210
commit
802c576c60
1 changed files with 36 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
||||||
/**
|
/**
|
||||||
* @author n1073645 [n1073645@gmail.com]
|
* @author n1073645 [n1073645@gmail.com]
|
||||||
|
* @author k3ach [k3ach@proton.me]
|
||||||
* @copyright Crown Copyright 2020
|
* @copyright Crown Copyright 2020
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
@ -20,11 +21,17 @@ class LuhnChecksum extends Operation {
|
||||||
|
|
||||||
this.name = "Luhn Checksum";
|
this.name = "Luhn Checksum";
|
||||||
this.module = "Default";
|
this.module = "Default";
|
||||||
this.description = "The Luhn algorithm, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers and Canadian Social Insurance Numbers.";
|
this.description = "The Luhn mod N algorithm is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of values in any even-numbered base. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of letters and digits or any arbitrary set of N characters where N is divisible by 2.";
|
||||||
this.infoURL = "https://wikipedia.org/wiki/Luhn_algorithm";
|
this.infoURL = "https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm";
|
||||||
this.inputType = "string";
|
this.inputType = "string";
|
||||||
this.outputType = "string";
|
this.outputType = "string";
|
||||||
this.args = [];
|
this.args = [
|
||||||
|
{
|
||||||
|
"name": "Radix",
|
||||||
|
"type": "number",
|
||||||
|
"value": 10
|
||||||
|
}
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,26 +40,27 @@ class LuhnChecksum extends Operation {
|
||||||
* @param {string} inputStr
|
* @param {string} inputStr
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
checksum(inputStr) {
|
checksum(inputStr, radix = 10) {
|
||||||
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 an integer based on the provided radix.
|
||||||
let temp = parseInt(elem, 10);
|
let temp = parseInt(elem, radix);
|
||||||
|
|
||||||
// If element is not an integer.
|
// If element is not a valid number in the given radix.
|
||||||
if (isNaN(temp))
|
if (isNaN(temp)) {
|
||||||
throw new OperationError("Character: " + elem + " is not a digit.");
|
throw new Error("Character: " + elem + " is not valid in radix " + radix + ".");
|
||||||
|
}
|
||||||
|
|
||||||
// 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 sum the quotient and remainder.
|
||||||
temp = 2 * elem;
|
temp = 2 * temp;
|
||||||
temp = Math.floor(temp/10) + (temp % 10);
|
temp = Math.floor(temp / radix) + (temp % radix);
|
||||||
}
|
}
|
||||||
|
|
||||||
even = !even;
|
even = !even;
|
||||||
return acc + temp;
|
return acc + temp;
|
||||||
}, 0) % 10;
|
}, 0) % radix; // Use radix as the modulus base
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,9 +71,20 @@ class LuhnChecksum extends Operation {
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
if (!input) return "";
|
if (!input) return "";
|
||||||
|
|
||||||
const checkSum = this.checksum(input);
|
let radix = args[0];
|
||||||
let checkDigit = this.checksum(input + "0");
|
|
||||||
checkDigit = checkDigit === 0 ? 0 : (10-checkDigit);
|
if (radix < 2 || radix > 36) {
|
||||||
|
throw new OperationError("Error: Radix argument must be between 2 and 36");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (radix % 2 != 0) {
|
||||||
|
throw new OperationError("Error: Radix argument must be divisible by 2");
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkSum = this.checksum(input, radix).toString(radix);
|
||||||
|
let checkDigit = this.checksum(input + "0", radix);
|
||||||
|
checkDigit = checkDigit === 0 ? 0 : (radix - checkDigit);
|
||||||
|
checkDigit = checkDigit.toString(radix)
|
||||||
|
|
||||||
return `Checksum: ${checkSum}
|
return `Checksum: ${checkSum}
|
||||||
Checkdigit: ${checkDigit}
|
Checkdigit: ${checkDigit}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue