Luhn’s Algoritmo, also known as the Fórmula de Luhn or algoritmo de módulo 10, is a simple checksum formula that is widely used to validate identification numbers, especially credit card numbers. Desenvolvido pela IBM scientist Hans Peter Luhn in 1954, this algorithm helps to prevent accidental errors in data entry and is employed by various industries to ensure the integrity of numeric identifiers.
O algoritmo funciona realizando as seguintes etapas:
- Começando pelo dígito mais à direita, dobre o valor de cada segundo dígito. Se esse dobro resultar em um número maior que 9, subtraia 9 do resultado.
- Some todos os dígitos, tanto os alterados quanto os não alterados.
- If the total modulo 10 is equal to 0, then the number is valid according to Luhn’s Algorithm.
Por exemplo, considere o número 4539 1488 0343 6467:
- Dobre cada segundo dígito a partir da direita: 4, 10, 3, 18, 8, 0, 3, 12, 6, 12, 7.
- Ajuste aqueles maiores que 9: 4, 1, 3, 9, 8, 0, 3, 3, 6, 3, 7.
- Soma: 4 + 1 + 3 + 9 + 8 + 0 + 3 + 3 + 6 + 3 + 7 = 43.
- Verifique se 43 módulo 10 é igual a 0: não é, portanto o número é inválido.
Luhn’s Algorithm is particularly useful because it can be easily implemented and is effective in detecting simple errors, such as mistyped digits or transposed numbers. However, it’s important to note that while Luhn’s Algorithm can indicate that a number might be valid, it does not guarantee that the number is legitimate or has not been compromised.