@ricsam/quickjs-crypto 0.0.1 → 0.2.14

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.
@@ -0,0 +1,287 @@
1
+ /**
2
+ * QuickJS Global Type Definitions for @ricsam/quickjs-crypto
3
+ *
4
+ * These types define the globals injected by setupCrypto() into a QuickJS context.
5
+ * Use these types to typecheck user code that will run inside QuickJS.
6
+ *
7
+ * @example
8
+ * // Generate random bytes
9
+ * const arr = new Uint8Array(16);
10
+ * crypto.getRandomValues(arr);
11
+ *
12
+ * // Generate UUID
13
+ * const uuid = crypto.randomUUID();
14
+ *
15
+ * // Use SubtleCrypto
16
+ * const key = await crypto.subtle.generateKey(
17
+ * { name: "AES-GCM", length: 256 },
18
+ * true,
19
+ * ["encrypt", "decrypt"]
20
+ * );
21
+ */
22
+
23
+ export {};
24
+
25
+ declare global {
26
+ /**
27
+ * CryptoKey represents a cryptographic key.
28
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey
29
+ */
30
+ interface CryptoKey {
31
+ /**
32
+ * The type of key: "public", "private", or "secret".
33
+ */
34
+ readonly type: "public" | "private" | "secret";
35
+
36
+ /**
37
+ * Whether the key can be exported.
38
+ */
39
+ readonly extractable: boolean;
40
+
41
+ /**
42
+ * The algorithm used by this key.
43
+ */
44
+ readonly algorithm: KeyAlgorithm;
45
+
46
+ /**
47
+ * The usages allowed for this key.
48
+ */
49
+ readonly usages: ReadonlyArray<KeyUsage>;
50
+ }
51
+
52
+ /**
53
+ * CryptoKey constructor (keys cannot be constructed directly).
54
+ */
55
+ const CryptoKey: {
56
+ prototype: CryptoKey;
57
+ };
58
+
59
+ /**
60
+ * SubtleCrypto interface for cryptographic operations.
61
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto
62
+ */
63
+ interface SubtleCrypto {
64
+ /**
65
+ * Generate a digest (hash) of the given data.
66
+ *
67
+ * @param algorithm - Hash algorithm (e.g., "SHA-256", "SHA-384", "SHA-512")
68
+ * @param data - Data to hash
69
+ * @returns Promise resolving to the hash as ArrayBuffer
70
+ */
71
+ digest(
72
+ algorithm: AlgorithmIdentifier,
73
+ data: BufferSource
74
+ ): Promise<ArrayBuffer>;
75
+
76
+ /**
77
+ * Generate a new cryptographic key or key pair.
78
+ *
79
+ * @param algorithm - Key generation algorithm
80
+ * @param extractable - Whether the key can be exported
81
+ * @param keyUsages - Allowed key usages
82
+ * @returns Promise resolving to a CryptoKey or CryptoKeyPair
83
+ */
84
+ generateKey(
85
+ algorithm: RsaHashedKeyGenParams | EcKeyGenParams | AesKeyGenParams | HmacKeyGenParams,
86
+ extractable: boolean,
87
+ keyUsages: KeyUsage[]
88
+ ): Promise<CryptoKey | CryptoKeyPair>;
89
+
90
+ /**
91
+ * Sign data using a private key.
92
+ *
93
+ * @param algorithm - Signing algorithm
94
+ * @param key - Private key to sign with
95
+ * @param data - Data to sign
96
+ * @returns Promise resolving to the signature as ArrayBuffer
97
+ */
98
+ sign(
99
+ algorithm: AlgorithmIdentifier,
100
+ key: CryptoKey,
101
+ data: BufferSource
102
+ ): Promise<ArrayBuffer>;
103
+
104
+ /**
105
+ * Verify a signature.
106
+ *
107
+ * @param algorithm - Signing algorithm
108
+ * @param key - Public key to verify with
109
+ * @param signature - Signature to verify
110
+ * @param data - Data that was signed
111
+ * @returns Promise resolving to true if valid, false otherwise
112
+ */
113
+ verify(
114
+ algorithm: AlgorithmIdentifier,
115
+ key: CryptoKey,
116
+ signature: BufferSource,
117
+ data: BufferSource
118
+ ): Promise<boolean>;
119
+
120
+ /**
121
+ * Encrypt data.
122
+ *
123
+ * @param algorithm - Encryption algorithm
124
+ * @param key - Encryption key
125
+ * @param data - Data to encrypt
126
+ * @returns Promise resolving to encrypted data as ArrayBuffer
127
+ */
128
+ encrypt(
129
+ algorithm: AlgorithmIdentifier,
130
+ key: CryptoKey,
131
+ data: BufferSource
132
+ ): Promise<ArrayBuffer>;
133
+
134
+ /**
135
+ * Decrypt data.
136
+ *
137
+ * @param algorithm - Decryption algorithm
138
+ * @param key - Decryption key
139
+ * @param data - Data to decrypt
140
+ * @returns Promise resolving to decrypted data as ArrayBuffer
141
+ */
142
+ decrypt(
143
+ algorithm: AlgorithmIdentifier,
144
+ key: CryptoKey,
145
+ data: BufferSource
146
+ ): Promise<ArrayBuffer>;
147
+
148
+ /**
149
+ * Import a key from external data.
150
+ *
151
+ * @param format - Key format ("raw", "pkcs8", "spki", "jwk")
152
+ * @param keyData - Key data
153
+ * @param algorithm - Key algorithm
154
+ * @param extractable - Whether the key can be exported
155
+ * @param keyUsages - Allowed key usages
156
+ * @returns Promise resolving to a CryptoKey
157
+ */
158
+ importKey(
159
+ format: "raw" | "pkcs8" | "spki" | "jwk",
160
+ keyData: BufferSource | JsonWebKey,
161
+ algorithm: AlgorithmIdentifier,
162
+ extractable: boolean,
163
+ keyUsages: KeyUsage[]
164
+ ): Promise<CryptoKey>;
165
+
166
+ /**
167
+ * Export a key.
168
+ *
169
+ * @param format - Export format ("raw", "pkcs8", "spki", "jwk")
170
+ * @param key - Key to export
171
+ * @returns Promise resolving to ArrayBuffer or JsonWebKey
172
+ */
173
+ exportKey(
174
+ format: "raw" | "pkcs8" | "spki" | "jwk",
175
+ key: CryptoKey
176
+ ): Promise<ArrayBuffer | JsonWebKey>;
177
+
178
+ /**
179
+ * Derive bits from a key.
180
+ *
181
+ * @param algorithm - Derivation algorithm
182
+ * @param baseKey - Base key for derivation
183
+ * @param length - Number of bits to derive
184
+ * @returns Promise resolving to derived bits as ArrayBuffer
185
+ */
186
+ deriveBits(
187
+ algorithm: AlgorithmIdentifier,
188
+ baseKey: CryptoKey,
189
+ length: number
190
+ ): Promise<ArrayBuffer>;
191
+
192
+ /**
193
+ * Derive a new key from a base key.
194
+ *
195
+ * @param algorithm - Derivation algorithm
196
+ * @param baseKey - Base key for derivation
197
+ * @param derivedKeyType - Type of key to derive
198
+ * @param extractable - Whether the derived key can be exported
199
+ * @param keyUsages - Allowed usages for derived key
200
+ * @returns Promise resolving to a CryptoKey
201
+ */
202
+ deriveKey(
203
+ algorithm: AlgorithmIdentifier,
204
+ baseKey: CryptoKey,
205
+ derivedKeyType: AlgorithmIdentifier,
206
+ extractable: boolean,
207
+ keyUsages: KeyUsage[]
208
+ ): Promise<CryptoKey>;
209
+
210
+ /**
211
+ * Wrap a key for secure export.
212
+ *
213
+ * @param format - Key format
214
+ * @param key - Key to wrap
215
+ * @param wrappingKey - Key to wrap with
216
+ * @param wrapAlgorithm - Wrapping algorithm
217
+ * @returns Promise resolving to wrapped key as ArrayBuffer
218
+ */
219
+ wrapKey(
220
+ format: "raw" | "pkcs8" | "spki" | "jwk",
221
+ key: CryptoKey,
222
+ wrappingKey: CryptoKey,
223
+ wrapAlgorithm: AlgorithmIdentifier
224
+ ): Promise<ArrayBuffer>;
225
+
226
+ /**
227
+ * Unwrap a wrapped key.
228
+ *
229
+ * @param format - Key format
230
+ * @param wrappedKey - Wrapped key data
231
+ * @param unwrappingKey - Key to unwrap with
232
+ * @param unwrapAlgorithm - Unwrapping algorithm
233
+ * @param unwrappedKeyAlgorithm - Algorithm for the unwrapped key
234
+ * @param extractable - Whether the unwrapped key can be exported
235
+ * @param keyUsages - Allowed usages for unwrapped key
236
+ * @returns Promise resolving to a CryptoKey
237
+ */
238
+ unwrapKey(
239
+ format: "raw" | "pkcs8" | "spki" | "jwk",
240
+ wrappedKey: BufferSource,
241
+ unwrappingKey: CryptoKey,
242
+ unwrapAlgorithm: AlgorithmIdentifier,
243
+ unwrappedKeyAlgorithm: AlgorithmIdentifier,
244
+ extractable: boolean,
245
+ keyUsages: KeyUsage[]
246
+ ): Promise<CryptoKey>;
247
+ }
248
+
249
+ /**
250
+ * Crypto interface providing cryptographic functionality.
251
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto
252
+ */
253
+ interface Crypto {
254
+ /**
255
+ * SubtleCrypto interface for cryptographic operations.
256
+ */
257
+ readonly subtle: SubtleCrypto;
258
+
259
+ /**
260
+ * Fill a TypedArray with cryptographically random values.
261
+ *
262
+ * @param array - TypedArray to fill (max 65536 bytes)
263
+ * @returns The same array, filled with random values
264
+ *
265
+ * @example
266
+ * const arr = new Uint8Array(16);
267
+ * crypto.getRandomValues(arr);
268
+ */
269
+ getRandomValues<T extends ArrayBufferView | null>(array: T): T;
270
+
271
+ /**
272
+ * Generate a random UUID v4.
273
+ *
274
+ * @returns A random UUID string
275
+ *
276
+ * @example
277
+ * const uuid = crypto.randomUUID();
278
+ * // "550e8400-e29b-41d4-a716-446655440000"
279
+ */
280
+ randomUUID(): string;
281
+ }
282
+
283
+ /**
284
+ * Crypto object providing cryptographic functionality.
285
+ */
286
+ const crypto: Crypto;
287
+ }
@@ -0,0 +1,14 @@
1
+ import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten";
2
+ /**
3
+ * Create the getRandomValues function for the crypto global
4
+ *
5
+ * getRandomValues fills a TypedArray with cryptographically random values
6
+ * and returns the same array (modified in-place)
7
+ */
8
+ export declare function createGetRandomValuesFunction(context: QuickJSContext): QuickJSHandle;
9
+ /**
10
+ * Create the randomUUID function for the crypto global
11
+ *
12
+ * randomUUID returns a string containing a randomly generated UUID v4
13
+ */
14
+ export declare function createRandomUUIDFunction(context: QuickJSContext): QuickJSHandle;
@@ -0,0 +1,43 @@
1
+ import type { QuickJSContext } from "quickjs-emscripten";
2
+ import type { SetupCryptoOptions, CryptoHandle } from "./types.ts";
3
+ /**
4
+ * Setup crypto globals in a QuickJS context
5
+ *
6
+ * Provides: crypto.subtle (SubtleCrypto), crypto.getRandomValues, crypto.randomUUID, CryptoKey
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const handle = setupCrypto(context);
11
+ *
12
+ * context.evalCode(`
13
+ * // Generate a random UUID
14
+ * const uuid = crypto.randomUUID();
15
+ *
16
+ * // Generate random bytes
17
+ * const arr = new Uint8Array(16);
18
+ * crypto.getRandomValues(arr);
19
+ *
20
+ * // Use SubtleCrypto for encryption
21
+ * const key = await crypto.subtle.generateKey(
22
+ * { name: "AES-GCM", length: 256 },
23
+ * true,
24
+ * ["encrypt", "decrypt"]
25
+ * );
26
+ *
27
+ * const iv = crypto.getRandomValues(new Uint8Array(12));
28
+ * const data = new TextEncoder().encode("secret message");
29
+ * const encrypted = await crypto.subtle.encrypt(
30
+ * { name: "AES-GCM", iv },
31
+ * key,
32
+ * data
33
+ * );
34
+ * `);
35
+ *
36
+ * handle.dispose();
37
+ * ```
38
+ *
39
+ * @param context - QuickJS context
40
+ * @param options - Setup options
41
+ * @returns CryptoHandle for cleanup
42
+ */
43
+ export declare function setupCrypto(context: QuickJSContext, options?: SetupCryptoOptions): CryptoHandle;
@@ -0,0 +1,12 @@
1
+ import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten";
2
+ /**
3
+ * Helper to create a CryptoKey instance in QuickJS from a host CryptoKey
4
+ *
5
+ * This registers the host key in our state management and creates
6
+ * a QuickJS CryptoKey object that references it via __instanceId__
7
+ */
8
+ export declare function createCryptoKeyInstance(context: QuickJSContext, hostKey: globalThis.CryptoKey): QuickJSHandle;
9
+ /**
10
+ * Create the SubtleCrypto object with all methods
11
+ */
12
+ export declare function createSubtleCryptoObject(context: QuickJSContext): QuickJSHandle;
@@ -0,0 +1,44 @@
1
+ import type { StateMap, CoreHandle } from "@ricsam/quickjs-core";
2
+ export type { StateMap, CoreHandle };
3
+ /**
4
+ * Key usage types as defined by Web Crypto API
5
+ */
6
+ export type KeyUsage = "encrypt" | "decrypt" | "sign" | "verify" | "deriveKey" | "deriveBits" | "wrapKey" | "unwrapKey";
7
+ /**
8
+ * Key type
9
+ */
10
+ export type KeyType = "public" | "private" | "secret";
11
+ /**
12
+ * Internal state for CryptoKey instances
13
+ * The actual CryptoKey stays on the host side
14
+ */
15
+ export interface CryptoKeyState {
16
+ /** Reference to host crypto key (actual CryptoKey from host) */
17
+ hostKey: globalThis.CryptoKey;
18
+ /** Key type: public, private, or secret */
19
+ type: KeyType;
20
+ /** Whether the key can be exported */
21
+ extractable: boolean;
22
+ /** Algorithm information */
23
+ algorithm: KeyAlgorithm;
24
+ /** Allowed usages for this key */
25
+ usages: KeyUsage[];
26
+ }
27
+ /**
28
+ * Options for setupCrypto
29
+ */
30
+ export interface SetupCryptoOptions {
31
+ /** Shared state map for instance tracking */
32
+ stateMap?: StateMap;
33
+ /** Core handle from setupCore */
34
+ coreHandle?: CoreHandle;
35
+ }
36
+ /**
37
+ * Handle returned from setupCrypto
38
+ */
39
+ export interface CryptoHandle {
40
+ /** Shared state map */
41
+ readonly stateMap: StateMap;
42
+ /** Dispose and cleanup */
43
+ dispose(): void;
44
+ }
package/package.json CHANGED
@@ -1,10 +1,58 @@
1
1
  {
2
2
  "name": "@ricsam/quickjs-crypto",
3
- "version": "0.0.1",
4
- "description": "OIDC trusted publishing setup package for @ricsam/quickjs-crypto",
3
+ "version": "0.2.14",
4
+ "main": "./dist/cjs/index.cjs",
5
+ "types": "./dist/types/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/types/index.d.ts",
9
+ "require": "./dist/cjs/index.cjs",
10
+ "import": "./dist/mjs/index.mjs"
11
+ },
12
+ "./quickjs": {
13
+ "types": "./dist/types/quickjs.d.ts"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "build": "bun build ./src/index.ts --outdir ./dist --target bun",
18
+ "test": "bun test",
19
+ "typecheck": "tsc --noEmit"
20
+ },
21
+ "dependencies": {
22
+ "@ricsam/quickjs-core": "^0.2.14",
23
+ "quickjs-emscripten": "^0.31.0"
24
+ },
25
+ "peerDependencies": {
26
+ "quickjs-emscripten": "^0.31.0"
27
+ },
28
+ "author": "Richard Samuelsson",
29
+ "license": "MIT",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/ricsam/richie-qjs.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/ricsam/richie-qjs/issues"
36
+ },
37
+ "homepage": "https://github.com/ricsam/richie-qjs#readme",
5
38
  "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
39
+ "quickjs",
40
+ "sandbox",
41
+ "javascript",
42
+ "runtime",
43
+ "fetch",
44
+ "filesystem",
45
+ "streams",
46
+ "wasm",
47
+ "emscripten"
48
+ ],
49
+ "description": "Web Crypto API implementation for QuickJS runtime",
50
+ "module": "./dist/mjs/index.mjs",
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "files": [
55
+ "dist",
56
+ "README.md"
9
57
  ]
10
- }
58
+ }