zuzu-js 0.1.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/LICENSE +5 -0
- package/README.md +113 -0
- package/bin/zuzu +17 -0
- package/bin/zuzu-build-browser-bundle +57 -0
- package/bin/zuzu-generate-browser-stdlib +584 -0
- package/bin/zuzu-js +23 -0
- package/bin/zuzu-js-compile +152 -0
- package/bin/zuzu-js-electron +19 -0
- package/dist/zuzu-browser-worker.js +45574 -0
- package/dist/zuzu-browser.js +45362 -0
- package/lib/browser-bundle-entry.js +160 -0
- package/lib/browser-gui-renderer.js +387 -0
- package/lib/browser-runtime.js +167 -0
- package/lib/browser-worker-entry.js +413 -0
- package/lib/browser-ztests/runner.html +103 -0
- package/lib/browser-ztests/runner.js +369 -0
- package/lib/cli.js +350 -0
- package/lib/collections.js +367 -0
- package/lib/compiler.js +303 -0
- package/lib/electron/launcher.js +70 -0
- package/lib/electron/main.js +956 -0
- package/lib/electron/preload.js +80 -0
- package/lib/electron/renderer.html +122 -0
- package/lib/electron/renderer.js +24 -0
- package/lib/execution-metadata.js +18 -0
- package/lib/gui/dom-renderer.js +778 -0
- package/lib/host/browser-host.js +278 -0
- package/lib/host/capabilities.js +47 -0
- package/lib/host/electron-host.js +15 -0
- package/lib/host/node-host.js +74 -0
- package/lib/paths.js +150 -0
- package/lib/runtime-entrypoints.js +60 -0
- package/lib/runtime-helpers.js +886 -0
- package/lib/runtime.js +3529 -0
- package/lib/tap.js +37 -0
- package/lib/transpiler-new/ast.js +23 -0
- package/lib/transpiler-new/codegen.js +2455 -0
- package/lib/transpiler-new/errors.js +28 -0
- package/lib/transpiler-new/index.js +26 -0
- package/lib/transpiler-new/lexer.js +834 -0
- package/lib/transpiler-new/parser.js +2332 -0
- package/lib/transpiler-new/validate-bindings.js +326 -0
- package/lib/transpiler-utils.js +95 -0
- package/lib/transpiler.js +33 -0
- package/lib/zuzu.js +53 -0
- package/modules/javascript.js +193 -0
- package/modules/std/archive.js +603 -0
- package/modules/std/clib.js +338 -0
- package/modules/std/data/csv.js +1331 -0
- package/modules/std/data/json.js +531 -0
- package/modules/std/data/xml.js +441 -0
- package/modules/std/data/yaml.js +256 -0
- package/modules/std/db-worker.js +250 -0
- package/modules/std/db.js +664 -0
- package/modules/std/digest/_hash.js +443 -0
- package/modules/std/digest/md5.js +26 -0
- package/modules/std/digest/sha.js +72 -0
- package/modules/std/eval.js +10 -0
- package/modules/std/gui/objects.js +1519 -0
- package/modules/std/internals.js +571 -0
- package/modules/std/io/socks-worker.js +318 -0
- package/modules/std/io/socks.js +186 -0
- package/modules/std/io.js +475 -0
- package/modules/std/marshal/cbor.js +463 -0
- package/modules/std/marshal/graph.js +1624 -0
- package/modules/std/marshal.js +87 -0
- package/modules/std/math/bignum.js +91 -0
- package/modules/std/math.js +79 -0
- package/modules/std/net/dns.js +306 -0
- package/modules/std/net/http.js +820 -0
- package/modules/std/net/smtp.js +943 -0
- package/modules/std/net/url.js +109 -0
- package/modules/std/proc.js +602 -0
- package/modules/std/secure.js +3724 -0
- package/modules/std/string/base64.js +138 -0
- package/modules/std/string.js +299 -0
- package/modules/std/task.js +914 -0
- package/modules/std/time.js +579 -0
- package/modules/std/tui.js +188 -0
- package/modules/std/worker-thread.js +246 -0
- package/modules/std/worker.js +790 -0
- package/package.json +67 -0
- package/stdlib/modules/javascript.zzm +99 -0
- package/stdlib/modules/perl.zzm +105 -0
- package/stdlib/modules/std/archive.zzm +132 -0
- package/stdlib/modules/std/cache/lru.zzm +174 -0
- package/stdlib/modules/std/clib.zzm +112 -0
- package/stdlib/modules/std/colour.zzm +220 -0
- package/stdlib/modules/std/config.zzm +818 -0
- package/stdlib/modules/std/data/cbor.zzm +497 -0
- package/stdlib/modules/std/data/csv.zzm +285 -0
- package/stdlib/modules/std/data/ini.zzm +472 -0
- package/stdlib/modules/std/data/json/schema/core.zzm +573 -0
- package/stdlib/modules/std/data/json/schema/format.zzm +581 -0
- package/stdlib/modules/std/data/json/schema/model.zzm +255 -0
- package/stdlib/modules/std/data/json/schema/output.zzm +272 -0
- package/stdlib/modules/std/data/json/schema/relative_pointer.zzm +299 -0
- package/stdlib/modules/std/data/json/schema/validation.zzm +1503 -0
- package/stdlib/modules/std/data/json/schema.zzm +306 -0
- package/stdlib/modules/std/data/json.zzm +102 -0
- package/stdlib/modules/std/data/kdl/json.zzm +460 -0
- package/stdlib/modules/std/data/kdl/xml.zzm +387 -0
- package/stdlib/modules/std/data/kdl.zzm +1631 -0
- package/stdlib/modules/std/data/toml.zzm +756 -0
- package/stdlib/modules/std/data/toon.zzm +1017 -0
- package/stdlib/modules/std/data/xml/escape.zzm +156 -0
- package/stdlib/modules/std/data/xml.zzm +276 -0
- package/stdlib/modules/std/data/yaml.zzm +94 -0
- package/stdlib/modules/std/db.zzm +173 -0
- package/stdlib/modules/std/defer.zzm +75 -0
- package/stdlib/modules/std/digest/crc32.zzm +196 -0
- package/stdlib/modules/std/digest/md5.zzm +54 -0
- package/stdlib/modules/std/digest/sha.zzm +83 -0
- package/stdlib/modules/std/dump.zzm +317 -0
- package/stdlib/modules/std/eval.zzm +63 -0
- package/stdlib/modules/std/getopt.zzm +432 -0
- package/stdlib/modules/std/gui/dialogue.zzm +592 -0
- package/stdlib/modules/std/gui/objects.zzm +123 -0
- package/stdlib/modules/std/gui.zzm +1914 -0
- package/stdlib/modules/std/internals.zzm +139 -0
- package/stdlib/modules/std/io/socks.zzm +139 -0
- package/stdlib/modules/std/io.zzm +157 -0
- package/stdlib/modules/std/lingua/en.zzm +347 -0
- package/stdlib/modules/std/log.zzm +169 -0
- package/stdlib/modules/std/mail.zzm +2726 -0
- package/stdlib/modules/std/marshal.zzm +138 -0
- package/stdlib/modules/std/math/bignum.zzm +98 -0
- package/stdlib/modules/std/math/range.zzm +116 -0
- package/stdlib/modules/std/math/roman.zzm +156 -0
- package/stdlib/modules/std/math.zzm +141 -0
- package/stdlib/modules/std/net/dns.zzm +93 -0
- package/stdlib/modules/std/net/http.zzm +278 -0
- package/stdlib/modules/std/net/smtp.zzm +257 -0
- package/stdlib/modules/std/net/url.zzm +69 -0
- package/stdlib/modules/std/path/jsonpointer.zzm +526 -0
- package/stdlib/modules/std/path/kdl.zzm +1003 -0
- package/stdlib/modules/std/path/simple.zzm +520 -0
- package/stdlib/modules/std/path/z/context.zzm +147 -0
- package/stdlib/modules/std/path/z/evaluate.zzm +549 -0
- package/stdlib/modules/std/path/z/functions.zzm +874 -0
- package/stdlib/modules/std/path/z/lexer.zzm +490 -0
- package/stdlib/modules/std/path/z/node.zzm +1455 -0
- package/stdlib/modules/std/path/z/operators.zzm +445 -0
- package/stdlib/modules/std/path/z/parser.zzm +359 -0
- package/stdlib/modules/std/path/z.zzm +403 -0
- package/stdlib/modules/std/path/zz/functions.zzm +828 -0
- package/stdlib/modules/std/path/zz/operators.zzm +1036 -0
- package/stdlib/modules/std/path/zz.zzm +100 -0
- package/stdlib/modules/std/proc.zzm +155 -0
- package/stdlib/modules/std/result.zzm +149 -0
- package/stdlib/modules/std/secure.zzm +606 -0
- package/stdlib/modules/std/string/base64.zzm +66 -0
- package/stdlib/modules/std/string/quoted_printable.zzm +485 -0
- package/stdlib/modules/std/string.zzm +179 -0
- package/stdlib/modules/std/task.zzm +221 -0
- package/stdlib/modules/std/template/z.zzm +531 -0
- package/stdlib/modules/std/template/zz.zzm +62 -0
- package/stdlib/modules/std/time.zzm +188 -0
- package/stdlib/modules/std/tui.zzm +89 -0
- package/stdlib/modules/std/uuid.zzm +223 -0
- package/stdlib/modules/std/web/session.zzm +388 -0
- package/stdlib/modules/std/web/static.zzm +329 -0
- package/stdlib/modules/std/web.zzm +1942 -0
- package/stdlib/modules/std/worker.zzm +202 -0
- package/stdlib/modules/std/zuzuzoo.zzm +3960 -0
- package/stdlib/modules/test/more.zzm +528 -0
- package/stdlib/modules/test/parser.zzm +209 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { BinaryString } = require( '../../../lib/runtime-helpers' );
|
|
4
|
+
|
|
5
|
+
function valueType( value ) {
|
|
6
|
+
if ( value == null ) {
|
|
7
|
+
return 'Null';
|
|
8
|
+
}
|
|
9
|
+
if ( typeof value === 'string' ) {
|
|
10
|
+
return 'String';
|
|
11
|
+
}
|
|
12
|
+
return value.constructor && value.constructor.name ? value.constructor.name : typeof value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function assertBinary( value, fnName, label = 'BinaryString' ) {
|
|
16
|
+
if ( !value || !( value.bytes instanceof Uint8Array ) ) {
|
|
17
|
+
throw new Error( `TypeException: ${fnName} expects ${label}, got ${valueType( value )}` );
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function toBinary( bytes ) {
|
|
22
|
+
return new BinaryString( Uint8Array.from( bytes ) );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function bytesToHex( bytes ) {
|
|
26
|
+
return Array.from( bytes, (byte) => byte.toString( 16 ).padStart( 2, '0' ) ).join( '' );
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function bytesToBase64( bytes ) {
|
|
30
|
+
if ( typeof Buffer !== 'undefined' ) {
|
|
31
|
+
return Buffer.from( bytes ).toString( 'base64' ).replace( /=+$/u, '' );
|
|
32
|
+
}
|
|
33
|
+
let text = '';
|
|
34
|
+
for ( let i = 0; i < bytes.length; i++ ) {
|
|
35
|
+
text += String.fromCharCode( bytes[i] );
|
|
36
|
+
}
|
|
37
|
+
return btoa( text ).replace( /=+$/u, '' );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function concatBytes( ...items ) {
|
|
41
|
+
const total = items.reduce( (sum, item) => sum + item.length, 0 );
|
|
42
|
+
const out = new Uint8Array( total );
|
|
43
|
+
let offset = 0;
|
|
44
|
+
for ( const item of items ) {
|
|
45
|
+
out.set( item, offset );
|
|
46
|
+
offset += item.length;
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function padded64( bytes, littleEndianLength = false ) {
|
|
52
|
+
const bitLength = bytes.length * 8;
|
|
53
|
+
const total = Math.ceil( ( bytes.length + 9 ) / 64 ) * 64;
|
|
54
|
+
const out = new Uint8Array( total );
|
|
55
|
+
out.set( bytes );
|
|
56
|
+
out[bytes.length] = 0x80;
|
|
57
|
+
if ( littleEndianLength ) {
|
|
58
|
+
let n = bitLength;
|
|
59
|
+
for ( let i = 0; i < 8; i++ ) {
|
|
60
|
+
out[total - 8 + i] = n & 0xff;
|
|
61
|
+
n = Math.floor( n / 256 );
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
let n = bitLength;
|
|
66
|
+
for ( let i = 7; i >= 0; i-- ) {
|
|
67
|
+
out[total - 8 + i] = n & 0xff;
|
|
68
|
+
n = Math.floor( n / 256 );
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function padded128( bytes ) {
|
|
75
|
+
const bitLength = BigInt( bytes.length ) * 8n;
|
|
76
|
+
const total = Math.ceil( ( bytes.length + 17 ) / 128 ) * 128;
|
|
77
|
+
const out = new Uint8Array( total );
|
|
78
|
+
out.set( bytes );
|
|
79
|
+
out[bytes.length] = 0x80;
|
|
80
|
+
let n = bitLength;
|
|
81
|
+
for ( let i = 15; i >= 0; i-- ) {
|
|
82
|
+
out[total - 16 + i] = Number( n & 0xffn );
|
|
83
|
+
n >>= 8n;
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function rotl32( value, bits ) {
|
|
89
|
+
return ( ( value << bits ) | ( value >>> ( 32 - bits ) ) ) >>> 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function rotr32( value, bits ) {
|
|
93
|
+
return ( ( value >>> bits ) | ( value << ( 32 - bits ) ) ) >>> 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function add32( ...values ) {
|
|
97
|
+
return values.reduce( (sum, value) => ( sum + value ) >>> 0, 0 );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function md5Bytes( bytes ) {
|
|
101
|
+
const s = [
|
|
102
|
+
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
|
103
|
+
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
|
104
|
+
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
|
105
|
+
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
|
|
106
|
+
];
|
|
107
|
+
const k = Array.from(
|
|
108
|
+
{ length: 64 },
|
|
109
|
+
( _value, i ) => Math.floor( Math.abs( Math.sin( i + 1 ) ) * 0x100000000 ) >>> 0,
|
|
110
|
+
);
|
|
111
|
+
const data = padded64( bytes, true );
|
|
112
|
+
let a0 = 0x67452301;
|
|
113
|
+
let b0 = 0xefcdab89;
|
|
114
|
+
let c0 = 0x98badcfe;
|
|
115
|
+
let d0 = 0x10325476;
|
|
116
|
+
|
|
117
|
+
for ( let offset = 0; offset < data.length; offset += 64 ) {
|
|
118
|
+
const m = new Uint32Array( 16 );
|
|
119
|
+
for ( let i = 0; i < 16; i++ ) {
|
|
120
|
+
const j = offset + i * 4;
|
|
121
|
+
m[i] = data[j] | ( data[j + 1] << 8 ) | ( data[j + 2] << 16 ) | ( data[j + 3] << 24 );
|
|
122
|
+
}
|
|
123
|
+
let a = a0;
|
|
124
|
+
let b = b0;
|
|
125
|
+
let c = c0;
|
|
126
|
+
let d = d0;
|
|
127
|
+
for ( let i = 0; i < 64; i++ ) {
|
|
128
|
+
let f;
|
|
129
|
+
let g;
|
|
130
|
+
if ( i < 16 ) {
|
|
131
|
+
f = ( b & c ) | ( ( ~b ) & d );
|
|
132
|
+
g = i;
|
|
133
|
+
}
|
|
134
|
+
else if ( i < 32 ) {
|
|
135
|
+
f = ( d & b ) | ( ( ~d ) & c );
|
|
136
|
+
g = ( 5 * i + 1 ) % 16;
|
|
137
|
+
}
|
|
138
|
+
else if ( i < 48 ) {
|
|
139
|
+
f = b ^ c ^ d;
|
|
140
|
+
g = ( 3 * i + 5 ) % 16;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
f = c ^ ( b | ( ~d ) );
|
|
144
|
+
g = ( 7 * i ) % 16;
|
|
145
|
+
}
|
|
146
|
+
const tmp = d;
|
|
147
|
+
d = c;
|
|
148
|
+
c = b;
|
|
149
|
+
b = add32( b, rotl32( add32( a, f, k[i], m[g] ), s[i] ) );
|
|
150
|
+
a = tmp;
|
|
151
|
+
}
|
|
152
|
+
a0 = add32( a0, a );
|
|
153
|
+
b0 = add32( b0, b );
|
|
154
|
+
c0 = add32( c0, c );
|
|
155
|
+
d0 = add32( d0, d );
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const out = new Uint8Array( 16 );
|
|
159
|
+
for ( const [ index, value ] of [ a0, b0, c0, d0 ].entries() ) {
|
|
160
|
+
out[index * 4] = value & 0xff;
|
|
161
|
+
out[index * 4 + 1] = ( value >>> 8 ) & 0xff;
|
|
162
|
+
out[index * 4 + 2] = ( value >>> 16 ) & 0xff;
|
|
163
|
+
out[index * 4 + 3] = ( value >>> 24 ) & 0xff;
|
|
164
|
+
}
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function sha1Bytes( bytes ) {
|
|
169
|
+
const data = padded64( bytes );
|
|
170
|
+
let h0 = 0x67452301;
|
|
171
|
+
let h1 = 0xefcdab89;
|
|
172
|
+
let h2 = 0x98badcfe;
|
|
173
|
+
let h3 = 0x10325476;
|
|
174
|
+
let h4 = 0xc3d2e1f0;
|
|
175
|
+
for ( let offset = 0; offset < data.length; offset += 64 ) {
|
|
176
|
+
const w = new Uint32Array( 80 );
|
|
177
|
+
for ( let i = 0; i < 16; i++ ) {
|
|
178
|
+
const j = offset + i * 4;
|
|
179
|
+
w[i] = ( data[j] << 24 ) | ( data[j + 1] << 16 ) | ( data[j + 2] << 8 ) | data[j + 3];
|
|
180
|
+
}
|
|
181
|
+
for ( let i = 16; i < 80; i++ ) {
|
|
182
|
+
w[i] = rotl32( w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1 );
|
|
183
|
+
}
|
|
184
|
+
let a = h0;
|
|
185
|
+
let b = h1;
|
|
186
|
+
let c = h2;
|
|
187
|
+
let d = h3;
|
|
188
|
+
let e = h4;
|
|
189
|
+
for ( let i = 0; i < 80; i++ ) {
|
|
190
|
+
let f;
|
|
191
|
+
let k;
|
|
192
|
+
if ( i < 20 ) {
|
|
193
|
+
f = ( b & c ) | ( ( ~b ) & d );
|
|
194
|
+
k = 0x5a827999;
|
|
195
|
+
}
|
|
196
|
+
else if ( i < 40 ) {
|
|
197
|
+
f = b ^ c ^ d;
|
|
198
|
+
k = 0x6ed9eba1;
|
|
199
|
+
}
|
|
200
|
+
else if ( i < 60 ) {
|
|
201
|
+
f = ( b & c ) | ( b & d ) | ( c & d );
|
|
202
|
+
k = 0x8f1bbcdc;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
f = b ^ c ^ d;
|
|
206
|
+
k = 0xca62c1d6;
|
|
207
|
+
}
|
|
208
|
+
const temp = add32( rotl32( a, 5 ), f, e, k, w[i] );
|
|
209
|
+
e = d;
|
|
210
|
+
d = c;
|
|
211
|
+
c = rotl32( b, 30 );
|
|
212
|
+
b = a;
|
|
213
|
+
a = temp;
|
|
214
|
+
}
|
|
215
|
+
h0 = add32( h0, a );
|
|
216
|
+
h1 = add32( h1, b );
|
|
217
|
+
h2 = add32( h2, c );
|
|
218
|
+
h3 = add32( h3, d );
|
|
219
|
+
h4 = add32( h4, e );
|
|
220
|
+
}
|
|
221
|
+
return words32ToBytes( [ h0, h1, h2, h3, h4 ] );
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const SHA256_K = [
|
|
225
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
226
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
227
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
228
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
229
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
230
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
231
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
232
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
233
|
+
];
|
|
234
|
+
|
|
235
|
+
function words32ToBytes( words ) {
|
|
236
|
+
const out = new Uint8Array( words.length * 4 );
|
|
237
|
+
for ( let i = 0; i < words.length; i++ ) {
|
|
238
|
+
out[i * 4] = ( words[i] >>> 24 ) & 0xff;
|
|
239
|
+
out[i * 4 + 1] = ( words[i] >>> 16 ) & 0xff;
|
|
240
|
+
out[i * 4 + 2] = ( words[i] >>> 8 ) & 0xff;
|
|
241
|
+
out[i * 4 + 3] = words[i] & 0xff;
|
|
242
|
+
}
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function sha256Core( bytes, initial ) {
|
|
247
|
+
const data = padded64( bytes );
|
|
248
|
+
const h = initial.slice();
|
|
249
|
+
for ( let offset = 0; offset < data.length; offset += 64 ) {
|
|
250
|
+
const w = new Uint32Array( 64 );
|
|
251
|
+
for ( let i = 0; i < 16; i++ ) {
|
|
252
|
+
const j = offset + i * 4;
|
|
253
|
+
w[i] = ( data[j] << 24 ) | ( data[j + 1] << 16 ) | ( data[j + 2] << 8 ) | data[j + 3];
|
|
254
|
+
}
|
|
255
|
+
for ( let i = 16; i < 64; i++ ) {
|
|
256
|
+
const s0 = rotr32( w[i - 15], 7 ) ^ rotr32( w[i - 15], 18 ) ^ ( w[i - 15] >>> 3 );
|
|
257
|
+
const s1 = rotr32( w[i - 2], 17 ) ^ rotr32( w[i - 2], 19 ) ^ ( w[i - 2] >>> 10 );
|
|
258
|
+
w[i] = add32( w[i - 16], s0, w[i - 7], s1 );
|
|
259
|
+
}
|
|
260
|
+
let [ a, b, c, d, e, f, g, hh ] = h;
|
|
261
|
+
for ( let i = 0; i < 64; i++ ) {
|
|
262
|
+
const s1 = rotr32( e, 6 ) ^ rotr32( e, 11 ) ^ rotr32( e, 25 );
|
|
263
|
+
const ch = ( e & f ) ^ ( ( ~e ) & g );
|
|
264
|
+
const temp1 = add32( hh, s1, ch, SHA256_K[i], w[i] );
|
|
265
|
+
const s0 = rotr32( a, 2 ) ^ rotr32( a, 13 ) ^ rotr32( a, 22 );
|
|
266
|
+
const maj = ( a & b ) ^ ( a & c ) ^ ( b & c );
|
|
267
|
+
const temp2 = add32( s0, maj );
|
|
268
|
+
hh = g;
|
|
269
|
+
g = f;
|
|
270
|
+
f = e;
|
|
271
|
+
e = add32( d, temp1 );
|
|
272
|
+
d = c;
|
|
273
|
+
c = b;
|
|
274
|
+
b = a;
|
|
275
|
+
a = add32( temp1, temp2 );
|
|
276
|
+
}
|
|
277
|
+
for ( const [ i, value ] of [ a, b, c, d, e, f, g, hh ].entries() ) {
|
|
278
|
+
h[i] = add32( h[i], value );
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return words32ToBytes( h );
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function sha224Bytes( bytes ) {
|
|
285
|
+
return sha256Core(
|
|
286
|
+
bytes,
|
|
287
|
+
[ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 ],
|
|
288
|
+
).slice( 0, 28 );
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function sha256Bytes( bytes ) {
|
|
292
|
+
return sha256Core(
|
|
293
|
+
bytes,
|
|
294
|
+
[ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ],
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const MASK64 = ( 1n << 64n ) - 1n;
|
|
299
|
+
const SHA512_K = [
|
|
300
|
+
0x428a2f98d728ae22n, 0x7137449123ef65cdn, 0xb5c0fbcfec4d3b2fn, 0xe9b5dba58189dbbcn,
|
|
301
|
+
0x3956c25bf348b538n, 0x59f111f1b605d019n, 0x923f82a4af194f9bn, 0xab1c5ed5da6d8118n,
|
|
302
|
+
0xd807aa98a3030242n, 0x12835b0145706fben, 0x243185be4ee4b28cn, 0x550c7dc3d5ffb4e2n,
|
|
303
|
+
0x72be5d74f27b896fn, 0x80deb1fe3b1696b1n, 0x9bdc06a725c71235n, 0xc19bf174cf692694n,
|
|
304
|
+
0xe49b69c19ef14ad2n, 0xefbe4786384f25e3n, 0x0fc19dc68b8cd5b5n, 0x240ca1cc77ac9c65n,
|
|
305
|
+
0x2de92c6f592b0275n, 0x4a7484aa6ea6e483n, 0x5cb0a9dcbd41fbd4n, 0x76f988da831153b5n,
|
|
306
|
+
0x983e5152ee66dfabn, 0xa831c66d2db43210n, 0xb00327c898fb213fn, 0xbf597fc7beef0ee4n,
|
|
307
|
+
0xc6e00bf33da88fc2n, 0xd5a79147930aa725n, 0x06ca6351e003826fn, 0x142929670a0e6e70n,
|
|
308
|
+
0x27b70a8546d22ffcn, 0x2e1b21385c26c926n, 0x4d2c6dfc5ac42aedn, 0x53380d139d95b3dfn,
|
|
309
|
+
0x650a73548baf63den, 0x766a0abb3c77b2a8n, 0x81c2c92e47edaee6n, 0x92722c851482353bn,
|
|
310
|
+
0xa2bfe8a14cf10364n, 0xa81a664bbc423001n, 0xc24b8b70d0f89791n, 0xc76c51a30654be30n,
|
|
311
|
+
0xd192e819d6ef5218n, 0xd69906245565a910n, 0xf40e35855771202an, 0x106aa07032bbd1b8n,
|
|
312
|
+
0x19a4c116b8d2d0c8n, 0x1e376c085141ab53n, 0x2748774cdf8eeb99n, 0x34b0bcb5e19b48a8n,
|
|
313
|
+
0x391c0cb3c5c95a63n, 0x4ed8aa4ae3418acbn, 0x5b9cca4f7763e373n, 0x682e6ff3d6b2b8a3n,
|
|
314
|
+
0x748f82ee5defb2fcn, 0x78a5636f43172f60n, 0x84c87814a1f0ab72n, 0x8cc702081a6439ecn,
|
|
315
|
+
0x90befffa23631e28n, 0xa4506cebde82bde9n, 0xbef9a3f7b2c67915n, 0xc67178f2e372532bn,
|
|
316
|
+
0xca273eceea26619cn, 0xd186b8c721c0c207n, 0xeada7dd6cde0eb1en, 0xf57d4f7fee6ed178n,
|
|
317
|
+
0x06f067aa72176fban, 0x0a637dc5a2c898a6n, 0x113f9804bef90daen, 0x1b710b35131c471bn,
|
|
318
|
+
0x28db77f523047d84n, 0x32caab7b40c72493n, 0x3c9ebe0a15c9bebcn, 0x431d67c49c100d4cn,
|
|
319
|
+
0x4cc5d4becb3e42b6n, 0x597f299cfc657e2an, 0x5fcb6fab3ad6faecn, 0x6c44198c4a475817n,
|
|
320
|
+
];
|
|
321
|
+
|
|
322
|
+
function rotr64( value, bits ) {
|
|
323
|
+
const n = BigInt( bits );
|
|
324
|
+
return ( ( value >> n ) | ( value << ( 64n - n ) ) ) & MASK64;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function add64( ...values ) {
|
|
328
|
+
return values.reduce( (sum, value) => ( sum + value ) & MASK64, 0n );
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function words64ToBytes( words ) {
|
|
332
|
+
const out = new Uint8Array( words.length * 8 );
|
|
333
|
+
for ( let i = 0; i < words.length; i++ ) {
|
|
334
|
+
let value = words[i];
|
|
335
|
+
for ( let j = 7; j >= 0; j-- ) {
|
|
336
|
+
out[i * 8 + j] = Number( value & 0xffn );
|
|
337
|
+
value >>= 8n;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function sha512Core( bytes, initial ) {
|
|
344
|
+
const data = padded128( bytes );
|
|
345
|
+
const h = initial.slice();
|
|
346
|
+
for ( let offset = 0; offset < data.length; offset += 128 ) {
|
|
347
|
+
const w = new Array( 80 ).fill( 0n );
|
|
348
|
+
for ( let i = 0; i < 16; i++ ) {
|
|
349
|
+
let value = 0n;
|
|
350
|
+
for ( let j = 0; j < 8; j++ ) {
|
|
351
|
+
value = ( value << 8n ) | BigInt( data[offset + i * 8 + j] );
|
|
352
|
+
}
|
|
353
|
+
w[i] = value;
|
|
354
|
+
}
|
|
355
|
+
for ( let i = 16; i < 80; i++ ) {
|
|
356
|
+
const s0 = rotr64( w[i - 15], 1 ) ^ rotr64( w[i - 15], 8 ) ^ ( w[i - 15] >> 7n );
|
|
357
|
+
const s1 = rotr64( w[i - 2], 19 ) ^ rotr64( w[i - 2], 61 ) ^ ( w[i - 2] >> 6n );
|
|
358
|
+
w[i] = add64( w[i - 16], s0, w[i - 7], s1 );
|
|
359
|
+
}
|
|
360
|
+
let [ a, b, c, d, e, f, g, hh ] = h;
|
|
361
|
+
for ( let i = 0; i < 80; i++ ) {
|
|
362
|
+
const s1 = rotr64( e, 14 ) ^ rotr64( e, 18 ) ^ rotr64( e, 41 );
|
|
363
|
+
const ch = ( e & f ) ^ ( ( ~e ) & g );
|
|
364
|
+
const temp1 = add64( hh, s1, ch, SHA512_K[i], w[i] );
|
|
365
|
+
const s0 = rotr64( a, 28 ) ^ rotr64( a, 34 ) ^ rotr64( a, 39 );
|
|
366
|
+
const maj = ( a & b ) ^ ( a & c ) ^ ( b & c );
|
|
367
|
+
const temp2 = add64( s0, maj );
|
|
368
|
+
hh = g;
|
|
369
|
+
g = f;
|
|
370
|
+
f = e;
|
|
371
|
+
e = add64( d, temp1 );
|
|
372
|
+
d = c;
|
|
373
|
+
c = b;
|
|
374
|
+
b = a;
|
|
375
|
+
a = add64( temp1, temp2 );
|
|
376
|
+
}
|
|
377
|
+
for ( const [ i, value ] of [ a, b, c, d, e, f, g, hh ].entries() ) {
|
|
378
|
+
h[i] = add64( h[i], value );
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return words64ToBytes( h );
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function sha384Bytes( bytes ) {
|
|
385
|
+
return sha512Core(
|
|
386
|
+
bytes,
|
|
387
|
+
[
|
|
388
|
+
0xcbbb9d5dc1059ed8n, 0x629a292a367cd507n, 0x9159015a3070dd17n, 0x152fecd8f70e5939n,
|
|
389
|
+
0x67332667ffc00b31n, 0x8eb44a8768581511n, 0xdb0c2e0d64f98fa7n, 0x47b5481dbefa4fa4n,
|
|
390
|
+
],
|
|
391
|
+
).slice( 0, 48 );
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function sha512Bytes( bytes ) {
|
|
395
|
+
return sha512Core(
|
|
396
|
+
bytes,
|
|
397
|
+
[
|
|
398
|
+
0x6a09e667f3bcc908n, 0xbb67ae8584caa73bn, 0x3c6ef372fe94f82bn, 0xa54ff53a5f1d36f1n,
|
|
399
|
+
0x510e527fade682d1n, 0x9b05688c2b3e6c1fn, 0x1f83d9abfb41bd6bn, 0x5be0cd19137e2179n,
|
|
400
|
+
],
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const ALGORITHMS = {
|
|
405
|
+
md5: { blockSize: 64, digest: md5Bytes },
|
|
406
|
+
sha1: { blockSize: 64, digest: sha1Bytes },
|
|
407
|
+
sha224: { blockSize: 64, digest: sha224Bytes },
|
|
408
|
+
sha256: { blockSize: 64, digest: sha256Bytes },
|
|
409
|
+
sha384: { blockSize: 128, digest: sha384Bytes },
|
|
410
|
+
sha512: { blockSize: 128, digest: sha512Bytes },
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
function digestBytes( algorithm, value, fnName ) {
|
|
414
|
+
assertBinary( value, fnName );
|
|
415
|
+
return ALGORITHMS[algorithm].digest( value.bytes );
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function hmacBytes( algorithm, value, key, fnName ) {
|
|
419
|
+
assertBinary( value, fnName );
|
|
420
|
+
assertBinary( key, fnName, 'BinaryString key' );
|
|
421
|
+
const spec = ALGORITHMS[algorithm];
|
|
422
|
+
let keyBytes = key.bytes;
|
|
423
|
+
if ( keyBytes.length > spec.blockSize ) {
|
|
424
|
+
keyBytes = spec.digest( keyBytes );
|
|
425
|
+
}
|
|
426
|
+
const paddedKey = new Uint8Array( spec.blockSize );
|
|
427
|
+
paddedKey.set( keyBytes );
|
|
428
|
+
const innerKey = new Uint8Array( spec.blockSize );
|
|
429
|
+
const outerKey = new Uint8Array( spec.blockSize );
|
|
430
|
+
for ( let i = 0; i < spec.blockSize; i++ ) {
|
|
431
|
+
innerKey[i] = paddedKey[i] ^ 0x36;
|
|
432
|
+
outerKey[i] = paddedKey[i] ^ 0x5c;
|
|
433
|
+
}
|
|
434
|
+
return spec.digest( concatBytes( outerKey, spec.digest( concatBytes( innerKey, value.bytes ) ) ) );
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
module.exports = {
|
|
438
|
+
digestBytes,
|
|
439
|
+
hmacBytes,
|
|
440
|
+
toBinary,
|
|
441
|
+
bytesToHex,
|
|
442
|
+
bytesToBase64,
|
|
443
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
bytesToBase64,
|
|
5
|
+
bytesToHex,
|
|
6
|
+
digestBytes,
|
|
7
|
+
toBinary,
|
|
8
|
+
} = require( './_hash' );
|
|
9
|
+
|
|
10
|
+
function md5( value ) {
|
|
11
|
+
return toBinary( digestBytes( 'md5', value, 'md5' ) );
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function md5_hex( value ) {
|
|
15
|
+
return bytesToHex( digestBytes( 'md5', value, 'md5_hex' ) );
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function md5_b64( value ) {
|
|
19
|
+
return bytesToBase64( digestBytes( 'md5', value, 'md5_b64' ) );
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = {
|
|
23
|
+
md5,
|
|
24
|
+
md5_hex,
|
|
25
|
+
md5_b64,
|
|
26
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
bytesToBase64,
|
|
5
|
+
bytesToHex,
|
|
6
|
+
digestBytes,
|
|
7
|
+
hmacBytes,
|
|
8
|
+
toBinary,
|
|
9
|
+
} = require( './_hash' );
|
|
10
|
+
|
|
11
|
+
function digestFn( algorithm, fnName ) {
|
|
12
|
+
return ( value ) => {
|
|
13
|
+
return toBinary( digestBytes( algorithm, value, fnName ) );
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function digestHexFn( algorithm, fnName ) {
|
|
18
|
+
return ( value ) => {
|
|
19
|
+
return bytesToHex( digestBytes( algorithm, value, fnName ) );
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function digestB64Fn( algorithm, fnName ) {
|
|
24
|
+
return ( value ) => {
|
|
25
|
+
return bytesToBase64( digestBytes( algorithm, value, fnName ) );
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function hmacFn( algorithm, fnName ) {
|
|
30
|
+
return ( value, key ) => {
|
|
31
|
+
return toBinary( hmacBytes( algorithm, value, key, fnName ) );
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function hmacHexFn( algorithm, fnName ) {
|
|
36
|
+
return ( value, key ) => {
|
|
37
|
+
return bytesToHex( hmacBytes( algorithm, value, key, fnName ) );
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function hmacB64Fn( algorithm, fnName ) {
|
|
42
|
+
return ( value, key ) => {
|
|
43
|
+
return bytesToBase64( hmacBytes( algorithm, value, key, fnName ) );
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = {
|
|
48
|
+
sha1: digestFn( 'sha1', 'sha1' ),
|
|
49
|
+
sha224: digestFn( 'sha224', 'sha224' ),
|
|
50
|
+
sha256: digestFn( 'sha256', 'sha256' ),
|
|
51
|
+
sha384: digestFn( 'sha384', 'sha384' ),
|
|
52
|
+
sha512: digestFn( 'sha512', 'sha512' ),
|
|
53
|
+
sha1_hex: digestHexFn( 'sha1', 'sha1_hex' ),
|
|
54
|
+
sha224_hex: digestHexFn( 'sha224', 'sha224_hex' ),
|
|
55
|
+
sha256_hex: digestHexFn( 'sha256', 'sha256_hex' ),
|
|
56
|
+
sha384_hex: digestHexFn( 'sha384', 'sha384_hex' ),
|
|
57
|
+
sha512_hex: digestHexFn( 'sha512', 'sha512_hex' ),
|
|
58
|
+
sha1_b64: digestB64Fn( 'sha1', 'sha1_b64' ),
|
|
59
|
+
sha256_b64: digestB64Fn( 'sha256', 'sha256_b64' ),
|
|
60
|
+
sha512_b64: digestB64Fn( 'sha512', 'sha512_b64' ),
|
|
61
|
+
hmac_sha1: hmacFn( 'sha1', 'hmac_sha1' ),
|
|
62
|
+
hmac_sha224: hmacFn( 'sha224', 'hmac_sha224' ),
|
|
63
|
+
hmac_sha256: hmacFn( 'sha256', 'hmac_sha256' ),
|
|
64
|
+
hmac_sha384: hmacFn( 'sha384', 'hmac_sha384' ),
|
|
65
|
+
hmac_sha512: hmacFn( 'sha512', 'hmac_sha512' ),
|
|
66
|
+
hmac_sha1_hex: hmacHexFn( 'sha1', 'hmac_sha1_hex' ),
|
|
67
|
+
hmac_sha224_hex: hmacHexFn( 'sha224', 'hmac_sha224_hex' ),
|
|
68
|
+
hmac_sha256_hex: hmacHexFn( 'sha256', 'hmac_sha256_hex' ),
|
|
69
|
+
hmac_sha384_hex: hmacHexFn( 'sha384', 'hmac_sha384_hex' ),
|
|
70
|
+
hmac_sha512_hex: hmacHexFn( 'sha512', 'hmac_sha512_hex' ),
|
|
71
|
+
hmac_sha256_b64: hmacB64Fn( 'sha256', 'hmac_sha256_b64' ),
|
|
72
|
+
};
|