This repository was archived by the owner on Jun 8, 2026. It is now read-only.
forked from grempe/secrets.js
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsecrets.min.js
More file actions
3 lines (2 loc) · 8.98 KB
/
Copy pathsecrets.min.js
File metadata and controls
3 lines (2 loc) · 8.98 KB
1
2
3
/*! secrets.js-grempe 2021-04-30 */
!function(t,e){"use strict";"function"==typeof define&&define.amd?define([],function(){return t.secrets=e(window.crypto)}):"object"==typeof exports?module.exports=e(require("crypto")):t.secrets=e(t.crypto)}(this,function(o){"use strict";let u,c,n,i,s;function h(){u={bits:8,radix:16,minBits:3,maxBits:20,bytesPerChar:2,maxBytesPerChar:6,primitivePolynomials:[null,null,1,3,3,5,3,3,29,17,9,5,83,27,43,3,45,9,39,39,9,5,3,33,27,9,71,39,9,5,83]},c={},n=new Array(1024).join("0"),i=!0,s=["nodeCryptoRandomBytes","browserCryptoGetRandomValues","testRandom"]}function f(){return!(!c||!c.rng||"function"!=typeof c.rng)}function b(t,e){let r;if(0===e||1===e)return t;if(e&&1024<e)throw new Error("Padding must be multiples of no larger than 1024 bits.");return e=e||c.bits,t&&(r=t.length%e),r?(n+t).slice(-(e-r+t.length)):t}function g(t){let e="",r,n;for(n=t.length-1;0<=n;n--){if(r=parseInt(t[n],16),isNaN(r))throw new Error("Invalid hex character.");e=b(r.toString(2),4)+e}return e}function m(t){let e="",r,n;for(t=b(t,4),n=t.length;4<=n;n-=4){if(r=parseInt(t.slice(n-4,n),2),isNaN(r))throw new Error("Invalid binary character.");e=r.toString(16)+e}return e}function a(){return!(!o||"object"!=typeof o||"function"!=typeof o.getRandomValues&&"object"!=typeof o.getRandomValues||"function"!=typeof Uint32Array&&"object"!=typeof Uint32Array)}function l(){return"object"==typeof o&&"function"==typeof o.randomBytes}function p(t){function i(t,e,r,n){let i=0,o,a="",s;for(e&&(o=e.length-1);i<o||a.length<t;)s=Math.abs(parseInt(e[i],r)),a+=b(s.toString(2),n),i++;return a=a.substr(-t),(a.match(/0/g)||[]).length===a.length?null:a}function e(t){let e;var r;let n=null;for(r=Math.ceil(t/8);null===n;)e=o.randomBytes(r),n=i(t,e.toString("hex"),16,4);return n}function r(t){var e;let r=null;for(e=Math.ceil(t/32);null===r;)r=i(t,o.getRandomValues(new Uint32Array(e)),10,32);return r}return t&&"testRandom"===t?(c.typeCSPRNG=t,function(t){let e;var r;let n=null;r=Math.ceil(t/32),e=new Uint32Array(r);for(let t=0;t<e.length;t++)e[t]=123456789;for(;null===n;)n=i(t,e,10,32);return n}):t&&"nodeCryptoRandomBytes"===t?(c.typeCSPRNG=t,e):t&&"browserCryptoGetRandomValues"===t?(c.typeCSPRNG=t,r):l()?(c.typeCSPRNG="nodeCryptoRandomBytes",e):a()?(c.typeCSPRNG="browserCryptoGetRandomValues",r):void 0}function d(t,e){const r=[];let n;for(e&&(t=b(t,e)),n=t.length;n>c.bits;n-=c.bits)r.push(parseInt(t.slice(n-c.bits,n),2));return r.push(parseInt(t.slice(0,n),2)),r}function w(t,e){var r=c.logs[t];let n=0,i;for(i=e.length-1;0<=i;i--)n=0!==n?c.exps[(r+c.logs[n])%c.maxShares]^e[i]:e[i];return n}function y(t,e,r){let n=0;var i;let o,a,s;for(a=0,i=e.length;a<i;a++)if(r[a]){for(o=c.logs[r[a]],s=0;s<i;s++)if(a!==s){if(t===e[s]){o=-1;break}o=(o+c.logs[t^e[s]]-c.logs[e[a]^e[s]]+c.maxShares)%c.maxShares}n=-1===o?n:n^c.exps[o]}return n}function x(t,e,r){const n=[],i=[t];let o;var a;for(o=1;o<r;o++)i[o]=parseInt(c.rng(c.bits),2);for(a=e+(o=1);o<a;o++)n[o-1]={x:o,y:w(o,i)};return n}function S(t,e,r){let n,i,o,a;if(e=parseInt(e,c.radix),n=(t=parseInt(t,10)||c.bits).toString(36).toUpperCase(),o=Math.pow(2,t)-1,a=o.toString(c.radix).length,i=b(e.toString(c.radix),a),"number"!=typeof e||e%1!=0||e<1||e>o)throw new Error("Share id must be an integer between 1 and "+o+", inclusive.");return n+i+r}const t={init:function(t,e){const r=[],n=[];let i=1;var o;let a;if(h(),t&&("number"!=typeof t||t%1!=0||t<u.minBits||t>u.maxBits))throw new Error("Number of bits must be an integer between "+u.minBits+" and "+u.maxBits+", inclusive.");if(e&&-1===s.indexOf(e))throw new Error("Invalid RNG type argument : '"+e+"'");for(c.radix=u.radix,c.bits=t||u.bits,c.size=Math.pow(2,c.bits),c.maxShares=c.size-1,o=u.primitivePolynomials[c.bits],a=0;a<c.size;a++)n[a]=i,r[i]=a,i<<=1,i>=c.size&&(i^=o,i&=c.maxShares);if(c.logs=r,c.exps=n,e&&this.setRNG(e),f()||this.setRNG(),!(f()&&c.bits&&c.size&&c.maxShares&&c.logs&&c.exps&&c.logs.length===c.size&&c.exps.length===c.size))throw new Error("Initialization failed.")},combine:function(t,e){let r,n,i;var o,a,s;let h="",u;const f=[],l=[];for(e=e||0,r=0,i=t.length;r<i;r++){if(a=this.extractShareComponents(t[r]),void 0===u)u=a.bits;else if(a.bits!==u)throw new Error("Mismatched shares: Different bit settings.");if(c.bits!==u&&this.init(u),-1===f.indexOf(a.id))for(f.push(a.id),s=d(g(a.data)),n=0,o=s.length;n<o;n++)l[n]=l[n]||[],l[n][f.length-1]=s[n]}for(r=0,i=l.length;r<i;r++)h=b(y(e,f,l[r]).toString(2))+h;return m(1<=e?h:h.slice(h.indexOf("1")+1))},getConfig:function(){const t={};return t.radix=c.radix,t.bits=c.bits,t.maxShares=c.maxShares,t.hasCSPRNG=f(),t.typeCSPRNG=c.typeCSPRNG,t},extractShareComponents:function(t){var e,r,n;let i;const o={};if((e=parseInt(t.substr(0,1),36))&&("number"!=typeof e||e%1!=0||e<u.minBits||e>u.maxBits))throw new Error("Invalid share : Number of bits must be an integer between "+u.minBits+" and "+u.maxBits+", inclusive.");if(r=Math.pow(2,e)-1,n=(Math.pow(2,e)-1).toString(c.radix).length,(n=new RegExp("^([a-kA-K3-9]{1})([a-fA-F0-9]{"+n+"})([a-fA-F0-9]+)$").exec(t))&&(i=parseInt(n[2],c.radix)),"number"!=typeof i||i%1!=0||i<1||i>r)throw new Error("Invalid share : Share id must be an integer between 1 and "+c.maxShares+", inclusive.");if(n&&n[3])return o.bits=e,o.id=i,o.data=n[3],o;throw new Error("The share data provided is invalid : "+t)},setRNG:function(t){var e="Random number generator is invalid ",r=" Supply an CSPRNG of the form function(bits){} that returns a string containing 'bits' number of random 1's and 0's.";if(t&&"string"==typeof t&&-1===s.indexOf(t))throw new Error("Invalid RNG type argument : '"+t+"'");if((t=t||p())&&"string"==typeof t&&(t=p(t)),i){if(t&&"function"!=typeof t)throw new Error(e+"(Not a function)."+r);if(t&&"string"!=typeof t(c.bits))throw new Error(e+"(Output is not a string)."+r);if(t&&!parseInt(t(c.bits),2))throw new Error(e+"(Binary string output not parseable to an Integer)."+r);if(t&&t(c.bits).length>c.bits)throw new Error(e+"(Output length is greater than config.bits)."+r);if(t&&t(c.bits).length<c.bits)throw new Error(e+"(Output length is less than config.bits)."+r)}return c.rng=t,!0},str2hex:function(t,e){var r,n,i,o;let a="",s,h;if("string"!=typeof t)throw new Error("Input must be a character string.");if("number"!=typeof(e=e||u.bytesPerChar)||e<1||e>u.maxBytesPerChar||e%1!=0)throw new Error("Bytes per character must be an integer between 1 and "+u.maxBytesPerChar+", inclusive.");for(r=2*e,n=Math.pow(16,r)-1,h=0,o=t.length;h<o;h++){if(s=t[h].charCodeAt(),isNaN(s))throw new Error("Invalid character: "+t[h]);if(s>n)throw i=Math.ceil(Math.log(s+1)/Math.log(256)),new Error("Invalid character code ("+s+"). Maximum allowable is 256^bytes-1 ("+n+"). To convert this character, use at least "+i+" bytes.");a=b(s.toString(16),r)+a}return a},hex2str:function(t,e){var r,n;let i="",o;if("string"!=typeof t)throw new Error("Input must be a hexadecimal string.");if("number"!=typeof(e=e||u.bytesPerChar)||e%1!=0||e<1||e>u.maxBytesPerChar)throw new Error("Bytes per character must be an integer between 1 and "+u.maxBytesPerChar+", inclusive.");for(t=b(t,r=2*e),o=0,n=t.length;o<n;o+=r)i=String.fromCharCode(parseInt(t.slice(o,o+r),16))+i;return i},random:function(t){if("number"!=typeof t||t%1!=0||t<2||65536<t)throw new Error("Number of bits must be an Integer between 1 and 65536.");return m(c.rng(t))},share:function(t,e,r,n){let i,o;const a=new Array(e),s=new Array(e);let h,u;var f;if(n=n||128,"string"!=typeof t)throw new Error("Secret must be a string.");if("number"!=typeof e||e%1!=0||e<2)throw new Error("Number of shares must be an integer between 2 and 2^bits-1 ("+c.maxShares+"), inclusive.");if(e>c.maxShares)throw i=Math.ceil(Math.log(e+1)/Math.LN2),new Error("Number of shares must be an integer between 2 and 2^bits-1 ("+c.maxShares+"), inclusive. To create "+e+" shares, use at least "+i+" bits.");if("number"!=typeof r||r%1!=0||r<2)throw new Error("Threshold number of shares must be an integer between 2 and 2^bits-1 ("+c.maxShares+"), inclusive.");if(r>c.maxShares)throw i=Math.ceil(Math.log(r+1)/Math.LN2),new Error("Threshold number of shares must be an integer between 2 and 2^bits-1 ("+c.maxShares+"), inclusive. To use a threshold of "+r+", use at least "+i+" bits.");if(e<r)throw new Error("Threshold number of shares was "+r+" but must be less than or equal to the "+e+" shares specified as the total to generate.");if("number"!=typeof n||n%1!=0||n<0||1024<n)throw new Error("Zero-pad length must be an integer between 0 and 1024 inclusive.");for(t=d(t="1"+g(t),n),h=0,f=t.length;h<f;h++)for(o=x(t[h],e,r),u=0;u<e;u++)a[u]=a[u]||o[u].x.toString(c.radix),s[u]=b(o[u].y.toString(2))+(s[u]||"");for(h=0;h<e;h++)a[h]=S(c.bits,a[h],m(s[h]));return a},newShare:function(t,e){var r=(t=t&&"string"==typeof t?parseInt(t,c.radix):t).toString(c.radix);if(t&&r&&e&&e[0])return S(this.extractShareComponents(e[0]).bits,r,this.combine(e,t));throw new Error("Invalid 'id' or 'shares' Array argument to newShare().")},_reset:h,_padLeft:b,_hex2bin:g,_bin2hex:m,_hasCryptoGetRandomValues:a,_hasCryptoRandomBytes:l,_getRNG:p,_isSetRNG:f,_splitNumStringToIntArray:d,_horner:w,_lagrange:y,_getShares:x,_constructPublicShareString:S};return t.init(),t});