shamir secret sharing demo in js
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
const secrets = require('secrets.js-grempe') | |
const shamir = { | |
create: (plainText, totalShares, threshold) => { | |
// convert the text into a hex string | |
var pwHex = secrets.str2hex(passphrase); // => hex string | |
// split into 2 shares with a threshold of 2. | |
// 1 share will be sent to backend and stored there | |
// and another share will be kept by the user. | |
var shares = secrets.share(pwHex, 2, 2); | |
return shares | |
}, | |
decode: (arrayOfShares, plainText) => { | |
const sharesCombined = secrets.combine(arrayOfShares); | |
//convert back to UTF string and compare to plainText | |
return secrets.hex2str(sharesCombined) | |
} | |
} | |
// a 10-word passphrase. | |
// the longer the passphrase the longer each shamir's share will be. | |
const passphrase = 'shifted patent embassy minimum heritage shifted patent embassy minimum heritage'; | |
const shamirShares = shamir.create(passphrase, 2, 2) | |
console.log('- shamir shares:\n', shamirShares) | |
console.log('- shamir shares decoded:', shamir.decode(shamirShares)); // => true | |
console.log('- shamir shares verification:', shamir.decode(shamirShares) === passphrase); // => true | |
// output: | |
// - shamir shares: | |
// [ '801242eb5c278b117be76675cbfa38b4e108b87385ec8d940d05c353e7c1f44565d5565f86d59006e0130c7963a69205843c31980d4affe9a558751e3fb669e0de735ef0b79bba9446ab4819aae7d55e229b6d3d575fd706df242fb049d796fe3cc65762809a034d81099cead3f8d7b662fd6674331ae31ff81f36121fe75a1b1b922f663153706df45faeaf755036331576bff49fd13970f7c70337e8f9bce50dd', | |
// '802485f7736f0d62ec2ec52b8d85b9d9c8f0bab70dc8d188022b8dd7c433e3aac01aa7dedbab28bdc97600631d7d2e6b0319b9d1dd5437d2918130ddb77cc821a436aa3165e6be0884875b529fafa12d9c771dbb745e749da5a8477089cf248db2aca5450725ddfadbf2f3647c50744cce5b179860241e9e38afb574242eaf97fd8445ec64a6e90a338e966f3360665623ed683924b269c1e64e0ccfcb82b39a032' ] | |
// - shamir shares decoded: shifted patent embassy minimum heritage shifted patent embassy minimum heritage | |
// - shamir shares verification: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment