UNPKG

shamirs-secret-sharing-ts

Version:

A simple implementation of Shamir's Secret Sharing configured to use a finite field in GF(2^8) with 128 bit padding

25 lines (24 loc) 841 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("./constants"); function parse(input) { const share = { id: null, bits: null, data: null }; if (Buffer.isBuffer(input)) { input = input.toString('hex'); } if ('0' === input[0]) { input = input.slice(1); } // bit count is in base36 share.bits = parseInt(input.slice(0, 1), 36); const maxBits = constants_1.BIT_SIZE - 1; const idLength = maxBits.toString(16).length; const regex = `^([a-kA-K3-9]{1})([a-fA-F0-9]{${idLength}})([a-fA-F0-9]+)$`; const matches = new RegExp(regex).exec(input); if (matches && matches.length) { share.id = parseInt(matches[2], 16); share.data = matches[3]; } return share; } exports.parse = parse;