cas-wasm 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/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # cas-wasm
2
+ A WebAssembly wrapper of the RustCrypto and Dalek-Cryptography algorithms
package/cas_wasm.d.ts ADDED
@@ -0,0 +1,79 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class RSAKeyPair {
5
+ private constructor();
6
+ free(): void;
7
+ [Symbol.dispose](): void;
8
+ readonly privateKey: string;
9
+ readonly publicKey: string;
10
+ }
11
+
12
+ export class RSAWrapper {
13
+ free(): void;
14
+ [Symbol.dispose](): void;
15
+ /**
16
+ * Generates an RSA key pair based of parameter sent in 1024, 2048, and 4096 are supported.
17
+ */
18
+ generateRsaKeys(key_size: number): RSAKeyPair;
19
+ constructor();
20
+ /**
21
+ * Signs a byte array with an RSA private key for verification.
22
+ */
23
+ sign(private_key: string, data: Uint8Array): Uint8Array;
24
+ /**
25
+ * Verifies signed data by the corresponding private key with an RSA public key.
26
+ */
27
+ verify(public_key: string, data: Uint8Array, signature: Uint8Array): boolean;
28
+ }
29
+
30
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
31
+
32
+ export interface InitOutput {
33
+ readonly memory: WebAssembly.Memory;
34
+ readonly __wbg_rsakeypair_free: (a: number, b: number) => void;
35
+ readonly rsakeypair_privateKey: (a: number) => [number, number];
36
+ readonly rsakeypair_publicKey: (a: number) => [number, number];
37
+ readonly __wbg_rsawrapper_free: (a: number, b: number) => void;
38
+ readonly rsawrapper_generateRsaKeys: (a: number, b: number) => number;
39
+ readonly rsawrapper_sign: (a: number, b: number, c: number, d: number, e: number) => [number, number];
40
+ readonly rsawrapper_verify: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => number;
41
+ readonly rsawrapper_new: () => number;
42
+ readonly rust_zstd_wasm_shim_qsort: (a: number, b: number, c: number, d: number) => void;
43
+ readonly rust_zstd_wasm_shim_malloc: (a: number) => number;
44
+ readonly rust_zstd_wasm_shim_memcmp: (a: number, b: number, c: number) => number;
45
+ readonly rust_zstd_wasm_shim_calloc: (a: number, b: number) => number;
46
+ readonly rust_zstd_wasm_shim_free: (a: number) => void;
47
+ readonly rust_zstd_wasm_shim_memcpy: (a: number, b: number, c: number) => number;
48
+ readonly rust_zstd_wasm_shim_memmove: (a: number, b: number, c: number) => number;
49
+ readonly rust_zstd_wasm_shim_memset: (a: number, b: number, c: number) => number;
50
+ readonly __wbindgen_exn_store: (a: number) => void;
51
+ readonly __externref_table_alloc: () => number;
52
+ readonly __wbindgen_externrefs: WebAssembly.Table;
53
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
54
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
55
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
56
+ readonly __wbindgen_start: () => void;
57
+ }
58
+
59
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
60
+
61
+ /**
62
+ * Instantiates the given `module`, which can either be bytes or
63
+ * a precompiled `WebAssembly.Module`.
64
+ *
65
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
66
+ *
67
+ * @returns {InitOutput}
68
+ */
69
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
70
+
71
+ /**
72
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
73
+ * for everything else, calls `WebAssembly.instantiate` directly.
74
+ *
75
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
76
+ *
77
+ * @returns {Promise<InitOutput>}
78
+ */
79
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
package/cas_wasm.js ADDED
@@ -0,0 +1,437 @@
1
+ /* @ts-self-types="./cas_wasm.d.ts" */
2
+
3
+ export class RSAKeyPair {
4
+ static __wrap(ptr) {
5
+ ptr = ptr >>> 0;
6
+ const obj = Object.create(RSAKeyPair.prototype);
7
+ obj.__wbg_ptr = ptr;
8
+ RSAKeyPairFinalization.register(obj, obj.__wbg_ptr, obj);
9
+ return obj;
10
+ }
11
+ __destroy_into_raw() {
12
+ const ptr = this.__wbg_ptr;
13
+ this.__wbg_ptr = 0;
14
+ RSAKeyPairFinalization.unregister(this);
15
+ return ptr;
16
+ }
17
+ free() {
18
+ const ptr = this.__destroy_into_raw();
19
+ wasm.__wbg_rsakeypair_free(ptr, 0);
20
+ }
21
+ /**
22
+ * @returns {string}
23
+ */
24
+ get privateKey() {
25
+ let deferred1_0;
26
+ let deferred1_1;
27
+ try {
28
+ const ret = wasm.rsakeypair_privateKey(this.__wbg_ptr);
29
+ deferred1_0 = ret[0];
30
+ deferred1_1 = ret[1];
31
+ return getStringFromWasm0(ret[0], ret[1]);
32
+ } finally {
33
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
34
+ }
35
+ }
36
+ /**
37
+ * @returns {string}
38
+ */
39
+ get publicKey() {
40
+ let deferred1_0;
41
+ let deferred1_1;
42
+ try {
43
+ const ret = wasm.rsakeypair_publicKey(this.__wbg_ptr);
44
+ deferred1_0 = ret[0];
45
+ deferred1_1 = ret[1];
46
+ return getStringFromWasm0(ret[0], ret[1]);
47
+ } finally {
48
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
49
+ }
50
+ }
51
+ }
52
+ if (Symbol.dispose) RSAKeyPair.prototype[Symbol.dispose] = RSAKeyPair.prototype.free;
53
+
54
+ export class RSAWrapper {
55
+ __destroy_into_raw() {
56
+ const ptr = this.__wbg_ptr;
57
+ this.__wbg_ptr = 0;
58
+ RSAWrapperFinalization.unregister(this);
59
+ return ptr;
60
+ }
61
+ free() {
62
+ const ptr = this.__destroy_into_raw();
63
+ wasm.__wbg_rsawrapper_free(ptr, 0);
64
+ }
65
+ /**
66
+ * Generates an RSA key pair based of parameter sent in 1024, 2048, and 4096 are supported.
67
+ * @param {number} key_size
68
+ * @returns {RSAKeyPair}
69
+ */
70
+ generateRsaKeys(key_size) {
71
+ const ret = wasm.rsawrapper_generateRsaKeys(this.__wbg_ptr, key_size);
72
+ return RSAKeyPair.__wrap(ret);
73
+ }
74
+ constructor() {
75
+ const ret = wasm.rsawrapper_new();
76
+ this.__wbg_ptr = ret >>> 0;
77
+ RSAWrapperFinalization.register(this, this.__wbg_ptr, this);
78
+ return this;
79
+ }
80
+ /**
81
+ * Signs a byte array with an RSA private key for verification.
82
+ * @param {string} private_key
83
+ * @param {Uint8Array} data
84
+ * @returns {Uint8Array}
85
+ */
86
+ sign(private_key, data) {
87
+ const ptr0 = passStringToWasm0(private_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
88
+ const len0 = WASM_VECTOR_LEN;
89
+ const ptr1 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
90
+ const len1 = WASM_VECTOR_LEN;
91
+ const ret = wasm.rsawrapper_sign(this.__wbg_ptr, ptr0, len0, ptr1, len1);
92
+ var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
93
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
94
+ return v3;
95
+ }
96
+ /**
97
+ * Verifies signed data by the corresponding private key with an RSA public key.
98
+ * @param {string} public_key
99
+ * @param {Uint8Array} data
100
+ * @param {Uint8Array} signature
101
+ * @returns {boolean}
102
+ */
103
+ verify(public_key, data, signature) {
104
+ const ptr0 = passStringToWasm0(public_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
105
+ const len0 = WASM_VECTOR_LEN;
106
+ const ptr1 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
107
+ const len1 = WASM_VECTOR_LEN;
108
+ const ptr2 = passArray8ToWasm0(signature, wasm.__wbindgen_malloc);
109
+ const len2 = WASM_VECTOR_LEN;
110
+ const ret = wasm.rsawrapper_verify(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
111
+ return ret !== 0;
112
+ }
113
+ }
114
+ if (Symbol.dispose) RSAWrapper.prototype[Symbol.dispose] = RSAWrapper.prototype.free;
115
+
116
+ function __wbg_get_imports() {
117
+ const import0 = {
118
+ __proto__: null,
119
+ __wbg___wbindgen_is_function_3c846841762788c1: function(arg0) {
120
+ const ret = typeof(arg0) === 'function';
121
+ return ret;
122
+ },
123
+ __wbg___wbindgen_is_object_781bc9f159099513: function(arg0) {
124
+ const val = arg0;
125
+ const ret = typeof(val) === 'object' && val !== null;
126
+ return ret;
127
+ },
128
+ __wbg___wbindgen_is_string_7ef6b97b02428fae: function(arg0) {
129
+ const ret = typeof(arg0) === 'string';
130
+ return ret;
131
+ },
132
+ __wbg___wbindgen_is_undefined_52709e72fb9f179c: function(arg0) {
133
+ const ret = arg0 === undefined;
134
+ return ret;
135
+ },
136
+ __wbg___wbindgen_throw_6ddd609b62940d55: function(arg0, arg1) {
137
+ throw new Error(getStringFromWasm0(arg0, arg1));
138
+ },
139
+ __wbg_call_2d781c1f4d5c0ef8: function() { return handleError(function (arg0, arg1, arg2) {
140
+ const ret = arg0.call(arg1, arg2);
141
+ return ret;
142
+ }, arguments); },
143
+ __wbg_crypto_38df2bab126b63dc: function(arg0) {
144
+ const ret = arg0.crypto;
145
+ return ret;
146
+ },
147
+ __wbg_getRandomValues_c44a50d8cfdaebeb: function() { return handleError(function (arg0, arg1) {
148
+ arg0.getRandomValues(arg1);
149
+ }, arguments); },
150
+ __wbg_length_ea16607d7b61445b: function(arg0) {
151
+ const ret = arg0.length;
152
+ return ret;
153
+ },
154
+ __wbg_msCrypto_bd5a034af96bcba6: function(arg0) {
155
+ const ret = arg0.msCrypto;
156
+ return ret;
157
+ },
158
+ __wbg_new_with_length_825018a1616e9e55: function(arg0) {
159
+ const ret = new Uint8Array(arg0 >>> 0);
160
+ return ret;
161
+ },
162
+ __wbg_node_84ea875411254db1: function(arg0) {
163
+ const ret = arg0.node;
164
+ return ret;
165
+ },
166
+ __wbg_process_44c7a14e11e9f69e: function(arg0) {
167
+ const ret = arg0.process;
168
+ return ret;
169
+ },
170
+ __wbg_prototypesetcall_d62e5099504357e6: function(arg0, arg1, arg2) {
171
+ Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
172
+ },
173
+ __wbg_randomFillSync_6c25eac9869eb53c: function() { return handleError(function (arg0, arg1) {
174
+ arg0.randomFillSync(arg1);
175
+ }, arguments); },
176
+ __wbg_require_b4edbdcf3e2a1ef0: function() { return handleError(function () {
177
+ const ret = module.require;
178
+ return ret;
179
+ }, arguments); },
180
+ __wbg_static_accessor_GLOBAL_8adb955bd33fac2f: function() {
181
+ const ret = typeof global === 'undefined' ? null : global;
182
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
183
+ },
184
+ __wbg_static_accessor_GLOBAL_THIS_ad356e0db91c7913: function() {
185
+ const ret = typeof globalThis === 'undefined' ? null : globalThis;
186
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
187
+ },
188
+ __wbg_static_accessor_SELF_f207c857566db248: function() {
189
+ const ret = typeof self === 'undefined' ? null : self;
190
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
191
+ },
192
+ __wbg_static_accessor_WINDOW_bb9f1ba69d61b386: function() {
193
+ const ret = typeof window === 'undefined' ? null : window;
194
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
195
+ },
196
+ __wbg_subarray_a068d24e39478a8a: function(arg0, arg1, arg2) {
197
+ const ret = arg0.subarray(arg1 >>> 0, arg2 >>> 0);
198
+ return ret;
199
+ },
200
+ __wbg_versions_276b2795b1c6a219: function(arg0) {
201
+ const ret = arg0.versions;
202
+ return ret;
203
+ },
204
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
205
+ // Cast intrinsic for `Ref(Slice(U8)) -> NamedExternref("Uint8Array")`.
206
+ const ret = getArrayU8FromWasm0(arg0, arg1);
207
+ return ret;
208
+ },
209
+ __wbindgen_cast_0000000000000002: function(arg0, arg1) {
210
+ // Cast intrinsic for `Ref(String) -> Externref`.
211
+ const ret = getStringFromWasm0(arg0, arg1);
212
+ return ret;
213
+ },
214
+ __wbindgen_init_externref_table: function() {
215
+ const table = wasm.__wbindgen_externrefs;
216
+ const offset = table.grow(4);
217
+ table.set(0, undefined);
218
+ table.set(offset + 0, undefined);
219
+ table.set(offset + 1, null);
220
+ table.set(offset + 2, true);
221
+ table.set(offset + 3, false);
222
+ },
223
+ };
224
+ return {
225
+ __proto__: null,
226
+ "./cas_wasm_bg.js": import0,
227
+ };
228
+ }
229
+
230
+ const RSAKeyPairFinalization = (typeof FinalizationRegistry === 'undefined')
231
+ ? { register: () => {}, unregister: () => {} }
232
+ : new FinalizationRegistry(ptr => wasm.__wbg_rsakeypair_free(ptr >>> 0, 1));
233
+ const RSAWrapperFinalization = (typeof FinalizationRegistry === 'undefined')
234
+ ? { register: () => {}, unregister: () => {} }
235
+ : new FinalizationRegistry(ptr => wasm.__wbg_rsawrapper_free(ptr >>> 0, 1));
236
+
237
+ function addToExternrefTable0(obj) {
238
+ const idx = wasm.__externref_table_alloc();
239
+ wasm.__wbindgen_externrefs.set(idx, obj);
240
+ return idx;
241
+ }
242
+
243
+ function getArrayU8FromWasm0(ptr, len) {
244
+ ptr = ptr >>> 0;
245
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
246
+ }
247
+
248
+ function getStringFromWasm0(ptr, len) {
249
+ ptr = ptr >>> 0;
250
+ return decodeText(ptr, len);
251
+ }
252
+
253
+ let cachedUint8ArrayMemory0 = null;
254
+ function getUint8ArrayMemory0() {
255
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
256
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
257
+ }
258
+ return cachedUint8ArrayMemory0;
259
+ }
260
+
261
+ function handleError(f, args) {
262
+ try {
263
+ return f.apply(this, args);
264
+ } catch (e) {
265
+ const idx = addToExternrefTable0(e);
266
+ wasm.__wbindgen_exn_store(idx);
267
+ }
268
+ }
269
+
270
+ function isLikeNone(x) {
271
+ return x === undefined || x === null;
272
+ }
273
+
274
+ function passArray8ToWasm0(arg, malloc) {
275
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
276
+ getUint8ArrayMemory0().set(arg, ptr / 1);
277
+ WASM_VECTOR_LEN = arg.length;
278
+ return ptr;
279
+ }
280
+
281
+ function passStringToWasm0(arg, malloc, realloc) {
282
+ if (realloc === undefined) {
283
+ const buf = cachedTextEncoder.encode(arg);
284
+ const ptr = malloc(buf.length, 1) >>> 0;
285
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
286
+ WASM_VECTOR_LEN = buf.length;
287
+ return ptr;
288
+ }
289
+
290
+ let len = arg.length;
291
+ let ptr = malloc(len, 1) >>> 0;
292
+
293
+ const mem = getUint8ArrayMemory0();
294
+
295
+ let offset = 0;
296
+
297
+ for (; offset < len; offset++) {
298
+ const code = arg.charCodeAt(offset);
299
+ if (code > 0x7F) break;
300
+ mem[ptr + offset] = code;
301
+ }
302
+ if (offset !== len) {
303
+ if (offset !== 0) {
304
+ arg = arg.slice(offset);
305
+ }
306
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
307
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
308
+ const ret = cachedTextEncoder.encodeInto(arg, view);
309
+
310
+ offset += ret.written;
311
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
312
+ }
313
+
314
+ WASM_VECTOR_LEN = offset;
315
+ return ptr;
316
+ }
317
+
318
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
319
+ cachedTextDecoder.decode();
320
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
321
+ let numBytesDecoded = 0;
322
+ function decodeText(ptr, len) {
323
+ numBytesDecoded += len;
324
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
325
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
326
+ cachedTextDecoder.decode();
327
+ numBytesDecoded = len;
328
+ }
329
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
330
+ }
331
+
332
+ const cachedTextEncoder = new TextEncoder();
333
+
334
+ if (!('encodeInto' in cachedTextEncoder)) {
335
+ cachedTextEncoder.encodeInto = function (arg, view) {
336
+ const buf = cachedTextEncoder.encode(arg);
337
+ view.set(buf);
338
+ return {
339
+ read: arg.length,
340
+ written: buf.length
341
+ };
342
+ };
343
+ }
344
+
345
+ let WASM_VECTOR_LEN = 0;
346
+
347
+ let wasmModule, wasm;
348
+ function __wbg_finalize_init(instance, module) {
349
+ wasm = instance.exports;
350
+ wasmModule = module;
351
+ cachedUint8ArrayMemory0 = null;
352
+ wasm.__wbindgen_start();
353
+ return wasm;
354
+ }
355
+
356
+ async function __wbg_load(module, imports) {
357
+ if (typeof Response === 'function' && module instanceof Response) {
358
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
359
+ try {
360
+ return await WebAssembly.instantiateStreaming(module, imports);
361
+ } catch (e) {
362
+ const validResponse = module.ok && expectedResponseType(module.type);
363
+
364
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
365
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
366
+
367
+ } else { throw e; }
368
+ }
369
+ }
370
+
371
+ const bytes = await module.arrayBuffer();
372
+ return await WebAssembly.instantiate(bytes, imports);
373
+ } else {
374
+ const instance = await WebAssembly.instantiate(module, imports);
375
+
376
+ if (instance instanceof WebAssembly.Instance) {
377
+ return { instance, module };
378
+ } else {
379
+ return instance;
380
+ }
381
+ }
382
+
383
+ function expectedResponseType(type) {
384
+ switch (type) {
385
+ case 'basic': case 'cors': case 'default': return true;
386
+ }
387
+ return false;
388
+ }
389
+ }
390
+
391
+ function initSync(module) {
392
+ if (wasm !== undefined) return wasm;
393
+
394
+
395
+ if (module !== undefined) {
396
+ if (Object.getPrototypeOf(module) === Object.prototype) {
397
+ ({module} = module)
398
+ } else {
399
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
400
+ }
401
+ }
402
+
403
+ const imports = __wbg_get_imports();
404
+ if (!(module instanceof WebAssembly.Module)) {
405
+ module = new WebAssembly.Module(module);
406
+ }
407
+ const instance = new WebAssembly.Instance(module, imports);
408
+ return __wbg_finalize_init(instance, module);
409
+ }
410
+
411
+ async function __wbg_init(module_or_path) {
412
+ if (wasm !== undefined) return wasm;
413
+
414
+
415
+ if (module_or_path !== undefined) {
416
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
417
+ ({module_or_path} = module_or_path)
418
+ } else {
419
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
420
+ }
421
+ }
422
+
423
+ if (module_or_path === undefined) {
424
+ module_or_path = new URL('cas_wasm_bg.wasm', import.meta.url);
425
+ }
426
+ const imports = __wbg_get_imports();
427
+
428
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
429
+ module_or_path = fetch(module_or_path);
430
+ }
431
+
432
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
433
+
434
+ return __wbg_finalize_init(instance, module);
435
+ }
436
+
437
+ export { initSync, __wbg_init as default };
Binary file
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "cas-wasm",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "files": [
6
+ "cas_wasm_bg.wasm",
7
+ "cas_wasm.js",
8
+ "cas_wasm.d.ts"
9
+ ],
10
+ "main": "cas_wasm.js",
11
+ "types": "cas_wasm.d.ts",
12
+ "sideEffects": [
13
+ "./snippets/*"
14
+ ]
15
+ }