UNPKG

apostille-library

Version:

A novel & holistic blockchain notarization and timestamping with transferable, updatable, branded, and conjointly owned notarizations.

63 lines 2.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_js_1 = __importDefault(require("crypto-js")); const Errors_1 = require("../types/Errors"); class Verifier { static verifyPublicApostille(data, payload) { if (this.isApostille(payload)) { if (this.isPublicApostille(payload)) { const fileHash = Verifier.retrieveHash(payload, data); return fileHash === payload.substring(10); } throw new Error(Errors_1.Errors[Errors_1.Errors.NOT_PUBLIC_APOSTILLE]); } throw new Error(Errors_1.Errors[Errors_1.Errors.NOT_APOSTILLE]); } static verifyPrivateApostille(signer, data, payload) { if (this.isApostille(payload)) { if (this.isPrivateApostille(payload)) { const fileHash = Verifier.retrieveHash(payload, data); return signer.verifySignature(fileHash, payload.substring(10)); } throw new Error(Errors_1.Errors[Errors_1.Errors.NOT_PRIVATE_APOSTILLE]); } throw new Error(Errors_1.Errors[Errors_1.Errors.NOT_APOSTILLE]); } static isApostille(payload) { return (payload.substring(0, 8) === 'fe4e5459'); } static isPublicApostille(payload) { const hashingByte = payload.substring(8, 10); return (hashingByte === '01' || hashingByte === '02' || hashingByte === '03' || hashingByte === '08' || hashingByte === '09'); } static isPrivateApostille(payload) { const hashingByte = payload.substring(8, 10); return (hashingByte === '81' || hashingByte === '82' || hashingByte === '83' || hashingByte === '88' || hashingByte === '89'); } static retrieveHash(apostilleHash, data) { const checksum = apostilleHash.substring(0, 10); const hashingVersionBytes = checksum.substring(8); if (hashingVersionBytes === '01' || hashingVersionBytes === '81') { return crypto_js_1.default.MD5(data).toString(crypto_js_1.default.enc.Hex); } else if (hashingVersionBytes === '02' || hashingVersionBytes === '82') { return crypto_js_1.default.SHA1(data).toString(crypto_js_1.default.enc.Hex); } else if (hashingVersionBytes === '03' || hashingVersionBytes === '83') { return crypto_js_1.default.SHA256(data).toString(crypto_js_1.default.enc.Hex); } else if (hashingVersionBytes === '08' || hashingVersionBytes === '88') { return crypto_js_1.default.SHA3(data, { outputLength: 256 }).toString(crypto_js_1.default.enc.Hex); } else { return crypto_js_1.default.SHA3(data, { outputLength: 512 }).toString(crypto_js_1.default.enc.Hex); } } } exports.Verifier = Verifier; //# sourceMappingURL=Verifier.js.map