mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-22 15:56:16 -04:00
Merge branch 'n1073645-Luhn'
This commit is contained in:
commit
1b19d20d0c
2 changed files with 47 additions and 10 deletions
|
@ -23,19 +23,19 @@ class LuhnChecksum extends Operation {
|
||||||
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 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.infoURL = "https://wikipedia.org/wiki/Luhn_algorithm";
|
this.infoURL = "https://wikipedia.org/wiki/Luhn_algorithm";
|
||||||
this.inputType = "string";
|
this.inputType = "string";
|
||||||
this.outputType = "number";
|
this.outputType = "string";
|
||||||
this.args = [];
|
this.args = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} input
|
* Generates the Luhn Checksum from the input.
|
||||||
* @param {Object[]} args
|
*
|
||||||
|
* @param {string} inputStr
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
checksum(inputStr) {
|
||||||
let even = false;
|
let even = false;
|
||||||
return input.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);
|
||||||
|
|
||||||
|
@ -45,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);
|
||||||
|
@ -53,10 +52,26 @@ class LuhnChecksum extends Operation {
|
||||||
|
|
||||||
even = !even;
|
even = !even;
|
||||||
return acc + temp;
|
return acc + temp;
|
||||||
|
|
||||||
}, 0) % 10;
|
}, 0) % 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
run(input, args) {
|
||||||
|
if (!input) return "";
|
||||||
|
|
||||||
|
const checkSum = this.checksum(input);
|
||||||
|
let checkDigit = this.checksum(input + "0");
|
||||||
|
checkDigit = checkDigit === 0 ? 0 : (10-checkDigit);
|
||||||
|
|
||||||
|
return `Checksum: ${checkSum}
|
||||||
|
Checkdigit: ${checkDigit}
|
||||||
|
Luhn Validated String: ${input + "" + checkDigit}`;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LuhnChecksum;
|
export default LuhnChecksum;
|
||||||
|
|
|
@ -11,7 +11,29 @@ TestRegister.addTests([
|
||||||
{
|
{
|
||||||
name: "Luhn Checksum on standard data",
|
name: "Luhn Checksum on standard data",
|
||||||
input: "35641709012469",
|
input: "35641709012469",
|
||||||
expectedOutput: "7",
|
expectedOutput: "Checksum: 7\nCheckdigit: 0\nLuhn Validated String: 356417090124690",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Luhn Checksum",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Luhn Checksum on standard data 2",
|
||||||
|
input: "896101950123440000",
|
||||||
|
expectedOutput: "Checksum: 5\nCheckdigit: 1\nLuhn Validated String: 8961019501234400001",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Luhn Checksum",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Luhn Checksum on standard data 3",
|
||||||
|
input: "35726908971331",
|
||||||
|
expectedOutput: "Checksum: 6\nCheckdigit: 7\nLuhn Validated String: 357269089713317",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "Luhn Checksum",
|
op: "Luhn Checksum",
|
||||||
|
@ -33,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