@cogitator-ai/wasm-tools 0.2.7 → 0.3.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/README.md +176 -45
- package/dist/index.d.ts +178 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +237 -19
- package/dist/index.js.map +1 -1
- package/dist/plugins/base64.d.ts +12 -0
- package/dist/plugins/base64.d.ts.map +1 -0
- package/dist/plugins/base64.js +96 -0
- package/dist/plugins/base64.js.map +1 -0
- package/dist/plugins/hash.d.ts +12 -0
- package/dist/plugins/hash.d.ts.map +1 -0
- package/dist/plugins/hash.js +296 -0
- package/dist/plugins/hash.js.map +1 -0
- package/dist/temp/base64.js +103 -0
- package/dist/temp/hash.js +407 -0
- package/dist/wasm/base64.wasm +0 -0
- package/dist/wasm/calc.wasm +0 -0
- package/dist/wasm/hash.wasm +0 -0
- package/dist/wasm/json.wasm +0 -0
- package/package.json +3 -2
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/plugins/hash.ts
|
|
21
|
+
var hash_exports = {};
|
|
22
|
+
__export(hash_exports, {
|
|
23
|
+
hash: () => hash
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(hash_exports);
|
|
26
|
+
function rotateLeft(n, b) {
|
|
27
|
+
return (n << b | n >>> 32 - b) >>> 0;
|
|
28
|
+
}
|
|
29
|
+
function toHex(arr) {
|
|
30
|
+
return arr.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
31
|
+
}
|
|
32
|
+
function stringToBytes(str) {
|
|
33
|
+
const bytes = [];
|
|
34
|
+
for (let i = 0; i < str.length; i++) {
|
|
35
|
+
bytes.push(str.charCodeAt(i) & 255);
|
|
36
|
+
}
|
|
37
|
+
return bytes;
|
|
38
|
+
}
|
|
39
|
+
function md5(message) {
|
|
40
|
+
const bytes = stringToBytes(message);
|
|
41
|
+
const originalLength = bytes.length;
|
|
42
|
+
const bitLength = originalLength * 8;
|
|
43
|
+
bytes.push(128);
|
|
44
|
+
while (bytes.length % 64 !== 56) {
|
|
45
|
+
bytes.push(0);
|
|
46
|
+
}
|
|
47
|
+
for (let i = 0; i < 8; i++) {
|
|
48
|
+
bytes.push(bitLength >>> i * 8 & 255);
|
|
49
|
+
}
|
|
50
|
+
const s = [
|
|
51
|
+
7,
|
|
52
|
+
12,
|
|
53
|
+
17,
|
|
54
|
+
22,
|
|
55
|
+
7,
|
|
56
|
+
12,
|
|
57
|
+
17,
|
|
58
|
+
22,
|
|
59
|
+
7,
|
|
60
|
+
12,
|
|
61
|
+
17,
|
|
62
|
+
22,
|
|
63
|
+
7,
|
|
64
|
+
12,
|
|
65
|
+
17,
|
|
66
|
+
22,
|
|
67
|
+
5,
|
|
68
|
+
9,
|
|
69
|
+
14,
|
|
70
|
+
20,
|
|
71
|
+
5,
|
|
72
|
+
9,
|
|
73
|
+
14,
|
|
74
|
+
20,
|
|
75
|
+
5,
|
|
76
|
+
9,
|
|
77
|
+
14,
|
|
78
|
+
20,
|
|
79
|
+
5,
|
|
80
|
+
9,
|
|
81
|
+
14,
|
|
82
|
+
20,
|
|
83
|
+
4,
|
|
84
|
+
11,
|
|
85
|
+
16,
|
|
86
|
+
23,
|
|
87
|
+
4,
|
|
88
|
+
11,
|
|
89
|
+
16,
|
|
90
|
+
23,
|
|
91
|
+
4,
|
|
92
|
+
11,
|
|
93
|
+
16,
|
|
94
|
+
23,
|
|
95
|
+
4,
|
|
96
|
+
11,
|
|
97
|
+
16,
|
|
98
|
+
23,
|
|
99
|
+
6,
|
|
100
|
+
10,
|
|
101
|
+
15,
|
|
102
|
+
21,
|
|
103
|
+
6,
|
|
104
|
+
10,
|
|
105
|
+
15,
|
|
106
|
+
21,
|
|
107
|
+
6,
|
|
108
|
+
10,
|
|
109
|
+
15,
|
|
110
|
+
21,
|
|
111
|
+
6,
|
|
112
|
+
10,
|
|
113
|
+
15,
|
|
114
|
+
21
|
|
115
|
+
];
|
|
116
|
+
const K = new Array(64);
|
|
117
|
+
for (let i = 0; i < 64; i++) {
|
|
118
|
+
K[i] = Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296) >>> 0;
|
|
119
|
+
}
|
|
120
|
+
let a0 = 1732584193;
|
|
121
|
+
let b0 = 4023233417;
|
|
122
|
+
let c0 = 2562383102;
|
|
123
|
+
let d0 = 271733878;
|
|
124
|
+
for (let chunkStart = 0; chunkStart < bytes.length; chunkStart += 64) {
|
|
125
|
+
const M = new Array(16);
|
|
126
|
+
for (let j = 0; j < 16; j++) {
|
|
127
|
+
const offset = chunkStart + j * 4;
|
|
128
|
+
M[j] = bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] << 24;
|
|
129
|
+
M[j] = M[j] >>> 0;
|
|
130
|
+
}
|
|
131
|
+
let A = a0;
|
|
132
|
+
let B = b0;
|
|
133
|
+
let C = c0;
|
|
134
|
+
let D = d0;
|
|
135
|
+
for (let i = 0; i < 64; i++) {
|
|
136
|
+
let F, g;
|
|
137
|
+
if (i < 16) {
|
|
138
|
+
F = B & C | ~B & D;
|
|
139
|
+
g = i;
|
|
140
|
+
} else if (i < 32) {
|
|
141
|
+
F = D & B | ~D & C;
|
|
142
|
+
g = (5 * i + 1) % 16;
|
|
143
|
+
} else if (i < 48) {
|
|
144
|
+
F = B ^ C ^ D;
|
|
145
|
+
g = (3 * i + 5) % 16;
|
|
146
|
+
} else {
|
|
147
|
+
F = C ^ (B | ~D);
|
|
148
|
+
g = 7 * i % 16;
|
|
149
|
+
}
|
|
150
|
+
F = F + A + K[i] + M[g] >>> 0;
|
|
151
|
+
A = D;
|
|
152
|
+
D = C;
|
|
153
|
+
C = B;
|
|
154
|
+
B = B + rotateLeft(F, s[i]) >>> 0;
|
|
155
|
+
}
|
|
156
|
+
a0 = a0 + A >>> 0;
|
|
157
|
+
b0 = b0 + B >>> 0;
|
|
158
|
+
c0 = c0 + C >>> 0;
|
|
159
|
+
d0 = d0 + D >>> 0;
|
|
160
|
+
}
|
|
161
|
+
const result = [];
|
|
162
|
+
[a0, b0, c0, d0].forEach((val) => {
|
|
163
|
+
for (let i = 0; i < 4; i++) {
|
|
164
|
+
result.push(val >>> i * 8 & 255);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
return toHex(result);
|
|
168
|
+
}
|
|
169
|
+
function sha1(message) {
|
|
170
|
+
const bytes = stringToBytes(message);
|
|
171
|
+
const originalLength = bytes.length;
|
|
172
|
+
const bitLength = BigInt(originalLength * 8);
|
|
173
|
+
bytes.push(128);
|
|
174
|
+
while (bytes.length % 64 !== 56) {
|
|
175
|
+
bytes.push(0);
|
|
176
|
+
}
|
|
177
|
+
for (let i = 7; i >= 0; i--) {
|
|
178
|
+
bytes.push(Number(bitLength >> BigInt(i * 8) & BigInt(255)));
|
|
179
|
+
}
|
|
180
|
+
let h0 = 1732584193;
|
|
181
|
+
let h1 = 4023233417;
|
|
182
|
+
let h2 = 2562383102;
|
|
183
|
+
let h3 = 271733878;
|
|
184
|
+
let h4 = 3285377520;
|
|
185
|
+
for (let chunkStart = 0; chunkStart < bytes.length; chunkStart += 64) {
|
|
186
|
+
const w = new Array(80);
|
|
187
|
+
for (let i = 0; i < 16; i++) {
|
|
188
|
+
const offset = chunkStart + i * 4;
|
|
189
|
+
w[i] = bytes[offset] << 24 | bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3];
|
|
190
|
+
w[i] = w[i] >>> 0;
|
|
191
|
+
}
|
|
192
|
+
for (let i = 16; i < 80; i++) {
|
|
193
|
+
w[i] = rotateLeft(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
|
|
194
|
+
}
|
|
195
|
+
let a = h0;
|
|
196
|
+
let b = h1;
|
|
197
|
+
let c = h2;
|
|
198
|
+
let d = h3;
|
|
199
|
+
let e = h4;
|
|
200
|
+
for (let i = 0; i < 80; i++) {
|
|
201
|
+
let f, k;
|
|
202
|
+
if (i < 20) {
|
|
203
|
+
f = b & c | ~b & d;
|
|
204
|
+
k = 1518500249;
|
|
205
|
+
} else if (i < 40) {
|
|
206
|
+
f = b ^ c ^ d;
|
|
207
|
+
k = 1859775393;
|
|
208
|
+
} else if (i < 60) {
|
|
209
|
+
f = b & c | b & d | c & d;
|
|
210
|
+
k = 2400959708;
|
|
211
|
+
} else {
|
|
212
|
+
f = b ^ c ^ d;
|
|
213
|
+
k = 3395469782;
|
|
214
|
+
}
|
|
215
|
+
const temp = rotateLeft(a, 5) + f + e + k + w[i] >>> 0;
|
|
216
|
+
e = d;
|
|
217
|
+
d = c;
|
|
218
|
+
c = rotateLeft(b, 30);
|
|
219
|
+
b = a;
|
|
220
|
+
a = temp;
|
|
221
|
+
}
|
|
222
|
+
h0 = h0 + a >>> 0;
|
|
223
|
+
h1 = h1 + b >>> 0;
|
|
224
|
+
h2 = h2 + c >>> 0;
|
|
225
|
+
h3 = h3 + d >>> 0;
|
|
226
|
+
h4 = h4 + e >>> 0;
|
|
227
|
+
}
|
|
228
|
+
const result = [];
|
|
229
|
+
[h0, h1, h2, h3, h4].forEach((val) => {
|
|
230
|
+
for (let i = 3; i >= 0; i--) {
|
|
231
|
+
result.push(val >>> i * 8 & 255);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
return toHex(result);
|
|
235
|
+
}
|
|
236
|
+
function sha256(message) {
|
|
237
|
+
const K = [
|
|
238
|
+
1116352408,
|
|
239
|
+
1899447441,
|
|
240
|
+
3049323471,
|
|
241
|
+
3921009573,
|
|
242
|
+
961987163,
|
|
243
|
+
1508970993,
|
|
244
|
+
2453635748,
|
|
245
|
+
2870763221,
|
|
246
|
+
3624381080,
|
|
247
|
+
310598401,
|
|
248
|
+
607225278,
|
|
249
|
+
1426881987,
|
|
250
|
+
1925078388,
|
|
251
|
+
2162078206,
|
|
252
|
+
2614888103,
|
|
253
|
+
3248222580,
|
|
254
|
+
3835390401,
|
|
255
|
+
4022224774,
|
|
256
|
+
264347078,
|
|
257
|
+
604807628,
|
|
258
|
+
770255983,
|
|
259
|
+
1249150122,
|
|
260
|
+
1555081692,
|
|
261
|
+
1996064986,
|
|
262
|
+
2554220882,
|
|
263
|
+
2821834349,
|
|
264
|
+
2952996808,
|
|
265
|
+
3210313671,
|
|
266
|
+
3336571891,
|
|
267
|
+
3584528711,
|
|
268
|
+
113926993,
|
|
269
|
+
338241895,
|
|
270
|
+
666307205,
|
|
271
|
+
773529912,
|
|
272
|
+
1294757372,
|
|
273
|
+
1396182291,
|
|
274
|
+
1695183700,
|
|
275
|
+
1986661051,
|
|
276
|
+
2177026350,
|
|
277
|
+
2456956037,
|
|
278
|
+
2730485921,
|
|
279
|
+
2820302411,
|
|
280
|
+
3259730800,
|
|
281
|
+
3345764771,
|
|
282
|
+
3516065817,
|
|
283
|
+
3600352804,
|
|
284
|
+
4094571909,
|
|
285
|
+
275423344,
|
|
286
|
+
430227734,
|
|
287
|
+
506948616,
|
|
288
|
+
659060556,
|
|
289
|
+
883997877,
|
|
290
|
+
958139571,
|
|
291
|
+
1322822218,
|
|
292
|
+
1537002063,
|
|
293
|
+
1747873779,
|
|
294
|
+
1955562222,
|
|
295
|
+
2024104815,
|
|
296
|
+
2227730452,
|
|
297
|
+
2361852424,
|
|
298
|
+
2428436474,
|
|
299
|
+
2756734187,
|
|
300
|
+
3204031479,
|
|
301
|
+
3329325298
|
|
302
|
+
];
|
|
303
|
+
const bytes = stringToBytes(message);
|
|
304
|
+
const originalLength = bytes.length;
|
|
305
|
+
const bitLength = BigInt(originalLength * 8);
|
|
306
|
+
bytes.push(128);
|
|
307
|
+
while (bytes.length % 64 !== 56) {
|
|
308
|
+
bytes.push(0);
|
|
309
|
+
}
|
|
310
|
+
for (let i = 7; i >= 0; i--) {
|
|
311
|
+
bytes.push(Number(bitLength >> BigInt(i * 8) & BigInt(255)));
|
|
312
|
+
}
|
|
313
|
+
let h0 = 1779033703;
|
|
314
|
+
let h1 = 3144134277;
|
|
315
|
+
let h2 = 1013904242;
|
|
316
|
+
let h3 = 2773480762;
|
|
317
|
+
let h4 = 1359893119;
|
|
318
|
+
let h5 = 2600822924;
|
|
319
|
+
let h6 = 528734635;
|
|
320
|
+
let h7 = 1541459225;
|
|
321
|
+
const rotr = (x, n) => (x >>> n | x << 32 - n) >>> 0;
|
|
322
|
+
for (let chunkStart = 0; chunkStart < bytes.length; chunkStart += 64) {
|
|
323
|
+
const w = new Array(64);
|
|
324
|
+
for (let i = 0; i < 16; i++) {
|
|
325
|
+
const offset = chunkStart + i * 4;
|
|
326
|
+
w[i] = bytes[offset] << 24 | bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3];
|
|
327
|
+
w[i] = w[i] >>> 0;
|
|
328
|
+
}
|
|
329
|
+
for (let i = 16; i < 64; i++) {
|
|
330
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
|
|
331
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ w[i - 2] >>> 10;
|
|
332
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
|
|
333
|
+
}
|
|
334
|
+
let a = h0;
|
|
335
|
+
let b = h1;
|
|
336
|
+
let c = h2;
|
|
337
|
+
let d = h3;
|
|
338
|
+
let e = h4;
|
|
339
|
+
let f = h5;
|
|
340
|
+
let g = h6;
|
|
341
|
+
let h = h7;
|
|
342
|
+
for (let i = 0; i < 64; i++) {
|
|
343
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
344
|
+
const ch = e & f ^ ~e & g;
|
|
345
|
+
const temp1 = h + S1 + ch + K[i] + w[i] >>> 0;
|
|
346
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
347
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
348
|
+
const temp2 = S0 + maj >>> 0;
|
|
349
|
+
h = g;
|
|
350
|
+
g = f;
|
|
351
|
+
f = e;
|
|
352
|
+
e = d + temp1 >>> 0;
|
|
353
|
+
d = c;
|
|
354
|
+
c = b;
|
|
355
|
+
b = a;
|
|
356
|
+
a = temp1 + temp2 >>> 0;
|
|
357
|
+
}
|
|
358
|
+
h0 = h0 + a >>> 0;
|
|
359
|
+
h1 = h1 + b >>> 0;
|
|
360
|
+
h2 = h2 + c >>> 0;
|
|
361
|
+
h3 = h3 + d >>> 0;
|
|
362
|
+
h4 = h4 + e >>> 0;
|
|
363
|
+
h5 = h5 + f >>> 0;
|
|
364
|
+
h6 = h6 + g >>> 0;
|
|
365
|
+
h7 = h7 + h >>> 0;
|
|
366
|
+
}
|
|
367
|
+
const result = [];
|
|
368
|
+
[h0, h1, h2, h3, h4, h5, h6, h7].forEach((val) => {
|
|
369
|
+
for (let i = 3; i >= 0; i--) {
|
|
370
|
+
result.push(val >>> i * 8 & 255);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
return toHex(result);
|
|
374
|
+
}
|
|
375
|
+
function hash() {
|
|
376
|
+
try {
|
|
377
|
+
const inputStr = Host.inputString();
|
|
378
|
+
const input = JSON.parse(inputStr);
|
|
379
|
+
let hashResult;
|
|
380
|
+
switch (input.algorithm) {
|
|
381
|
+
case "md5":
|
|
382
|
+
hashResult = md5(input.text);
|
|
383
|
+
break;
|
|
384
|
+
case "sha1":
|
|
385
|
+
hashResult = sha1(input.text);
|
|
386
|
+
break;
|
|
387
|
+
case "sha256":
|
|
388
|
+
default:
|
|
389
|
+
hashResult = sha256(input.text);
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
const output = {
|
|
393
|
+
hash: hashResult,
|
|
394
|
+
algorithm: input.algorithm
|
|
395
|
+
};
|
|
396
|
+
Host.outputString(JSON.stringify(output));
|
|
397
|
+
return 0;
|
|
398
|
+
} catch (error) {
|
|
399
|
+
const output = {
|
|
400
|
+
hash: "",
|
|
401
|
+
algorithm: "unknown",
|
|
402
|
+
error: error instanceof Error ? error.message : String(error)
|
|
403
|
+
};
|
|
404
|
+
Host.outputString(JSON.stringify(output));
|
|
405
|
+
return 1;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
Binary file
|
package/dist/wasm/calc.wasm
CHANGED
|
Binary file
|
|
Binary file
|
package/dist/wasm/json.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cogitator-ai/wasm-tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "WASM-based tools for Cogitator agents",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"zod": "^3.22.0",
|
|
20
|
-
"
|
|
20
|
+
"zod-to-json-schema": "^3.22.0",
|
|
21
|
+
"@cogitator-ai/types": "0.10.0"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
24
|
"@extism/js-pdk": "^1.0.0",
|