Created
April 10, 2020 18:58
-
-
Save will123195/45e9a6b62a24a3c89e84febe98f84366 to your computer and use it in GitHub Desktop.
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
import sss from 'shamirs-secret-sharing' | |
const { crypto } = window | |
const u8ToB64 = u8 => Buffer.from(u8).toString('base64') | |
const b64toU8 = b64 => Buffer.from(b64, 'base64') | |
async function sssDemo() { | |
const keypair = await crypto.subtle.generateKey( | |
{ | |
name: 'RSA-OAEP', | |
modulusLength: 4096, | |
publicExponent: new Uint8Array([1, 0, 1]), | |
hash: 'SHA-256' | |
}, | |
true, | |
['encrypt', 'decrypt'] | |
) | |
const pubKeyJWK = await crypto.subtle.exportKey('jwk', keypair.publicKey) | |
const privKeyJWK = await crypto.subtle.exportKey('jwk', keypair.privateKey) | |
const privKeyStr = JSON.stringify(privKeyJWK) | |
const secretBuffer = Buffer.from(privKeyStr) | |
console.log({ secretBuffer }) | |
const allFragments = sss.split(secretBuffer, { shares: 3, threshold: 2 }).map(u8ToB64) | |
console.log({ allFragments }) // only 2 of these 3 fragments are required to recover the original private key | |
const twoFragments = allFragments.slice(0, 2).reverse() | |
const recovered = sss.combine(twoFragments.map(b64toU8)) | |
const recoveredPrivKeyJWK = JSON.parse(recovered.toString()) | |
console.log({ recoveredPrivKeyJWK }) | |
} | |
sssDemo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment