Created
May 2, 2021 23:24
-
-
Save asafpm/3e38d61fa756ebe1108573702bd4376f to your computer and use it in GitHub Desktop.
Validación de firmas digitales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "ad213817", | |
"metadata": {}, | |
"source": [ | |
"## Ejemplo de verificación de firma\n", | |
"\n", | |
"Este programa valida firmas digitales generadas mediante la función `openssl_sign` de PHP usando el algoritmo SHA256." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "08185718", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Certificado válido\n" | |
] | |
} | |
], | |
"source": [ | |
"from base64 import b64encode,b64decode\n", | |
"\n", | |
"from Crypto.Hash import SHA256\n", | |
"from Crypto.Signature import PKCS1_v1_5\n", | |
"from Crypto.PublicKey import RSA\n", | |
"\n", | |
"# La llave pública de la autoridad que firma el certificado\n", | |
"public_key = \"\"\"-----BEGIN PUBLIC KEY-----\n", | |
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjzf06Cgd8KZevWQVraikO5u+3\n", | |
"r+JzygUB9ooAml2CV4jnU3KxQZjGvnLBi2EYzx43hkz6O6xvYDn17+gJs5OnjhP0\n", | |
"iWj0qgoN1m3tBG71/Vzs+1sBs6T26Q3/imj2NZ9xlkq0oECsobN/iUKxtYnOXLuv\n", | |
"qVxkeNsqwrKVdoHTswIDAQAB\n", | |
"-----END PUBLIC KEY-----\"\"\"\n", | |
"\n", | |
"# El certificado a verificar\n", | |
"cert = \"\"\"###\n", | |
"-id_cert:d45bde575fafad5895e5ff9e64af4c3e563aa5d360d4289c2745f5c00959223f\n", | |
"-id_elec:gatoperro\n", | |
"-roles:{Estudiante:2009-1,Profesor:2021-1}\n", | |
"--- FIRMA ---\n", | |
"yamW5Ew+C71srrsGNVQF69QMYbnYA94ll6LAVJZWBa0mzajr6Uaa/xH3/SqD4RzCFe8Jj2l5X5Q3yDToFYw40hqml84Yvq4jjpO8T3NoHuuh2iexaZGvht3vbi0Y3Lu6Dfy+ZJ9SY8njO+rJ+S3IdktF+GYopR4ysOKqsG99iUo=\n", | |
"###\"\"\"\n", | |
"\n", | |
"# Separar la firma y el contenido del mensage\n", | |
"message, signature = cert.strip(\"#\\n\").split(\"\\n--- FIRMA ---\\n\")\n", | |
"message = bytes(message,'utf-8')\n", | |
"signature = bytes(signature,'utf-8')\n", | |
"\n", | |
"# Obtener un resumen del mensaje\n", | |
"digest = SHA256.new()\n", | |
"digest.update(message)\n", | |
"\n", | |
"# Generar el verificador de firmas\n", | |
"rsapubkey = RSA.importKey(public_key) \n", | |
"verifier = PKCS1_v1_5.new(rsapubkey)\n", | |
"\n", | |
"# Verificar la firma\n", | |
"verified = verifier.verify(digest, b64decode(signature))\n", | |
"\n", | |
"if verified:\n", | |
" print(\"Certificado válido\")\n", | |
"else:\n", | |
" print(\"Certificado no válido\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "5b237c4b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment