Skip to content

Instantly share code, notes, and snippets.

@fitorec
Last active May 2, 2022 08:48
Show Gist options
  • 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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
id="svg8"
version="1.1"
viewBox="0 0 124.35417 41.010416"
height="155"
width="470">
<defs
id="defs2">
<marker
style="overflow:visible"
id="marker5135"
refX="0"
refY="0"
orient="auto">
<path
transform="scale(-0.6)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:#ff0000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path5133" />
</marker>
<marker
orient="auto"
refY="0"
refX="0"
id="marker3299"
style="overflow:visible">
<path
id="path3297"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
transform="scale(-0.6)" />
</marker>
<marker
orient="auto"
refY="0"
refX="0"
id="marker1235"
style="overflow:visible">
<path
id="path1233"
style="fill:#0000ff;fill-opacity:1;fill-rule:evenodd;stroke:#0000ff;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
transform="scale(-0.6)" />
</marker>
<marker
style="overflow:visible"
id="marker1193"
refX="0"
refY="0"
orient="auto">
<path
transform="scale(-0.6)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#008000;fill-opacity:1;fill-rule:evenodd;stroke:#008000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path1191" />
</marker>
<marker
style="overflow:visible"
id="Arrow2Mend"
refX="0"
refY="0"
orient="auto">
<path
transform="scale(-0.6)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#008000;fill-opacity:1;fill-rule:evenodd;stroke:#008000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path890" />
</marker>
</defs>
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
transform="translate(0,-255.9896)"
id="layer1">
<g
transform="translate(-15.87029,211.40383)"
id="g6613">
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
x="37.041668"
y="63.410713"
id="text817"><tspan
id="tspan815"
x="37.041668"
y="63.410713"
style="stroke-width:0.26458332"><tspan
style="fill:#0000ff"
id="tspan821">72</tspan> <tspan
style="fill:#008000"
id="tspan835">79 56</tspan> 0804 <tspan
style="font-weight:bold;fill:#ff0000"
id="tspan819">0</tspan></tspan></text>
<text
id="text829"
y="49.296539"
x="36.159218"
style="font-style:normal;font-weight:normal;font-size:5.19802713px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#0000ff;fill-opacity:1;stroke:none;stroke-width:0.12995069"
xml:space="preserve"><tspan
style="fill:#0000ff;stroke-width:0.12995069"
y="49.296539"
x="36.159218"
id="tspan831">Region IMSS</tspan></text>
<text
id="text829-3"
y="73.485344"
x="16.784279"
style="font-style:normal;font-weight:normal;font-size:5.1772294px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#008000;fill-opacity:1;stroke:none;stroke-width:0.12943073"
xml:space="preserve"><tspan
style="fill:#008000;stroke-width:0.12943073"
y="73.485344"
x="16.784279"
id="tspan831-6">Año de alta</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:5.1772294px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#008000;fill-opacity:1;stroke:none;stroke-width:0.12943073"
x="15.829843"
y="82.233673"
id="text857"><tspan
id="tspan855"
x="15.829843"
y="82.233673"
style="fill:#008000;stroke-width:0.12943073">Año de nacimiento</tspan></text>
<path
style="fill:none;stroke:#008000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#marker1193)"
d="m 62.54107,65.678224 v 5.746295 h -8.018087"
id="path859" />
<path
style="fill:none;stroke:#008000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#Arrow2Mend)"
d="M 80.715397,66.079128 V 78.774429 H 72.69731"
id="path861" />
<path
id="path1231"
d="m 34.802278,48.305704 -6.280834,0.06682 v 10.490329 h 8.018087"
style="fill:none;stroke:#0000ff;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#marker1235)" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:5.1772294px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.12943073"
x="99.536934"
y="48.676411"
id="text857-7"><tspan
id="tspan855-5"
x="99.536934"
y="48.676411"
style="fill:#000000;stroke-width:0.12943073">Folio IMSS</tspan></text>
<path
id="path3295"
d="m 99.292466,46.802312 c -4.142678,-0.03341 -5.144935,0 -5.144935,0 v 8.018087"
style="fill:none;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#marker3299)" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:5.1772294px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#ff0000;fill-opacity:1;stroke:none;stroke-width:0.12943073"
x="84.675949"
y="74.034248"
id="text857-7-3"><tspan
id="tspan855-5-5"
x="84.675949"
y="74.034248"
style="fill:#ff0000;stroke-width:0.12943073">Verificador usando</tspan></text>
<text
id="text5121"
y="80.081871"
x="85.24292"
style="font-style:normal;font-weight:normal;font-size:5.1772294px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#ff0000;fill-opacity:1;stroke:none;stroke-width:0.12943073"
xml:space="preserve"><tspan
style="fill:#ff0000;stroke-width:0.12943073"
y="80.081871"
x="85.24292"
id="tspan5119">El algoritmo <tspan
style="font-weight:bold;fill:#ff0000"
id="tspan5123">Luhn</tspan></tspan></text>
<path
style="fill:none;stroke:#ff0000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#marker5135)"
d="m 133.60758,76.070434 2.50108,-0.06682 V 61.166559 h -8.01809"
id="path5131" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="40.560299"
y="65.784897"
id="text6470"><tspan
id="tspan6468"
x="40.560299"
y="65.784897"
style="fill:#808080;stroke-width:0.07987373">1</tspan></text>
<text
id="text6474"
y="65.784897"
x="49.614056"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
style="fill:#808080;stroke-width:0.07987373"
y="65.784897"
x="49.614056"
id="tspan6472">2</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="57.765778"
y="65.739655"
id="text6478"><tspan
id="tspan6476"
x="57.765778"
y="65.739655"
style="fill:#808080;stroke-width:0.07987373">3</tspan></text>
<text
id="text6482"
y="65.784897"
x="65.65023"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
style="fill:#808080;stroke-width:0.07987373"
y="65.784897"
x="65.65023"
id="tspan6480">4</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="75.606018"
y="65.739655"
id="text6486"><tspan
id="tspan6484"
x="75.606018"
y="65.739655"
style="fill:#808080;stroke-width:0.07987373">5</tspan></text>
<text
id="text6490"
y="65.739655"
x="83.15638"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
style="fill:#808080;stroke-width:0.07987373"
y="65.739655"
x="83.15638"
id="tspan6488">6</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="92.511292"
y="65.784897"
id="text6494"><tspan
id="tspan6492"
x="92.511292"
y="65.784897"
style="fill:#808080;stroke-width:0.07987373">7</tspan></text>
<text
id="text6498"
y="65.739655"
x="99.409355"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
style="fill:#808080;stroke-width:0.07987373"
y="65.739655"
x="99.409355"
id="tspan6496">8</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="99.409355"
y="65.739655"
id="text6502"><tspan
id="tspan6500"
x="99.409355"
y="65.739655"
style="fill:#808080;stroke-width:0.07987373">8</tspan></text>
<text
id="text6506"
y="65.739655"
x="105.92944"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
style="fill:#808080;stroke-width:0.07987373"
y="65.739655"
x="105.92944"
id="tspan6504">9</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="122.27692"
y="65.784897"
id="text6510"><tspan
id="tspan6508"
x="122.27692"
y="65.784897"
style="fill:#808080;stroke-width:0.07987373">11</tspan></text>
<text
id="text6514"
y="65.739655"
x="112.63853"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
style="fill:#808080;stroke-width:0.07987373"
y="65.739655"
x="112.63853"
id="tspan6512">10</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="71.526985"
y="47.755341"
id="text6470-6"><tspan
id="tspan6468-2"
x="71.526985"
y="47.755341"
style="fill:#808080;stroke-width:0.07987373">[1 - 2]</tspan></text>
<text
id="text6536"
y="75.631081"
x="46.675053"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
style="fill:#808080;stroke-width:0.07987373"
y="75.631081"
x="46.675053"
id="tspan6534">[3 - 4]</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="64.250946"
y="85.17498"
id="text6540"><tspan
x="64.250946"
y="85.17498"
style="fill:#808080;stroke-width:0.07987373"
id="tspan6542">[5 - 6 ]</tspan></text>
<text
id="text6548"
y="50.023808"
x="125.94157"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
xml:space="preserve"><tspan
id="tspan6546"
style="fill:#808080;stroke-width:0.07987373"
y="50.023808"
x="125.94157">[7 - 10 ]</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.19494915px;line-height:10;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#808080;fill-opacity:1;stroke:none;stroke-width:0.07987373"
x="132.66129"
y="80.65303"
id="text6552"><tspan
x="132.66129"
y="80.65303"
style="fill:#808080;stroke-width:0.07987373"
id="tspan6550">[11 ]</tspan></text>
</g>
</g>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment