26 lines
1023 B
JavaScript
26 lines
1023 B
JavaScript
// compara_cert_ok_vs_pem.js
|
|
const fs = require("fs");
|
|
const crypto = require("crypto");
|
|
|
|
const pemToB64 = (pem) =>
|
|
String(pem).replace(/-----BEGIN CERTIFICATE-----|-----END CERTIFICATE-----|\s+/g, "").trim();
|
|
const b64ToDer = (b64) => Buffer.from(b64, "base64");
|
|
const printInfo = (label, der) => {
|
|
const sha1 = crypto.createHash("sha1").update(der).digest("hex");
|
|
const sha256 = crypto.createHash("sha256").update(der).digest("hex");
|
|
console.log(`[${label}] sha1=${sha1} sha256=${sha256}`);
|
|
};
|
|
|
|
const xml = fs.readFileSync("Signature.xml", "utf8");
|
|
const m = xml.match(/<X509Certificate>([^<]+)<\/X509Certificate>/i);
|
|
if (!m) throw new Error("No se encontró <X509Certificate> en Signature.xml");
|
|
const okB64 = m[1].replace(/\s+/g, "");
|
|
const okDer = b64ToDer(okB64);
|
|
|
|
const pem = fs.readFileSync("onlyCert.pem", "utf8");
|
|
const pemDer = b64ToDer(pemToB64(pem));
|
|
|
|
printInfo("DGII_OK", okDer);
|
|
printInfo("NODE_PEM", pemDer);
|
|
console.log("\n¿Mismo certificado?:", okDer.equals(pemDer));
|