@digitaldefiance/secrets 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -0
- package/README.md +445 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/browser-types.d.ts +112 -0
- package/dist/browser-types.d.ts.map +1 -0
- package/dist/browser-types.js +242 -0
- package/dist/browser-types.js.map +1 -0
- package/dist/errors.d.ts +33 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +92 -0
- package/dist/errors.js.map +1 -0
- package/dist/esm/.tsbuildinfo +1 -0
- package/dist/esm/browser-types.d.ts +112 -0
- package/dist/esm/browser-types.d.ts.map +1 -0
- package/dist/esm/browser-types.js +201 -0
- package/dist/esm/browser-types.js.map +1 -0
- package/dist/esm/errors.d.ts +33 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +54 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/node-types.d.ts +87 -0
- package/dist/esm/node-types.d.ts.map +1 -0
- package/dist/esm/node-types.js +157 -0
- package/dist/esm/node-types.js.map +1 -0
- package/dist/esm/secrets.d.ts +262 -0
- package/dist/esm/secrets.d.ts.map +1 -0
- package/dist/esm/secrets.js +894 -0
- package/dist/esm/secrets.js.map +1 -0
- package/dist/esm/types.d.ts +249 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +61 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/validation.d.ts +34 -0
- package/dist/esm/validation.d.ts.map +1 -0
- package/dist/esm/validation.js +72 -0
- package/dist/esm/validation.js.map +1 -0
- package/dist/node-types.d.ts +87 -0
- package/dist/node-types.d.ts.map +1 -0
- package/dist/node-types.js +191 -0
- package/dist/node-types.js.map +1 -0
- package/dist/secrets.d.ts +262 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +897 -0
- package/dist/secrets.js.map +1 -0
- package/dist/types.d.ts +249 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +67 -0
- package/dist/types.js.map +1 -0
- package/dist/umd/.tsbuildinfo +1 -0
- package/dist/umd/browser-types.d.ts +112 -0
- package/dist/umd/browser-types.d.ts.map +1 -0
- package/dist/umd/browser-types.js +252 -0
- package/dist/umd/browser-types.js.map +1 -0
- package/dist/umd/errors.d.ts +33 -0
- package/dist/umd/errors.d.ts.map +1 -0
- package/dist/umd/errors.js +102 -0
- package/dist/umd/errors.js.map +1 -0
- package/dist/umd/node-types.d.ts +87 -0
- package/dist/umd/node-types.d.ts.map +1 -0
- package/dist/umd/node-types.js +201 -0
- package/dist/umd/node-types.js.map +1 -0
- package/dist/umd/secrets.d.ts +262 -0
- package/dist/umd/secrets.d.ts.map +1 -0
- package/dist/umd/secrets.js +907 -0
- package/dist/umd/secrets.js.map +1 -0
- package/dist/umd/secrets.min.js +2 -0
- package/dist/umd/types.d.ts +249 -0
- package/dist/umd/types.d.ts.map +1 -0
- package/dist/umd/types.js +77 -0
- package/dist/umd/types.js.map +1 -0
- package/dist/umd/validation.d.ts +34 -0
- package/dist/umd/validation.d.ts.map +1 -0
- package/dist/umd/validation.js +92 -0
- package/dist/umd/validation.js.map +1 -0
- package/dist/validation.d.ts +34 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +82 -0
- package/dist/validation.js.map +1 -0
- package/package.json +181 -0
|
@@ -0,0 +1,894 @@
|
|
|
1
|
+
// @preserve author Alexander Stetsyuk
|
|
2
|
+
// @preserve author Glenn Rempe <glenn@rempe.us>
|
|
3
|
+
// @license MIT
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// Module State
|
|
6
|
+
// ============================================================================
|
|
7
|
+
let defaults;
|
|
8
|
+
let config;
|
|
9
|
+
let preGenPadding;
|
|
10
|
+
let runCSPRNGTest;
|
|
11
|
+
let CSPRNGTypes;
|
|
12
|
+
let byteToHex;
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Initialization and Reset
|
|
15
|
+
// ============================================================================
|
|
16
|
+
function reset() {
|
|
17
|
+
defaults = {
|
|
18
|
+
bits: 8,
|
|
19
|
+
radix: 16,
|
|
20
|
+
minBits: 3,
|
|
21
|
+
maxBits: 20,
|
|
22
|
+
bytesPerChar: 2,
|
|
23
|
+
maxBytesPerChar: 6,
|
|
24
|
+
primitivePolynomials: [
|
|
25
|
+
null, null, 1, 3, 3, 5, 3, 3, 29, 17, 9, 5, 83, 27, 43, 3,
|
|
26
|
+
45, 9, 39, 39, 9, 5, 3, 33, 27, 9, 71, 39, 9, 5, 83
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
config = {};
|
|
30
|
+
preGenPadding = new Array(1024).join("0");
|
|
31
|
+
runCSPRNGTest = true;
|
|
32
|
+
byteToHex = [];
|
|
33
|
+
for (let i = 0; i <= 0xff; i++) {
|
|
34
|
+
let hexOctet = i.toString(16);
|
|
35
|
+
if (hexOctet.length === 1) {
|
|
36
|
+
hexOctet = "0" + hexOctet;
|
|
37
|
+
}
|
|
38
|
+
byteToHex.push(hexOctet);
|
|
39
|
+
}
|
|
40
|
+
CSPRNGTypes = [
|
|
41
|
+
"nodeCryptoRandomBytes",
|
|
42
|
+
"browserCryptoGetRandomValues",
|
|
43
|
+
"testRandom"
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
function isSetRNG() {
|
|
47
|
+
if (config && config.rng && typeof config.rng === "function") {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
// ============================================================================
|
|
53
|
+
// Utility Functions
|
|
54
|
+
// ============================================================================
|
|
55
|
+
function padLeft(str, multipleOfBits) {
|
|
56
|
+
let missing;
|
|
57
|
+
if (multipleOfBits === 0 || multipleOfBits === 1) {
|
|
58
|
+
return str;
|
|
59
|
+
}
|
|
60
|
+
if (multipleOfBits && multipleOfBits > 1024) {
|
|
61
|
+
throw new Error("Padding must be multiples of no larger than 1024 bits.");
|
|
62
|
+
}
|
|
63
|
+
const bits = multipleOfBits || config.bits;
|
|
64
|
+
if (str) {
|
|
65
|
+
missing = str.length % bits;
|
|
66
|
+
}
|
|
67
|
+
if (missing) {
|
|
68
|
+
return (preGenPadding + str).slice(-(bits - missing + str.length));
|
|
69
|
+
}
|
|
70
|
+
return str;
|
|
71
|
+
}
|
|
72
|
+
function hex2bin(str) {
|
|
73
|
+
let bin = "";
|
|
74
|
+
let num;
|
|
75
|
+
for (let i = str.length - 1; i >= 0; i--) {
|
|
76
|
+
num = parseInt(str[i], 16);
|
|
77
|
+
if (isNaN(num)) {
|
|
78
|
+
throw new Error("Invalid hex character.");
|
|
79
|
+
}
|
|
80
|
+
bin = padLeft(num.toString(2), 4) + bin;
|
|
81
|
+
}
|
|
82
|
+
return bin;
|
|
83
|
+
}
|
|
84
|
+
function bin2hex(str) {
|
|
85
|
+
let hex = "";
|
|
86
|
+
let num;
|
|
87
|
+
str = padLeft(str, 4);
|
|
88
|
+
for (let i = str.length; i >= 4; i -= 4) {
|
|
89
|
+
num = parseInt(str.slice(i - 4, i), 2);
|
|
90
|
+
if (isNaN(num)) {
|
|
91
|
+
throw new Error("Invalid binary character.");
|
|
92
|
+
}
|
|
93
|
+
hex = num.toString(16) + hex;
|
|
94
|
+
}
|
|
95
|
+
return hex;
|
|
96
|
+
}
|
|
97
|
+
function bytesToHex(bytes) {
|
|
98
|
+
let hex = "";
|
|
99
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
100
|
+
hex += byteToHex[bytes[i]];
|
|
101
|
+
}
|
|
102
|
+
return hex;
|
|
103
|
+
}
|
|
104
|
+
// ============================================================================
|
|
105
|
+
// Crypto Environment Detection
|
|
106
|
+
// ============================================================================
|
|
107
|
+
function hasCryptoGetRandomValues() {
|
|
108
|
+
const crypto = (typeof window !== 'undefined' && window.crypto) || global.crypto;
|
|
109
|
+
if (crypto &&
|
|
110
|
+
typeof crypto === "object" &&
|
|
111
|
+
(typeof crypto.getRandomValues === "function" ||
|
|
112
|
+
typeof crypto.getRandomValues === "object") &&
|
|
113
|
+
(typeof Uint32Array === "function" || typeof Uint32Array === "object")) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
function hasCryptoRandomBytes() {
|
|
119
|
+
let crypto;
|
|
120
|
+
try {
|
|
121
|
+
crypto = require("crypto");
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (typeof crypto === "object" && typeof crypto.randomBytes === "function") {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
function getRNG(type) {
|
|
132
|
+
function construct(bits, arr, radix, size) {
|
|
133
|
+
let i = 0;
|
|
134
|
+
let len;
|
|
135
|
+
let str = "";
|
|
136
|
+
let parsedInt;
|
|
137
|
+
if (arr) {
|
|
138
|
+
len = arr.length - 1;
|
|
139
|
+
}
|
|
140
|
+
while (i < len || str.length < bits) {
|
|
141
|
+
parsedInt = Math.abs(parseInt(arr[i], radix));
|
|
142
|
+
str = str + padLeft(parsedInt.toString(2), size);
|
|
143
|
+
i++;
|
|
144
|
+
}
|
|
145
|
+
str = str.substr(-bits);
|
|
146
|
+
if ((str.match(/0/g) || []).length === str.length) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
return str;
|
|
150
|
+
}
|
|
151
|
+
function nodeCryptoRandomBytes(bits) {
|
|
152
|
+
let buf;
|
|
153
|
+
let bytes;
|
|
154
|
+
const radix = 16;
|
|
155
|
+
const size = 4;
|
|
156
|
+
let str = null;
|
|
157
|
+
bytes = Math.ceil(bits / 8);
|
|
158
|
+
const crypto = require("crypto");
|
|
159
|
+
while (str === null) {
|
|
160
|
+
buf = crypto.randomBytes(bytes);
|
|
161
|
+
str = construct(bits, bytesToHex(buf), radix, size);
|
|
162
|
+
}
|
|
163
|
+
return str;
|
|
164
|
+
}
|
|
165
|
+
function browserCryptoGetRandomValues(bits) {
|
|
166
|
+
let elems;
|
|
167
|
+
const radix = 10;
|
|
168
|
+
const size = 32;
|
|
169
|
+
let str = null;
|
|
170
|
+
elems = Math.ceil(bits / 32);
|
|
171
|
+
const crypto = (typeof window !== 'undefined' && window.crypto) || global.crypto;
|
|
172
|
+
while (str === null) {
|
|
173
|
+
str = construct(bits, crypto.getRandomValues(new Uint32Array(elems)), radix, size);
|
|
174
|
+
}
|
|
175
|
+
return str;
|
|
176
|
+
}
|
|
177
|
+
function testRandom(bits) {
|
|
178
|
+
let arr;
|
|
179
|
+
let elems;
|
|
180
|
+
const int = 123456789;
|
|
181
|
+
const radix = 10;
|
|
182
|
+
const size = 32;
|
|
183
|
+
let str = null;
|
|
184
|
+
elems = Math.ceil(bits / 32);
|
|
185
|
+
arr = new Uint32Array(elems);
|
|
186
|
+
for (let i = 0; i < arr.length; i++) {
|
|
187
|
+
arr[i] = int;
|
|
188
|
+
}
|
|
189
|
+
while (str === null) {
|
|
190
|
+
str = construct(bits, arr, radix, size);
|
|
191
|
+
}
|
|
192
|
+
return str;
|
|
193
|
+
}
|
|
194
|
+
if (type && type === "testRandom") {
|
|
195
|
+
config.typeCSPRNG = type;
|
|
196
|
+
return testRandom;
|
|
197
|
+
}
|
|
198
|
+
else if (type && type === "nodeCryptoRandomBytes") {
|
|
199
|
+
config.typeCSPRNG = type;
|
|
200
|
+
return nodeCryptoRandomBytes;
|
|
201
|
+
}
|
|
202
|
+
else if (type && type === "browserCryptoGetRandomValues") {
|
|
203
|
+
config.typeCSPRNG = type;
|
|
204
|
+
return browserCryptoGetRandomValues;
|
|
205
|
+
}
|
|
206
|
+
else if (hasCryptoRandomBytes()) {
|
|
207
|
+
config.typeCSPRNG = "nodeCryptoRandomBytes";
|
|
208
|
+
return nodeCryptoRandomBytes;
|
|
209
|
+
}
|
|
210
|
+
else if (hasCryptoGetRandomValues()) {
|
|
211
|
+
config.typeCSPRNG = "browserCryptoGetRandomValues";
|
|
212
|
+
return browserCryptoGetRandomValues;
|
|
213
|
+
}
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
// ============================================================================
|
|
217
|
+
// Core Algorithm Functions
|
|
218
|
+
// ============================================================================
|
|
219
|
+
function splitNumStringToIntArray(str, padLength) {
|
|
220
|
+
const parts = [];
|
|
221
|
+
if (padLength) {
|
|
222
|
+
str = padLeft(str, padLength);
|
|
223
|
+
}
|
|
224
|
+
let i;
|
|
225
|
+
for (i = str.length; i > config.bits; i -= config.bits) {
|
|
226
|
+
parts.push(parseInt(str.slice(i - config.bits, i), 2));
|
|
227
|
+
}
|
|
228
|
+
parts.push(parseInt(str.slice(0, i), 2));
|
|
229
|
+
return parts;
|
|
230
|
+
}
|
|
231
|
+
function horner(x, coeffs) {
|
|
232
|
+
const logx = config.logs[x];
|
|
233
|
+
let fx = 0;
|
|
234
|
+
for (let i = coeffs.length - 1; i >= 0; i--) {
|
|
235
|
+
if (fx !== 0) {
|
|
236
|
+
fx = config.exps[(logx + config.logs[fx]) % config.maxShares] ^ coeffs[i];
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
fx = coeffs[i];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return fx;
|
|
243
|
+
}
|
|
244
|
+
function lagrange(at, x, y) {
|
|
245
|
+
let sum = 0;
|
|
246
|
+
const len = x.length;
|
|
247
|
+
let product;
|
|
248
|
+
for (let i = 0; i < len; i++) {
|
|
249
|
+
if (y[i]) {
|
|
250
|
+
product = config.logs[y[i]];
|
|
251
|
+
for (let j = 0; j < len; j++) {
|
|
252
|
+
if (i !== j) {
|
|
253
|
+
if (at === x[j]) {
|
|
254
|
+
product = -1;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
product =
|
|
258
|
+
(product +
|
|
259
|
+
config.logs[at ^ x[j]] -
|
|
260
|
+
config.logs[x[i] ^ x[j]] +
|
|
261
|
+
config.maxShares) %
|
|
262
|
+
config.maxShares;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
sum = product === -1 ? sum : sum ^ config.exps[product];
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return sum;
|
|
269
|
+
}
|
|
270
|
+
function getShares(secret, numShares, threshold) {
|
|
271
|
+
const shares = [];
|
|
272
|
+
const coeffs = [secret];
|
|
273
|
+
for (let i = 1; i < threshold; i++) {
|
|
274
|
+
coeffs[i] = parseInt(config.rng(config.bits), 2);
|
|
275
|
+
}
|
|
276
|
+
for (let i = 1, len = numShares + 1; i < len; i++) {
|
|
277
|
+
shares[i - 1] = {
|
|
278
|
+
x: i,
|
|
279
|
+
y: horner(i, coeffs)
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return shares;
|
|
283
|
+
}
|
|
284
|
+
function constructPublicShareString(bits, id, data) {
|
|
285
|
+
const bitsNum = typeof bits === 'string' ? parseInt(bits, 10) : bits;
|
|
286
|
+
const bitsBase36 = bitsNum.toString(36).toUpperCase();
|
|
287
|
+
const idMax = Math.pow(2, bitsNum) - 1;
|
|
288
|
+
let numericId;
|
|
289
|
+
if (typeof id === "number") {
|
|
290
|
+
numericId = id;
|
|
291
|
+
}
|
|
292
|
+
else if (typeof id === "string") {
|
|
293
|
+
numericId = parseInt(id, 10);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
numericId = NaN;
|
|
297
|
+
}
|
|
298
|
+
if (typeof numericId !== "number" ||
|
|
299
|
+
numericId % 1 !== 0 ||
|
|
300
|
+
numericId < 1 ||
|
|
301
|
+
numericId > idMax ||
|
|
302
|
+
isNaN(numericId)) {
|
|
303
|
+
throw new Error("Share id must be an integer between 1 and " + idMax + ", inclusive.");
|
|
304
|
+
}
|
|
305
|
+
const idPaddingLen = idMax.toString(config.radix).length;
|
|
306
|
+
const idHex = padLeft(numericId.toString(config.radix), idPaddingLen);
|
|
307
|
+
const newShareString = bitsBase36 + idHex + data;
|
|
308
|
+
return newShareString;
|
|
309
|
+
}
|
|
310
|
+
// ============================================================================
|
|
311
|
+
// Public API
|
|
312
|
+
// ============================================================================
|
|
313
|
+
const SecretsLibrary = {
|
|
314
|
+
/**
|
|
315
|
+
* Initialize the secrets library with specified bit length and RNG type.
|
|
316
|
+
*
|
|
317
|
+
* This function sets up the Galois Field arithmetic tables (logs and exps)
|
|
318
|
+
* required for Shamir's Secret Sharing. It must be called before using
|
|
319
|
+
* share() or combine() operations.
|
|
320
|
+
*
|
|
321
|
+
* @param bits - Number of bits for the Galois Field (between 3 and 20, default 8).
|
|
322
|
+
* Determines the maximum number of shares (2^bits - 1).
|
|
323
|
+
* @param rngType - Type of cryptographically secure random number generator to use.
|
|
324
|
+
* If not specified, automatically detects the best available CSPRNG.
|
|
325
|
+
* @throws {Error} If bits is not an integer between 3 and 20, inclusive.
|
|
326
|
+
* @throws {Error} If rngType is invalid or RNG initialization fails.
|
|
327
|
+
* @throws {Error} If initialization fails for any reason.
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```typescript
|
|
331
|
+
* // Initialize with default 8 bits (max 255 shares)
|
|
332
|
+
* secrets.init();
|
|
333
|
+
*
|
|
334
|
+
* // Initialize with 10 bits (max 1023 shares)
|
|
335
|
+
* secrets.init(10);
|
|
336
|
+
*
|
|
337
|
+
* // Initialize with specific RNG type
|
|
338
|
+
* secrets.init(8, 'nodeCryptoRandomBytes');
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
init(bits, rngType) {
|
|
342
|
+
const logs = [];
|
|
343
|
+
const exps = [];
|
|
344
|
+
let x = 1;
|
|
345
|
+
let primitive;
|
|
346
|
+
reset();
|
|
347
|
+
if (bits &&
|
|
348
|
+
(typeof bits !== "number" ||
|
|
349
|
+
bits % 1 !== 0 ||
|
|
350
|
+
bits < defaults.minBits ||
|
|
351
|
+
bits > defaults.maxBits)) {
|
|
352
|
+
throw new Error("Number of bits must be an integer between " +
|
|
353
|
+
defaults.minBits +
|
|
354
|
+
" and " +
|
|
355
|
+
defaults.maxBits +
|
|
356
|
+
", inclusive.");
|
|
357
|
+
}
|
|
358
|
+
if (rngType && CSPRNGTypes.indexOf(rngType) === -1) {
|
|
359
|
+
throw new Error("Invalid RNG type argument : '" + rngType + "'");
|
|
360
|
+
}
|
|
361
|
+
config.radix = defaults.radix;
|
|
362
|
+
config.bits = bits || defaults.bits;
|
|
363
|
+
config.size = Math.pow(2, config.bits);
|
|
364
|
+
config.maxShares = config.size - 1;
|
|
365
|
+
primitive = defaults.primitivePolynomials[config.bits];
|
|
366
|
+
for (let i = 0; i < config.size; i++) {
|
|
367
|
+
exps[i] = x;
|
|
368
|
+
logs[x] = i;
|
|
369
|
+
x = x << 1;
|
|
370
|
+
if (x >= config.size) {
|
|
371
|
+
x = x ^ primitive;
|
|
372
|
+
x = x & config.maxShares;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
config.logs = logs;
|
|
376
|
+
config.exps = exps;
|
|
377
|
+
if (rngType) {
|
|
378
|
+
this.setRNG(rngType);
|
|
379
|
+
}
|
|
380
|
+
if (!isSetRNG()) {
|
|
381
|
+
this.setRNG();
|
|
382
|
+
}
|
|
383
|
+
if (!isSetRNG() ||
|
|
384
|
+
!config.bits ||
|
|
385
|
+
!config.size ||
|
|
386
|
+
!config.maxShares ||
|
|
387
|
+
!config.logs ||
|
|
388
|
+
!config.exps ||
|
|
389
|
+
config.logs.length !== config.size ||
|
|
390
|
+
config.exps.length !== config.size) {
|
|
391
|
+
throw new Error("Initialization failed.");
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
/**
|
|
395
|
+
* Combine shares to reconstruct the original secret.
|
|
396
|
+
*
|
|
397
|
+
* Uses Lagrange interpolation to reconstruct the secret from a threshold
|
|
398
|
+
* number of shares. The shares must have been created with the same bit
|
|
399
|
+
* configuration.
|
|
400
|
+
*
|
|
401
|
+
* @param shares - Array of share strings to combine. Must contain at least
|
|
402
|
+
* the threshold number of shares used during secret creation.
|
|
403
|
+
* @param at - Point at which to evaluate the polynomial (default 0 for secret recovery).
|
|
404
|
+
* Use non-zero values to generate new shares.
|
|
405
|
+
* @returns The reconstructed secret as a hexadecimal string.
|
|
406
|
+
* @throws {Error} If shares have mismatched bit settings.
|
|
407
|
+
* @throws {Error} If share format is invalid.
|
|
408
|
+
*
|
|
409
|
+
* @example
|
|
410
|
+
* ```typescript
|
|
411
|
+
* const shares = secrets.share('deadbeef', 5, 3);
|
|
412
|
+
* const secret = secrets.combine(shares.slice(0, 3));
|
|
413
|
+
* console.log(secret); // 'deadbeef'
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
combine(shares, at) {
|
|
417
|
+
let setBits;
|
|
418
|
+
let share;
|
|
419
|
+
let splitShare;
|
|
420
|
+
const x = [];
|
|
421
|
+
const y = [];
|
|
422
|
+
let result = "";
|
|
423
|
+
at = at || 0;
|
|
424
|
+
for (let i = 0, len = shares.length; i < len; i++) {
|
|
425
|
+
share = this.extractShareComponents(shares[i]);
|
|
426
|
+
if (setBits === undefined) {
|
|
427
|
+
setBits = share.bits;
|
|
428
|
+
}
|
|
429
|
+
else if (share.bits !== setBits) {
|
|
430
|
+
throw new Error("Mismatched shares: Different bit settings.");
|
|
431
|
+
}
|
|
432
|
+
if (config.bits !== setBits) {
|
|
433
|
+
this.init(setBits);
|
|
434
|
+
}
|
|
435
|
+
if (x.indexOf(share.id) === -1) {
|
|
436
|
+
x.push(share.id);
|
|
437
|
+
splitShare = splitNumStringToIntArray(hex2bin(share.data));
|
|
438
|
+
for (let j = 0, len2 = splitShare.length; j < len2; j++) {
|
|
439
|
+
y[j] = y[j] || [];
|
|
440
|
+
y[j][x.length - 1] = splitShare[j];
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
for (let i = 0, len = y.length; i < len; i++) {
|
|
445
|
+
result = padLeft(lagrange(at, x, y[i]).toString(2)) + result;
|
|
446
|
+
}
|
|
447
|
+
return bin2hex(at >= 1 ? result : result.slice(result.indexOf("1") + 1));
|
|
448
|
+
},
|
|
449
|
+
/**
|
|
450
|
+
* Get the current configuration of the secrets library.
|
|
451
|
+
*
|
|
452
|
+
* Returns information about the current Galois Field configuration,
|
|
453
|
+
* including bit length, radix, maximum shares, and RNG status.
|
|
454
|
+
*
|
|
455
|
+
* @returns Configuration object with current settings.
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```typescript
|
|
459
|
+
* const config = secrets.getConfig();
|
|
460
|
+
* console.log(`Max shares: ${config.maxShares}`);
|
|
461
|
+
* console.log(`Has CSPRNG: ${config.hasCSPRNG}`);
|
|
462
|
+
* console.log(`RNG Type: ${config.typeCSPRNG}`);
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
getConfig() {
|
|
466
|
+
const obj = {
|
|
467
|
+
radix: config.radix,
|
|
468
|
+
bits: config.bits,
|
|
469
|
+
maxShares: config.maxShares,
|
|
470
|
+
hasCSPRNG: isSetRNG(),
|
|
471
|
+
typeCSPRNG: config.typeCSPRNG
|
|
472
|
+
};
|
|
473
|
+
return obj;
|
|
474
|
+
},
|
|
475
|
+
/**
|
|
476
|
+
* Extract the components from a public share string.
|
|
477
|
+
*
|
|
478
|
+
* Parses a share string to extract the bit configuration, share ID,
|
|
479
|
+
* and share data. Useful for inspecting shares or validating share format.
|
|
480
|
+
*
|
|
481
|
+
* @param share - The share string to parse.
|
|
482
|
+
* @returns Object containing bits, id, and data components.
|
|
483
|
+
* @throws {Error} If share format is invalid.
|
|
484
|
+
* @throws {Error} If share ID is out of valid range.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* ```typescript
|
|
488
|
+
* const shares = secrets.share('abc123', 5, 3);
|
|
489
|
+
* const components = secrets.extractShareComponents(shares[0]);
|
|
490
|
+
* console.log(`Bits: ${components.bits}`);
|
|
491
|
+
* console.log(`ID: ${components.id}`);
|
|
492
|
+
* console.log(`Data: ${components.data}`);
|
|
493
|
+
* ```
|
|
494
|
+
*/
|
|
495
|
+
extractShareComponents(share) {
|
|
496
|
+
let bits;
|
|
497
|
+
let id;
|
|
498
|
+
let idLen;
|
|
499
|
+
let max;
|
|
500
|
+
let regexStr;
|
|
501
|
+
let shareComponents;
|
|
502
|
+
bits = parseInt(share.substr(0, 1), 36);
|
|
503
|
+
if (bits &&
|
|
504
|
+
(typeof bits !== "number" ||
|
|
505
|
+
bits % 1 !== 0 ||
|
|
506
|
+
bits < defaults.minBits ||
|
|
507
|
+
bits > defaults.maxBits)) {
|
|
508
|
+
throw new Error("Invalid share : Number of bits must be an integer between " +
|
|
509
|
+
defaults.minBits +
|
|
510
|
+
" and " +
|
|
511
|
+
defaults.maxBits +
|
|
512
|
+
", inclusive.");
|
|
513
|
+
}
|
|
514
|
+
max = Math.pow(2, bits) - 1;
|
|
515
|
+
idLen = (Math.pow(2, bits) - 1).toString(config.radix).length;
|
|
516
|
+
regexStr = "^([a-kA-K3-9]{1})([a-fA-F0-9]{" + idLen + "})([a-fA-F0-9]+)$";
|
|
517
|
+
shareComponents = new RegExp(regexStr).exec(share);
|
|
518
|
+
if (shareComponents) {
|
|
519
|
+
id = parseInt(shareComponents[2], config.radix);
|
|
520
|
+
}
|
|
521
|
+
if (typeof id !== "number" || id % 1 !== 0 || id < 1 || id > max) {
|
|
522
|
+
throw new Error("Invalid share : Share id must be an integer between 1 and " +
|
|
523
|
+
config.maxShares +
|
|
524
|
+
", inclusive.");
|
|
525
|
+
}
|
|
526
|
+
if (shareComponents && shareComponents[3]) {
|
|
527
|
+
return {
|
|
528
|
+
bits: bits,
|
|
529
|
+
id: id,
|
|
530
|
+
data: shareComponents[3]
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
throw new Error("The share data provided is invalid : " + share);
|
|
534
|
+
},
|
|
535
|
+
/**
|
|
536
|
+
* Set the random number generator to use for share generation.
|
|
537
|
+
*
|
|
538
|
+
* Allows specifying a custom RNG or selecting a specific CSPRNG type.
|
|
539
|
+
* If no argument is provided, automatically detects and uses the best
|
|
540
|
+
* available CSPRNG for the current environment.
|
|
541
|
+
*
|
|
542
|
+
* @param rng - Either a CSPRNG type string or a custom RNG function.
|
|
543
|
+
* Custom functions must return a binary string of specified length.
|
|
544
|
+
* @returns True if RNG was successfully set.
|
|
545
|
+
* @throws {Error} If RNG type is invalid.
|
|
546
|
+
* @throws {Error} If custom RNG function fails validation tests.
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* ```typescript
|
|
550
|
+
* // Use specific CSPRNG type
|
|
551
|
+
* secrets.setRNG('nodeCryptoRandomBytes');
|
|
552
|
+
*
|
|
553
|
+
* // Use custom RNG function
|
|
554
|
+
* secrets.setRNG((bits) => {
|
|
555
|
+
* // Return binary string of specified length
|
|
556
|
+
* return customRandomBits(bits);
|
|
557
|
+
* });
|
|
558
|
+
* ```
|
|
559
|
+
*/
|
|
560
|
+
setRNG(rng) {
|
|
561
|
+
const errPrefix = "Random number generator is invalid ";
|
|
562
|
+
const errSuffix = " Supply an CSPRNG of the form function(bits){} that returns a string containing 'bits' number of random 1's and 0's.";
|
|
563
|
+
if (rng &&
|
|
564
|
+
typeof rng === "string" &&
|
|
565
|
+
CSPRNGTypes.indexOf(rng) === -1) {
|
|
566
|
+
throw new Error("Invalid RNG type argument : '" + rng + "'");
|
|
567
|
+
}
|
|
568
|
+
let rngFunc;
|
|
569
|
+
if (!rng) {
|
|
570
|
+
rngFunc = getRNG();
|
|
571
|
+
}
|
|
572
|
+
else if (typeof rng === "string") {
|
|
573
|
+
rngFunc = getRNG(rng);
|
|
574
|
+
}
|
|
575
|
+
else {
|
|
576
|
+
rngFunc = rng;
|
|
577
|
+
}
|
|
578
|
+
if (runCSPRNGTest && rngFunc) {
|
|
579
|
+
if (typeof rngFunc !== "function") {
|
|
580
|
+
throw new Error(errPrefix + "(Not a function)." + errSuffix);
|
|
581
|
+
}
|
|
582
|
+
if (typeof rngFunc(config.bits) !== "string") {
|
|
583
|
+
throw new Error(errPrefix + "(Output is not a string)." + errSuffix);
|
|
584
|
+
}
|
|
585
|
+
if (!parseInt(rngFunc(config.bits), 2)) {
|
|
586
|
+
throw new Error(errPrefix +
|
|
587
|
+
"(Binary string output not parseable to an Integer)." +
|
|
588
|
+
errSuffix);
|
|
589
|
+
}
|
|
590
|
+
if (rngFunc(config.bits).length > config.bits) {
|
|
591
|
+
throw new Error(errPrefix + "(Output length is greater than config.bits)." + errSuffix);
|
|
592
|
+
}
|
|
593
|
+
if (rngFunc(config.bits).length < config.bits) {
|
|
594
|
+
throw new Error(errPrefix + "(Output length is less than config.bits)." + errSuffix);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
config.rng = rngFunc;
|
|
598
|
+
return true;
|
|
599
|
+
},
|
|
600
|
+
/**
|
|
601
|
+
* Convert a UTF-16 string to hexadecimal representation.
|
|
602
|
+
*
|
|
603
|
+
* Each character is represented by bytesPerChar bytes in the output.
|
|
604
|
+
* Useful for converting text secrets to hex format before sharing.
|
|
605
|
+
*
|
|
606
|
+
* @param str - The string to convert to hexadecimal.
|
|
607
|
+
* @param bytesPerChar - Number of bytes per character (1-6, default 2).
|
|
608
|
+
* Higher values support larger character codes.
|
|
609
|
+
* @returns Hexadecimal string representation.
|
|
610
|
+
* @throws {Error} If input is not a string.
|
|
611
|
+
* @throws {Error} If bytesPerChar is not an integer between 1 and 6.
|
|
612
|
+
* @throws {Error} If character code exceeds maximum for bytesPerChar.
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* ```typescript
|
|
616
|
+
* const hex = secrets.str2hex('Hello');
|
|
617
|
+
* const shares = secrets.share(hex, 5, 3);
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
str2hex(str, bytesPerChar) {
|
|
621
|
+
let hexChars;
|
|
622
|
+
let max;
|
|
623
|
+
let out = "";
|
|
624
|
+
let neededBytes;
|
|
625
|
+
let num;
|
|
626
|
+
if (typeof str !== "string") {
|
|
627
|
+
throw new Error("Input must be a character string.");
|
|
628
|
+
}
|
|
629
|
+
if (!bytesPerChar) {
|
|
630
|
+
bytesPerChar = defaults.bytesPerChar;
|
|
631
|
+
}
|
|
632
|
+
if (typeof bytesPerChar !== "number" ||
|
|
633
|
+
bytesPerChar < 1 ||
|
|
634
|
+
bytesPerChar > defaults.maxBytesPerChar ||
|
|
635
|
+
bytesPerChar % 1 !== 0) {
|
|
636
|
+
throw new Error("Bytes per character must be an integer between 1 and " +
|
|
637
|
+
defaults.maxBytesPerChar +
|
|
638
|
+
", inclusive.");
|
|
639
|
+
}
|
|
640
|
+
hexChars = 2 * bytesPerChar;
|
|
641
|
+
max = Math.pow(16, hexChars) - 1;
|
|
642
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
643
|
+
num = str[i].charCodeAt(0);
|
|
644
|
+
if (isNaN(num)) {
|
|
645
|
+
throw new Error("Invalid character: " + str[i]);
|
|
646
|
+
}
|
|
647
|
+
if (num > max) {
|
|
648
|
+
neededBytes = Math.ceil(Math.log(num + 1) / Math.log(256));
|
|
649
|
+
throw new Error("Invalid character code (" +
|
|
650
|
+
num +
|
|
651
|
+
"). Maximum allowable is 256^bytes-1 (" +
|
|
652
|
+
max +
|
|
653
|
+
"). To convert this character, use at least " +
|
|
654
|
+
neededBytes +
|
|
655
|
+
" bytes.");
|
|
656
|
+
}
|
|
657
|
+
out = padLeft(num.toString(16), hexChars) + out;
|
|
658
|
+
}
|
|
659
|
+
return out;
|
|
660
|
+
},
|
|
661
|
+
/**
|
|
662
|
+
* Convert a hexadecimal string to UTF-16 string representation.
|
|
663
|
+
*
|
|
664
|
+
* Reverses the str2hex operation. Each bytesPerChar bytes in the input
|
|
665
|
+
* represents one character in the output.
|
|
666
|
+
*
|
|
667
|
+
* @param str - The hexadecimal string to convert.
|
|
668
|
+
* @param bytesPerChar - Number of bytes per character (1-6, default 2).
|
|
669
|
+
* Must match the value used in str2hex.
|
|
670
|
+
* @returns UTF-16 string representation.
|
|
671
|
+
* @throws {Error} If input is not a hexadecimal string.
|
|
672
|
+
* @throws {Error} If bytesPerChar is not an integer between 1 and 6.
|
|
673
|
+
*
|
|
674
|
+
* @example
|
|
675
|
+
* ```typescript
|
|
676
|
+
* const shares = secrets.share(secrets.str2hex('Hello'), 5, 3);
|
|
677
|
+
* const recovered = secrets.hex2str(secrets.combine(shares));
|
|
678
|
+
* console.log(recovered); // 'Hello'
|
|
679
|
+
* ```
|
|
680
|
+
*/
|
|
681
|
+
hex2str(str, bytesPerChar) {
|
|
682
|
+
let hexChars;
|
|
683
|
+
let out = "";
|
|
684
|
+
if (typeof str !== "string") {
|
|
685
|
+
throw new Error("Input must be a hexadecimal string.");
|
|
686
|
+
}
|
|
687
|
+
bytesPerChar = bytesPerChar || defaults.bytesPerChar;
|
|
688
|
+
if (typeof bytesPerChar !== "number" ||
|
|
689
|
+
bytesPerChar % 1 !== 0 ||
|
|
690
|
+
bytesPerChar < 1 ||
|
|
691
|
+
bytesPerChar > defaults.maxBytesPerChar) {
|
|
692
|
+
throw new Error("Bytes per character must be an integer between 1 and " +
|
|
693
|
+
defaults.maxBytesPerChar +
|
|
694
|
+
", inclusive.");
|
|
695
|
+
}
|
|
696
|
+
hexChars = 2 * bytesPerChar;
|
|
697
|
+
str = padLeft(str, hexChars);
|
|
698
|
+
for (let i = 0, len = str.length; i < len; i += hexChars) {
|
|
699
|
+
out = String.fromCharCode(parseInt(str.slice(i, i + hexChars), 16)) + out;
|
|
700
|
+
}
|
|
701
|
+
return out;
|
|
702
|
+
},
|
|
703
|
+
/**
|
|
704
|
+
* Generate a random hexadecimal string of specified bit length.
|
|
705
|
+
*
|
|
706
|
+
* Uses the configured CSPRNG to generate cryptographically secure
|
|
707
|
+
* random numbers. Useful for generating random secrets.
|
|
708
|
+
*
|
|
709
|
+
* @param bits - Number of random bits to generate (2-65536).
|
|
710
|
+
* @returns Random hexadecimal string.
|
|
711
|
+
* @throws {Error} If bits is not an integer between 2 and 65536.
|
|
712
|
+
*
|
|
713
|
+
* @example
|
|
714
|
+
* ```typescript
|
|
715
|
+
* const randomSecret = secrets.random(128);
|
|
716
|
+
* const shares = secrets.share(randomSecret, 5, 3);
|
|
717
|
+
* ```
|
|
718
|
+
*/
|
|
719
|
+
random(bits) {
|
|
720
|
+
if (typeof bits !== "number" ||
|
|
721
|
+
bits % 1 !== 0 ||
|
|
722
|
+
bits < 2 ||
|
|
723
|
+
bits > 65536) {
|
|
724
|
+
throw new Error("Number of bits must be an Integer between 1 and 65536.");
|
|
725
|
+
}
|
|
726
|
+
return bin2hex(config.rng(bits));
|
|
727
|
+
},
|
|
728
|
+
/**
|
|
729
|
+
* Split a secret into shares using Shamir's Secret Sharing.
|
|
730
|
+
*
|
|
731
|
+
* Creates numShares shares such that any threshold number of shares
|
|
732
|
+
* can reconstruct the original secret, but fewer shares reveal no
|
|
733
|
+
* information about the secret.
|
|
734
|
+
*
|
|
735
|
+
* @param secret - The secret to split, as a hexadecimal string.
|
|
736
|
+
* @param numShares - Total number of shares to generate (2 to 2^bits-1).
|
|
737
|
+
* @param threshold - Minimum number of shares needed to reconstruct (2 to numShares).
|
|
738
|
+
* @param padLength - Zero-pad the secret to a multiple of this length (0-1024, default 128).
|
|
739
|
+
* @returns Array of share strings.
|
|
740
|
+
* @throws {Error} If secret is not a string.
|
|
741
|
+
* @throws {Error} If numShares or threshold are invalid.
|
|
742
|
+
* @throws {Error} If threshold exceeds numShares.
|
|
743
|
+
* @throws {Error} If padLength is invalid.
|
|
744
|
+
*
|
|
745
|
+
* @example
|
|
746
|
+
* ```typescript
|
|
747
|
+
* // Split a hex secret into 5 shares, requiring 3 to reconstruct
|
|
748
|
+
* const shares = secrets.share('deadbeef', 5, 3);
|
|
749
|
+
*
|
|
750
|
+
* // Any 3 shares can reconstruct the secret
|
|
751
|
+
* const recovered = secrets.combine([shares[0], shares[2], shares[4]]);
|
|
752
|
+
* console.log(recovered); // 'deadbeef'
|
|
753
|
+
* ```
|
|
754
|
+
*/
|
|
755
|
+
share(secret, numShares, threshold, padLength) {
|
|
756
|
+
let neededBits;
|
|
757
|
+
let subShares;
|
|
758
|
+
const x = new Array(numShares);
|
|
759
|
+
const y = new Array(numShares);
|
|
760
|
+
padLength = padLength || 128;
|
|
761
|
+
if (typeof secret !== "string") {
|
|
762
|
+
throw new Error("Secret must be a string.");
|
|
763
|
+
}
|
|
764
|
+
if (typeof numShares !== "number" ||
|
|
765
|
+
numShares % 1 !== 0 ||
|
|
766
|
+
numShares < 2) {
|
|
767
|
+
throw new Error("Number of shares must be an integer between 2 and 2^bits-1 (" +
|
|
768
|
+
config.maxShares +
|
|
769
|
+
"), inclusive.");
|
|
770
|
+
}
|
|
771
|
+
if (numShares > config.maxShares) {
|
|
772
|
+
neededBits = Math.ceil(Math.log(numShares + 1) / Math.LN2);
|
|
773
|
+
throw new Error("Number of shares must be an integer between 2 and 2^bits-1 (" +
|
|
774
|
+
config.maxShares +
|
|
775
|
+
"), inclusive. To create " +
|
|
776
|
+
numShares +
|
|
777
|
+
" shares, use at least " +
|
|
778
|
+
neededBits +
|
|
779
|
+
" bits.");
|
|
780
|
+
}
|
|
781
|
+
if (typeof threshold !== "number" ||
|
|
782
|
+
threshold % 1 !== 0 ||
|
|
783
|
+
threshold < 2) {
|
|
784
|
+
throw new Error("Threshold number of shares must be an integer between 2 and 2^bits-1 (" +
|
|
785
|
+
config.maxShares +
|
|
786
|
+
"), inclusive.");
|
|
787
|
+
}
|
|
788
|
+
if (threshold > config.maxShares) {
|
|
789
|
+
neededBits = Math.ceil(Math.log(threshold + 1) / Math.LN2);
|
|
790
|
+
throw new Error("Threshold number of shares must be an integer between 2 and 2^bits-1 (" +
|
|
791
|
+
config.maxShares +
|
|
792
|
+
"), inclusive. To use a threshold of " +
|
|
793
|
+
threshold +
|
|
794
|
+
", use at least " +
|
|
795
|
+
neededBits +
|
|
796
|
+
" bits.");
|
|
797
|
+
}
|
|
798
|
+
if (threshold > numShares) {
|
|
799
|
+
throw new Error("Threshold number of shares was " +
|
|
800
|
+
threshold +
|
|
801
|
+
" but must be less than or equal to the " +
|
|
802
|
+
numShares +
|
|
803
|
+
" shares specified as the total to generate.");
|
|
804
|
+
}
|
|
805
|
+
if (typeof padLength !== "number" ||
|
|
806
|
+
padLength % 1 !== 0 ||
|
|
807
|
+
padLength < 0 ||
|
|
808
|
+
padLength > 1024) {
|
|
809
|
+
throw new Error("Zero-pad length must be an integer between 0 and 1024 inclusive.");
|
|
810
|
+
}
|
|
811
|
+
let secretBin = "1" + hex2bin(secret);
|
|
812
|
+
const secretParts = splitNumStringToIntArray(secretBin, padLength);
|
|
813
|
+
for (let i = 0, len = secretParts.length; i < len; i++) {
|
|
814
|
+
subShares = getShares(secretParts[i], numShares, threshold);
|
|
815
|
+
for (let j = 0; j < numShares; j++) {
|
|
816
|
+
x[j] = x[j] || subShares[j].x;
|
|
817
|
+
y[j] = padLeft(subShares[j].y.toString(2)) + (y[j] || "");
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
for (let i = 0; i < numShares; i++) {
|
|
821
|
+
x[i] = constructPublicShareString(config.bits, x[i], bin2hex(y[i]));
|
|
822
|
+
}
|
|
823
|
+
return x;
|
|
824
|
+
},
|
|
825
|
+
/**
|
|
826
|
+
* Generate a new share with a specific ID from existing shares.
|
|
827
|
+
*
|
|
828
|
+
* Uses Lagrange interpolation to create a new share at the specified
|
|
829
|
+
* ID point. Useful for generating additional shares without access to
|
|
830
|
+
* the original secret.
|
|
831
|
+
*
|
|
832
|
+
* @param id - The ID for the new share (1 to 2^bits-1).
|
|
833
|
+
* @param shares - Array of existing shares (at least threshold number).
|
|
834
|
+
* @returns New share string with the specified ID.
|
|
835
|
+
* @throws {Error} If id is invalid.
|
|
836
|
+
* @throws {Error} If shares array is invalid or empty.
|
|
837
|
+
*
|
|
838
|
+
* @example
|
|
839
|
+
* ```typescript
|
|
840
|
+
* const shares = secrets.share('abc123', 5, 3);
|
|
841
|
+
* // Generate a new share with ID 10
|
|
842
|
+
* const newShare = secrets.newShare(10, shares.slice(0, 3));
|
|
843
|
+
* ```
|
|
844
|
+
*/
|
|
845
|
+
newShare(id, shares) {
|
|
846
|
+
let share;
|
|
847
|
+
let numericId;
|
|
848
|
+
if (typeof id === "string") {
|
|
849
|
+
numericId = parseInt(id, 10);
|
|
850
|
+
}
|
|
851
|
+
else if (typeof id === "number") {
|
|
852
|
+
numericId = Math.floor(id);
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
numericId = NaN;
|
|
856
|
+
}
|
|
857
|
+
if (numericId && shares && shares[0]) {
|
|
858
|
+
share = this.extractShareComponents(shares[0]);
|
|
859
|
+
return constructPublicShareString(share.bits, numericId, this.combine(shares, numericId));
|
|
860
|
+
}
|
|
861
|
+
throw new Error("Invalid 'id' or 'shares' Array argument to newShare().");
|
|
862
|
+
},
|
|
863
|
+
// Private functions exported for testing
|
|
864
|
+
_reset: reset,
|
|
865
|
+
_padLeft: padLeft,
|
|
866
|
+
_hex2bin: hex2bin,
|
|
867
|
+
_bin2hex: bin2hex,
|
|
868
|
+
_bytesToHex: bytesToHex,
|
|
869
|
+
_hasCryptoGetRandomValues: hasCryptoGetRandomValues,
|
|
870
|
+
_hasCryptoRandomBytes: hasCryptoRandomBytes,
|
|
871
|
+
_getRNG: getRNG,
|
|
872
|
+
_isSetRNG: isSetRNG,
|
|
873
|
+
_splitNumStringToIntArray: splitNumStringToIntArray,
|
|
874
|
+
_horner: horner,
|
|
875
|
+
_lagrange: lagrange,
|
|
876
|
+
_getShares: getShares,
|
|
877
|
+
_constructPublicShareString: constructPublicShareString
|
|
878
|
+
};
|
|
879
|
+
// Initialize with default settings
|
|
880
|
+
SecretsLibrary.init();
|
|
881
|
+
// Export for different module systems
|
|
882
|
+
// For CommonJS/Node.js
|
|
883
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
884
|
+
module.exports = SecretsLibrary;
|
|
885
|
+
}
|
|
886
|
+
// For browser globals (UMD)
|
|
887
|
+
if (typeof window !== 'undefined') {
|
|
888
|
+
window.secrets = SecretsLibrary;
|
|
889
|
+
}
|
|
890
|
+
// For ES Modules
|
|
891
|
+
export default SecretsLibrary;
|
|
892
|
+
// Named exports for ES Modules
|
|
893
|
+
export const { init, combine, getConfig, extractShareComponents, setRNG, str2hex, hex2str, random, share, newShare } = SecretsLibrary;
|
|
894
|
+
//# sourceMappingURL=secrets.js.map
|