js-sha3 0.9.3 → 0.11.0
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/CHANGELOG.md +12 -0
- package/README.md +38 -3
- package/build/sha3.min.js +2 -2
- package/build/sha3.min.mjs +9 -0
- package/build/sha3.mjs +848 -0
- package/index.d.ts +98 -0
- package/package.json +23 -4
- package/rollup.config.mjs +23 -0
- package/src/sha3.js +143 -6
- package/src/sha3.mjs +43 -0
package/index.d.ts
CHANGED
|
@@ -291,6 +291,96 @@ interface KmacHash {
|
|
|
291
291
|
update(key: Message, message: Message, outputBits: number, customization: Message): Hasher;
|
|
292
292
|
}
|
|
293
293
|
|
|
294
|
+
type TupleInput = Message[];
|
|
295
|
+
|
|
296
|
+
interface TupleHash extends Hasher {
|
|
297
|
+
/**
|
|
298
|
+
* Start a streaming tuple input with a known UTF-8/binary byte length.
|
|
299
|
+
*
|
|
300
|
+
* @param byteLength The byte length of the input that will follow through updateChunk().
|
|
301
|
+
*/
|
|
302
|
+
beginInput(byteLength: number): TupleHash;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Absorb part of the active streaming tuple input.
|
|
306
|
+
*
|
|
307
|
+
* @param message The next message chunk.
|
|
308
|
+
*/
|
|
309
|
+
updateChunk(message: Message): TupleHash;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Append one complete tuple input string.
|
|
313
|
+
* Equivalent to beginInput(byteLength) followed by updateChunk(message).
|
|
314
|
+
*
|
|
315
|
+
* @param message The input string to encode and absorb.
|
|
316
|
+
*/
|
|
317
|
+
update(message: Message): TupleHash;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
interface TupleHashMethod {
|
|
321
|
+
/**
|
|
322
|
+
* Hash a tuple and return hex string.
|
|
323
|
+
*
|
|
324
|
+
* @param inputs The tuple of input strings.
|
|
325
|
+
* @param outputBits The length of output.
|
|
326
|
+
* @param customization The customization string.
|
|
327
|
+
*/
|
|
328
|
+
(inputs: TupleInput, outputBits: number, customization: Message): string;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Hash a tuple and return hex string.
|
|
332
|
+
*
|
|
333
|
+
* @param inputs The tuple of input strings.
|
|
334
|
+
* @param outputBits The length of output.
|
|
335
|
+
* @param customization The customization string.
|
|
336
|
+
*/
|
|
337
|
+
hex(inputs: TupleInput, outputBits: number, customization: Message): string;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Hash a tuple and return ArrayBuffer.
|
|
341
|
+
*
|
|
342
|
+
* @param inputs The tuple of input strings.
|
|
343
|
+
* @param outputBits The length of output.
|
|
344
|
+
* @param customization The customization string.
|
|
345
|
+
*/
|
|
346
|
+
arrayBuffer(inputs: TupleInput, outputBits: number, customization: Message): ArrayBuffer;
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Hash a tuple and return integer array.
|
|
350
|
+
*
|
|
351
|
+
* @param inputs The tuple of input strings.
|
|
352
|
+
* @param outputBits The length of output.
|
|
353
|
+
* @param customization The customization string.
|
|
354
|
+
*/
|
|
355
|
+
digest(inputs: TupleInput, outputBits: number, customization: Message): number[];
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Hash a tuple and return integer array.
|
|
359
|
+
*
|
|
360
|
+
* @param inputs The tuple of input strings.
|
|
361
|
+
* @param outputBits The length of output.
|
|
362
|
+
* @param customization The customization string.
|
|
363
|
+
*/
|
|
364
|
+
array(inputs: TupleInput, outputBits: number, customization: Message): number[];
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Create a TupleHash object.
|
|
368
|
+
*
|
|
369
|
+
* @param outputBits The length of output.
|
|
370
|
+
* @param customization The customization string.
|
|
371
|
+
*/
|
|
372
|
+
create(outputBits: number, customization: Message): TupleHash;
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Create a TupleHash object and absorb the given tuple inputs.
|
|
376
|
+
*
|
|
377
|
+
* @param inputs The tuple of input strings.
|
|
378
|
+
* @param outputBits The length of output.
|
|
379
|
+
* @param customization The customization string.
|
|
380
|
+
*/
|
|
381
|
+
update(inputs: TupleInput, outputBits: number, customization: Message): TupleHash;
|
|
382
|
+
}
|
|
383
|
+
|
|
294
384
|
export var sha3_512: Hash;
|
|
295
385
|
export var sha3_384: Hash;
|
|
296
386
|
export var sha3_256: Hash;
|
|
@@ -315,3 +405,11 @@ export var kmac_128: KmacHash;
|
|
|
315
405
|
export var kmac_256: KmacHash;
|
|
316
406
|
export var kmac128: KmacHash;
|
|
317
407
|
export var kmac256: KmacHash;
|
|
408
|
+
export var tuplehash_128: TupleHashMethod;
|
|
409
|
+
export var tuplehash_256: TupleHashMethod;
|
|
410
|
+
export var tuplehash128: TupleHashMethod;
|
|
411
|
+
export var tuplehash256: TupleHashMethod;
|
|
412
|
+
export var tuplehashxof_128: TupleHashMethod;
|
|
413
|
+
export var tuplehashxof_256: TupleHashMethod;
|
|
414
|
+
export var tuplehashxof128: TupleHashMethod;
|
|
415
|
+
export var tuplehashxof256: TupleHashMethod;
|
package/package.json
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-sha3",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A simple SHA-3 / Keccak /
|
|
3
|
+
"version": "0.11.0",
|
|
4
|
+
"description": "A simple SHA-3 / Keccak / SHAKE / cSHAKE / KMAC / TupleHash hash function for JavaScript supports UTF-8 encoding.",
|
|
5
5
|
"main": "src/sha3.js",
|
|
6
|
+
"module": "build/sha3.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"import": "./build/sha3.mjs",
|
|
11
|
+
"require": "./src/sha3.js",
|
|
12
|
+
"default": "./src/sha3.js"
|
|
13
|
+
},
|
|
14
|
+
"./src/sha3.js": "./src/sha3.js",
|
|
15
|
+
"./build/sha3.min.js": "./build/sha3.min.js",
|
|
16
|
+
"./build/sha3.mjs": "./build/sha3.mjs",
|
|
17
|
+
"./build/sha3.min.mjs": "./build/sha3.min.mjs",
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"types": "./index.d.ts",
|
|
6
21
|
"devDependencies": {
|
|
22
|
+
"@rollup/plugin-commonjs": "^28.0.5",
|
|
23
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
7
24
|
"expect.js": "~0.3.1",
|
|
8
|
-
"mocha": "
|
|
25
|
+
"mocha": "^11.7.6",
|
|
9
26
|
"nyc": "^15.1.0",
|
|
27
|
+
"rollup": "^4.43.0",
|
|
10
28
|
"tiny-worker": "^2.3.0",
|
|
11
29
|
"uglify-js": "^3.1.9"
|
|
12
30
|
},
|
|
@@ -14,7 +32,7 @@
|
|
|
14
32
|
"test": "nyc mocha tests/node-test.js",
|
|
15
33
|
"report": "nyc --reporter=html --reporter=text mocha tests/node-test.js",
|
|
16
34
|
"coveralls": "nyc report --reporter=text-lcov | coveralls",
|
|
17
|
-
"build": "uglifyjs src/sha3.js -c -m --comments --output build/sha3.min.js"
|
|
35
|
+
"build": "rollup -c && uglifyjs src/sha3.js -c -m --comments --output build/sha3.min.js"
|
|
18
36
|
},
|
|
19
37
|
"repository": {
|
|
20
38
|
"type": "git",
|
|
@@ -26,6 +44,7 @@
|
|
|
26
44
|
"shake",
|
|
27
45
|
"cshake",
|
|
28
46
|
"kmac",
|
|
47
|
+
"tuplehash",
|
|
29
48
|
"hash",
|
|
30
49
|
"encryption",
|
|
31
50
|
"cryptography",
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
2
|
+
import terser from '@rollup/plugin-terser';
|
|
3
|
+
|
|
4
|
+
export default [
|
|
5
|
+
{
|
|
6
|
+
input: 'src/sha3.mjs',
|
|
7
|
+
output: {
|
|
8
|
+
file: 'build/sha3.mjs',
|
|
9
|
+
format: 'es'
|
|
10
|
+
},
|
|
11
|
+
plugins: [commonjs({
|
|
12
|
+
strictRequires: false
|
|
13
|
+
})]
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
input: 'build/sha3.mjs',
|
|
17
|
+
output: {
|
|
18
|
+
file: 'build/sha3.min.mjs',
|
|
19
|
+
format: 'es'
|
|
20
|
+
},
|
|
21
|
+
plugins: [terser()]
|
|
22
|
+
}
|
|
23
|
+
];
|
package/src/sha3.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
|
3
3
|
*
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.11.0
|
|
5
5
|
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
|
6
6
|
* @copyright Chen, Yi-Cyuan 2015-2023
|
|
7
7
|
* @license MIT
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
|
|
13
13
|
var INPUT_ERROR = 'input is invalid type';
|
|
14
14
|
var FINALIZE_ERROR = 'finalize already called';
|
|
15
|
+
var TUPLE_ACTIVE_ERROR = 'no active tuple input';
|
|
16
|
+
var TUPLE_INCOMPLETE_ERROR = 'tuple input is incomplete';
|
|
17
|
+
var TUPLE_LENGTH_ERROR = 'tuple input exceeds declared length';
|
|
18
|
+
var TUPLE_BYTE_LENGTH_ERROR = 'tuple input byte length is invalid';
|
|
15
19
|
var WINDOW = typeof window === 'object';
|
|
16
20
|
var root = WINDOW ? window : {};
|
|
17
21
|
if (root.JS_SHA3_NO_WINDOW) {
|
|
@@ -171,12 +175,45 @@
|
|
|
171
175
|
return createOutputMethods(method, createKmacOutputMethod, bits, padding);
|
|
172
176
|
};
|
|
173
177
|
|
|
178
|
+
var createTupleHashOutputMethod = function (bits, padding, xof, outputType) {
|
|
179
|
+
return function (inputs, outputBits, s) {
|
|
180
|
+
return methods[(xof ? 'tuplehashxof' : 'tuplehash') + bits].update(inputs, outputBits, s)[outputType]();
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
var createTupleHashMethod = function (bits, padding, xof) {
|
|
185
|
+
var w = CSHAKE_BYTEPAD[bits];
|
|
186
|
+
var method = createTupleHashOutputMethod(bits, padding, xof, 'hex');
|
|
187
|
+
method.create = function (outputBits, s) {
|
|
188
|
+
return new TupleHash(bits, padding, outputBits, xof).bytepad(['TupleHash', s], w);
|
|
189
|
+
};
|
|
190
|
+
method.update = function (inputs, outputBits, s) {
|
|
191
|
+
if (!isArray(inputs)) {
|
|
192
|
+
throw new Error(INPUT_ERROR);
|
|
193
|
+
}
|
|
194
|
+
var hash = method.create(outputBits, s);
|
|
195
|
+
for (var i = 0; i < inputs.length; ++i) {
|
|
196
|
+
hash.update(inputs[i]);
|
|
197
|
+
}
|
|
198
|
+
return hash;
|
|
199
|
+
};
|
|
200
|
+
return createOutputMethods(method, function (b, p, outputType) {
|
|
201
|
+
return createTupleHashOutputMethod(b, p, xof, outputType);
|
|
202
|
+
}, bits, padding);
|
|
203
|
+
};
|
|
204
|
+
|
|
174
205
|
var algorithms = [
|
|
175
206
|
{ name: 'keccak', padding: KECCAK_PADDING, bits: BITS, createMethod: createMethod },
|
|
176
207
|
{ name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod },
|
|
177
208
|
{ name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod },
|
|
178
209
|
{ name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod },
|
|
179
|
-
{ name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod }
|
|
210
|
+
{ name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod },
|
|
211
|
+
{ name: 'tuplehash', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: function (bits, padding) {
|
|
212
|
+
return createTupleHashMethod(bits, padding, false);
|
|
213
|
+
}},
|
|
214
|
+
{ name: 'tuplehashxof', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: function (bits, padding) {
|
|
215
|
+
return createTupleHashMethod(bits, padding, true);
|
|
216
|
+
}}
|
|
180
217
|
];
|
|
181
218
|
|
|
182
219
|
var methods = {}, methodNames = [];
|
|
@@ -290,7 +327,7 @@
|
|
|
290
327
|
} else {
|
|
291
328
|
bytes.unshift(n);
|
|
292
329
|
}
|
|
293
|
-
|
|
330
|
+
Keccak.prototype.update.call(this, bytes);
|
|
294
331
|
return bytes.length;
|
|
295
332
|
};
|
|
296
333
|
|
|
@@ -317,7 +354,7 @@
|
|
|
317
354
|
bytes = length;
|
|
318
355
|
}
|
|
319
356
|
bytes += this.encode(bytes * 8);
|
|
320
|
-
|
|
357
|
+
Keccak.prototype.update.call(this, str);
|
|
321
358
|
return bytes;
|
|
322
359
|
};
|
|
323
360
|
|
|
@@ -329,7 +366,7 @@
|
|
|
329
366
|
var paddingBytes = (w - bytes % w) % w;
|
|
330
367
|
var zeros = [];
|
|
331
368
|
zeros.length = paddingBytes;
|
|
332
|
-
|
|
369
|
+
Keccak.prototype.update.call(this, zeros);
|
|
333
370
|
return this;
|
|
334
371
|
};
|
|
335
372
|
|
|
@@ -458,7 +495,107 @@
|
|
|
458
495
|
Kmac.prototype = new Keccak();
|
|
459
496
|
|
|
460
497
|
Kmac.prototype.finalize = function () {
|
|
461
|
-
|
|
498
|
+
if (!this.finalized) {
|
|
499
|
+
this.encode(this.outputBits, true);
|
|
500
|
+
}
|
|
501
|
+
return Keccak.prototype.finalize.call(this);
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
function TupleHash(bits, padding, outputBits, xof) {
|
|
505
|
+
Keccak.call(this, bits, padding, outputBits);
|
|
506
|
+
this.xof = !!xof;
|
|
507
|
+
this.inputActive = false;
|
|
508
|
+
this.inputBytesRemaining = 0;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
TupleHash.prototype = new Keccak();
|
|
512
|
+
|
|
513
|
+
TupleHash.prototype.getMessageByteLength = function (message) {
|
|
514
|
+
var result = formatMessage(message);
|
|
515
|
+
message = result[0];
|
|
516
|
+
if (!result[1]) {
|
|
517
|
+
return message.length;
|
|
518
|
+
}
|
|
519
|
+
var bytes = 0;
|
|
520
|
+
for (var i = 0; i < message.length; ++i) {
|
|
521
|
+
var code = message.charCodeAt(i);
|
|
522
|
+
if (code < 0x80) {
|
|
523
|
+
bytes += 1;
|
|
524
|
+
} else if (code < 0x800) {
|
|
525
|
+
bytes += 2;
|
|
526
|
+
} else if (code < 0xd800 || code >= 0xe000) {
|
|
527
|
+
bytes += 3;
|
|
528
|
+
} else {
|
|
529
|
+
++i;
|
|
530
|
+
bytes += 4;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return bytes;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
TupleHash.prototype.beginInput = function (byteLength) {
|
|
537
|
+
if (this.finalized) {
|
|
538
|
+
throw new Error(FINALIZE_ERROR);
|
|
539
|
+
}
|
|
540
|
+
if (this.inputActive) {
|
|
541
|
+
throw new Error(TUPLE_INCOMPLETE_ERROR);
|
|
542
|
+
}
|
|
543
|
+
if (typeof byteLength !== 'number' || !isFinite(byteLength) || byteLength < 0 ||
|
|
544
|
+
Math.floor(byteLength) !== byteLength || byteLength > 0x0fffffff) {
|
|
545
|
+
throw new Error(TUPLE_BYTE_LENGTH_ERROR);
|
|
546
|
+
}
|
|
547
|
+
this.encode(byteLength * 8, false);
|
|
548
|
+
if (byteLength === 0) {
|
|
549
|
+
this.inputActive = false;
|
|
550
|
+
this.inputBytesRemaining = 0;
|
|
551
|
+
} else {
|
|
552
|
+
this.inputActive = true;
|
|
553
|
+
this.inputBytesRemaining = byteLength;
|
|
554
|
+
}
|
|
555
|
+
return this;
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
TupleHash.prototype.updateChunk = function (message) {
|
|
559
|
+
if (this.finalized) {
|
|
560
|
+
throw new Error(FINALIZE_ERROR);
|
|
561
|
+
}
|
|
562
|
+
if (!this.inputActive) {
|
|
563
|
+
throw new Error(TUPLE_ACTIVE_ERROR);
|
|
564
|
+
}
|
|
565
|
+
var byteLength = this.getMessageByteLength(message);
|
|
566
|
+
if (byteLength > this.inputBytesRemaining) {
|
|
567
|
+
throw new Error(TUPLE_LENGTH_ERROR);
|
|
568
|
+
}
|
|
569
|
+
Keccak.prototype.update.call(this, message);
|
|
570
|
+
this.inputBytesRemaining -= byteLength;
|
|
571
|
+
if (this.inputBytesRemaining === 0) {
|
|
572
|
+
this.inputActive = false;
|
|
573
|
+
}
|
|
574
|
+
return this;
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
TupleHash.prototype.update = function (message) {
|
|
578
|
+
if (this.finalized) {
|
|
579
|
+
throw new Error(FINALIZE_ERROR);
|
|
580
|
+
}
|
|
581
|
+
if (this.inputActive) {
|
|
582
|
+
throw new Error(TUPLE_INCOMPLETE_ERROR);
|
|
583
|
+
}
|
|
584
|
+
var byteLength = this.getMessageByteLength(message);
|
|
585
|
+
this.beginInput(byteLength);
|
|
586
|
+
if (byteLength === 0) {
|
|
587
|
+
return this;
|
|
588
|
+
}
|
|
589
|
+
return this.updateChunk(message);
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
TupleHash.prototype.finalize = function () {
|
|
593
|
+
if (this.inputActive) {
|
|
594
|
+
throw new Error(TUPLE_INCOMPLETE_ERROR);
|
|
595
|
+
}
|
|
596
|
+
if (!this.finalized) {
|
|
597
|
+
this.encode(this.xof ? 0 : this.outputBits, true);
|
|
598
|
+
}
|
|
462
599
|
return Keccak.prototype.finalize.call(this);
|
|
463
600
|
};
|
|
464
601
|
|
package/src/sha3.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import sha3 from './sha3.js';
|
|
2
|
+
|
|
3
|
+
export const {
|
|
4
|
+
sha3_224,
|
|
5
|
+
sha3_256,
|
|
6
|
+
sha3_384,
|
|
7
|
+
sha3_512,
|
|
8
|
+
|
|
9
|
+
keccak_224,
|
|
10
|
+
keccak_256,
|
|
11
|
+
keccak_384,
|
|
12
|
+
keccak_512,
|
|
13
|
+
keccak224,
|
|
14
|
+
keccak256,
|
|
15
|
+
keccak384,
|
|
16
|
+
keccak512,
|
|
17
|
+
|
|
18
|
+
shake_128,
|
|
19
|
+
shake_256,
|
|
20
|
+
shake128,
|
|
21
|
+
shake256,
|
|
22
|
+
|
|
23
|
+
cshake_128,
|
|
24
|
+
cshake_256,
|
|
25
|
+
cshake128,
|
|
26
|
+
cshake256,
|
|
27
|
+
|
|
28
|
+
kmac_128,
|
|
29
|
+
kmac_256,
|
|
30
|
+
kmac128,
|
|
31
|
+
kmac256,
|
|
32
|
+
|
|
33
|
+
tuplehash_128,
|
|
34
|
+
tuplehash_256,
|
|
35
|
+
tuplehash128,
|
|
36
|
+
tuplehash256,
|
|
37
|
+
tuplehashxof_128,
|
|
38
|
+
tuplehashxof_256,
|
|
39
|
+
tuplehashxof128,
|
|
40
|
+
tuplehashxof256
|
|
41
|
+
} = sha3;
|
|
42
|
+
|
|
43
|
+
export default sha3;
|