Skip to content

Instantly share code, notes, and snippets.

@fitorec
Last active May 2, 2022 08:48
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 fitorec/82a3e27fae3bab709a07c19c71c3a8d4 to your computer and use it in GitHub Desktop.
Save fitorec/82a3e27fae3bab709a07c19c71c3a8d4 to your computer and use it in GitHub Desktop.
Validador del checksum del número de Seguro Social de México, usando el algoritmo Luhn

Número de seguro social NSS

El numero de seguro social de México se conforma de 11 dígitos en donde el ultimo es el dígito verificador(Checksum), el cual es generado por el algoritmo Luhn.

Formato.

Formato img

Nota: para mayores informes consultar la definición del algoritmo en wikipedia: :point_right: https://es.wikipedia.org/wiki/Algoritmo_de_Luhn

/**
* Recibe un numero de 10 digitos y devuelve el 11vo
* el que corresponde a la suma de validación que a su vez
* se basa del algoritmo del algoritmo de Luhn, mayores informes
* consultar:
*
* https://es.wikipedia.org/wiki/Algoritmo_de_Luhn
*/
const nss_checksum = function(strIn) {
let suma = 0
for (let i = 0; i<10; i += 1) {
let factor = (i % 2 === 1) ? 2: 1
const v = parseInt(strIn.charAt(i), 10) * factor
suma += (v>9) ? (1 + v % 10) : v
}
return (suma * 9) % 10
}
// testing
// print(nss_checksum('7992739871')) # result 3 (ejemplo wikipedia)
# Recibe un numero de 10 digitos y devuelve el 11vo
# el que corresponde a la suma de validación que a su vez
# se basa del algoritmo del algoritmo de Luhn, mayores informes
# consultar:
#
# https://es.wikipedia.org/wiki/Algoritmo_de_Luhn
#
def nss_checksum(nss):
suma = 0
for i in range(10):
factor = 2 if (i % 2 == 1) else 1
v = int(nss[i]) * factor
suma += (1 + v % 10) if (v >9) else v
return (suma * 9) % 10
# testing
# print(nss_checksum('7992739871')) # result 3 (ejemplo wikipedia)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment