Skip to content

Commit f790057

Browse files
committed
Test create syntax
1 parent 4ce352f commit f790057

30 files changed

+208
-30
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* Fix WebAssembly detection
44
* Fix crash when starting many parallel hash calculations
5-
* Allocate less memory
5+
* Allocate less memory for WASM modules
66

77
## 2.5.1 (April 13, 2020)
88

jest.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = {
22
roots: [
3-
'<rootDir>/test'
3+
'<rootDir>/test',
44
],
55
transform: {
6-
'^.+\\.ts$': 'ts-jest'
6+
'^.+\\.ts$': 'ts-jest',
77
},
88
cacheDirectory: '<rootDir>/.jest-cache',
99
};

lib/keccak.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function createKeccak(bits: IValidBits = 512) {
3333
if (!wasm) wasm = tempWasm;
3434
}
3535

36-
wasm.init();
36+
wasm.init(bits);
3737

3838
return {
3939
init: () => wasm.init(bits),

lib/sha224.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function createSHA224() {
1919
const tempWasm = await WASMInterface(wasmJson, 28);
2020
if (!wasm) wasm = tempWasm;
2121
}
22-
wasm.init();
22+
23+
wasm.init(224);
2324

2425
return {
2526
init: () => wasm.init(224),

lib/sha256.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function createSHA256() {
1919
const tempWasm = await WASMInterface(wasmJson, 32);
2020
if (!wasm) wasm = tempWasm;
2121
}
22-
wasm.init();
22+
23+
wasm.init(256);
2324

2425
return {
2526
init: () => wasm.init(256),

lib/sha3.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export async function createSHA3(bits: IValidBits = 512) {
3232
const tempWasm = await WASMInterface(wasmJson, bits / 8);
3333
if (!wasm) wasm = tempWasm;
3434
}
35-
wasm.init();
35+
36+
wasm.init(bits);
3637

3738
return {
3839
init: () => wasm.init(bits),

lib/sha384.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function createSHA384() {
1919
const tempWasm = await WASMInterface(wasmJson, 48);
2020
if (!wasm) wasm = tempWasm;
2121
}
22-
wasm.init();
22+
23+
wasm.init(384);
2324

2425
return {
2526
init: () => wasm.init(384),

lib/sha512.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function createSHA512() {
1919
const tempWasm = await WASMInterface(wasmJson, 64);
2020
if (!wasm) wasm = tempWasm;
2121
}
22-
wasm.init();
22+
23+
wasm.init(512);
2324

2425
return {
2526
init: () => wasm.init(512),

lib/xxhash32.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export async function createXXHash32(seed = 0) {
3131
const tempWasm = await WASMInterface(wasmJson, 4);
3232
if (!wasm) wasm = tempWasm;
3333
}
34-
wasm.init();
34+
35+
wasm.init(seed);
3536

3637
return {
3738
init: () => wasm.init(seed),

lib/xxhash64.ts

+2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export async function createXXHash64(seedLow = 0, seedHigh = 0) {
4343
const tempWasm = await WASMInterface(wasmJson, 8);
4444
if (!wasm) wasm = tempWasm;
4545
}
46+
4647
writeSeed(seedLow, seedHigh);
48+
4749
wasm.writeMemory(new Uint32Array(seedBuffer));
4850
wasm.init();
4951

rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default {
1212
{
1313
file: 'dist/index.esm.js',
1414
format: 'es',
15-
}
15+
},
1616
],
1717
plugins: [
1818
json(),

scripts/make_json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs');
22
const path = require('path');
33
const dir = path.resolve(__dirname, '..', 'wasm');
4-
const files = fs.readdirSync(dir).filter(file => file.endsWith('.wasm'));
4+
const files = fs.readdirSync(dir).filter((file) => file.endsWith('.wasm'));
55

66
files.forEach((file) => {
77
const data = fs.readFileSync(path.join(dir, file));

test/crc32.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { crc32 } from '../lib';
2+
import { crc32, createCRC32 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -51,3 +51,12 @@ test('long buffers', async () => {
5151
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5252
expect(await crc32(buf)).toBe('8717a175');
5353
});
54+
55+
test('chunked', async () => {
56+
const hash = await createCRC32();
57+
expect(hash.digest()).toBe('00000000');
58+
hash.init();
59+
hash.update('a');
60+
hash.update(new Uint8Array([0]));
61+
expect(hash.digest()).toBe('3d3f4819');
62+
});

test/keccak-224.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { keccak } from '../lib';
2+
import { keccak, createKeccak } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await keccak(buf, 224)).toBe('81eadc502f5e5969929e6b707990d98bd096f1aa084500b5ed6d3fab');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createKeccak(224);
56+
expect(hash.digest()).toBe('f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('1b914ebf869b542b9d8440e07ca1dfe5da48ebb1c563e928ded523c3');
61+
});

test/keccak-256.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { keccak } from '../lib';
2+
import { keccak, createKeccak } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await keccak(buf, 256)).toBe('ee2baea5067d74104d62c153ea178dfbfeef70a800a74e333d81ed4c1bb828ff');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createKeccak(256);
56+
expect(hash.digest()).toBe('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('a3fe1181ce8d13858f6f383445749f49a3ae8b0cab89823918bab81153ca4300');
61+
});

test/keccak-384.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { keccak } from '../lib';
2+
import { keccak, createKeccak } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await keccak(buf, 384)).toBe('590f148219ae72d6ed25476ae7ee2c469d21daeed95f54b8ce8c900a337a1d3e04304ac211731fe8b7953d9d208364aa');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createKeccak(384);
56+
expect(hash.digest()).toBe('2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('028bf394389395fe49cda14bee0b5b54333babd65a9861e57c882b9ec7d752800a0a9d7abba9fdfe1c0f7dbe17378bab');
61+
});

test/keccak-512.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { keccak } from '../lib';
2+
import { keccak, createKeccak } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await keccak(buf)).toBe('7e9ee5244865c5101d1fc2bba5b67aa099eb2abf234b84bdc89011360c3f7cc7f241a362911f54296b24e70b9427131cb21ad7ded4c44d873c323eda3ad0b06a');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createKeccak(512);
56+
expect(hash.digest()).toBe('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('50470286ea9f645134c527432303a7187a2a1451956148a1228d94b33edbf35ba9146301e43ddb84491469ccf1ca72cec501032df5e16958232a24ba90a93fb0');
61+
});

test/md4.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { md4 } from '../lib';
2+
import { md4, createMD4 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await md4(buf)).toBe('249f0de16d9c9498cb6810a51489d8e0');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createMD4();
56+
expect(hash.digest()).toBe('31d6cfe0d16ae931b73c59d7e0c089c0');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('186cb09181e2c2ecaac768c47c729904');
61+
});

test/md5.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { md5 } from '../lib';
2+
import { md5, createMD5 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await md5(buf)).toBe('f195aef51a25af5d29ca871eb3780c06');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createMD5();
56+
expect(hash.digest()).toBe('d41d8cd98f00b204e9800998ecf8427e');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('4144e195f46de78a3623da7364d04f11');
61+
});

test/sha1.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { sha1 } from '../lib';
2+
import { sha1, createSHA1 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await sha1(buf)).toBe('6677c7445dd30888d61009461afe79df58d7721b');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createSHA1();
56+
expect(hash.digest()).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('0a04b971b03da607ce6c455184037b660ca89f78');
61+
});

test/sha224.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { sha224 } from '../lib';
2+
import { sha224, createSHA224 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await sha224(buf)).toBe('927c4d3dcc570b35d21a0e8043b808d967a8b30a7ba56633c6b36625');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createSHA224();
56+
expect(hash.digest()).toBe('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('3118199937a95dd0dd06a74ac0bf1517e958f08ae87ef9d7e89f139a');
61+
});

test/sha256.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { sha256 } from '../lib';
2+
import { sha256, createSHA256 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await sha256(buf)).toBe('58d45217f7307541ae6d450d03cd0eb11979bdcd9d535584f1299c99139c1479');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createSHA256();
56+
expect(hash.digest()).toBe('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('ffe9aaeaa2a2d5048174df0b80599ef0197ec024c4b051bc9860cff58ef7f9f3');
61+
});

test/sha3-224.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { sha3 } from '../lib';
2+
import { sha3, createSHA3 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await sha3(buf, 224)).toBe('7feec941280e634553f242f3ec6c92ff3c174c141b6a4578232bdb48');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createSHA3(224);
56+
expect(hash.digest()).toBe('6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('853ee21e10638dd5d5a30ad979d7c0d1b91145fec39c8197637ce9d8');
61+
});

test/sha3-256.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { sha3 } from '../lib';
2+
import { sha3, createSHA3 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await sha3(buf, 256)).toBe('5fcaad8772a4d9e0d378e364eda70aaef208c88962acee744e81d946c9ca0390');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createSHA3(256);
56+
expect(hash.digest()).toBe('a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('39fdad608c5b60008da2f12414441f5f664472792c8bc1567a9fbae617800604');
61+
});

test/sha3-384.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { sha3 } from '../lib';
2+
import { sha3, createSHA3 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await sha3(buf, 384)).toBe('c06ed93fc6432516c12a6d1563c8c32f7e36d94d30a887866ac23da4869368956235f657950355b1d09cdbd61eaff061');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createSHA3(384);
56+
expect(hash.digest()).toBe('0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('03f38a5f45cd7742b1529999f875d9896d73030cad2a037b5ba56271cd140c6c4f5997a033e890ecbcf72ce7d5cab512');
61+
});

test/sha3-512.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { sha3 } from '../lib';
2+
import { sha3, createSHA3 } from '../lib';
33
/* global test, expect */
44

55
test('simple strings', async () => {
@@ -50,3 +50,12 @@ test('long buffers', async () => {
5050
buf.fill('\x00\x01\x02\x03\x04\x05\x06\x07\x08\xFF');
5151
expect(await sha3(buf)).toBe('4fefa4aacf871180c27f83ec93f2d058758a6ecba564747fd681a067f9438638fc1e70362121fa64d1734d77ad09eff7822c8bc108761598a9a12e816e24ad57');
5252
});
53+
54+
test('chunked', async () => {
55+
const hash = await createSHA3(512);
56+
expect(hash.digest()).toBe('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
57+
hash.init();
58+
hash.update('a');
59+
hash.update(new Uint8Array([0]));
60+
expect(hash.digest()).toBe('8d9b65030b4721341fcff7d39811d5acbd65c730500b4a0c58aaa5150b5ec490d3508edda2d3a8a4f32a5428e39c64dc9ebf2b44edfab27863221c8b633d3fc6');
61+
});

0 commit comments

Comments
 (0)