@alikhalilll/nuxt-crypto 1.2.0 → 1.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.
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/base64.ts","../src/core/subtle.ts"],"sourcesContent":["import type { Bytes } from './types';\n\n/** Encode bytes to standard base64. Works in browser and Node. */\nexport function toBase64(data: Bytes): string {\n if (typeof btoa === 'function') {\n let binary = '';\n const CHUNK = 0x8000;\n for (let i = 0; i < data.length; i += CHUNK) {\n binary += String.fromCharCode(...data.subarray(i, i + CHUNK));\n }\n return btoa(binary);\n }\n // Node fallback — Buffer is globally available in Node.\n return Buffer.from(data).toString('base64');\n}\n\n/** Decode a base64 string back to bytes. Works in browser and Node. */\nexport function fromBase64(input: string): Bytes {\n if (typeof atob === 'function') {\n const binary = atob(input);\n const out = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);\n return out;\n }\n const buf = Buffer.from(input, 'base64');\n const out = new Uint8Array(buf.length);\n out.set(buf);\n return out;\n}\n","/**\n * Resolve a SubtleCrypto implementation across environments:\n *\n * - Browser / Deno / Bun / Node 20+: `globalThis.crypto.subtle` is available.\n * - Older Node: fall back to `node:crypto`'s `webcrypto`.\n */\nexport async function getSubtle(): Promise<SubtleCrypto> {\n if (globalThis.crypto?.subtle) return globalThis.crypto.subtle;\n const { webcrypto } = await import('node:crypto');\n return webcrypto.subtle as unknown as SubtleCrypto;\n}\n\n/** Fill the given buffer with cryptographically strong random bytes. */\nexport function getRandomBytes(length: number): Uint8Array<ArrayBuffer> {\n const out = new Uint8Array(length);\n const c = globalThis.crypto;\n if (c?.getRandomValues) {\n c.getRandomValues(out);\n return out;\n }\n throw new Error(\n '[nuxt-crypto] globalThis.crypto.getRandomValues is not available. Upgrade your runtime.'\n );\n}\n"],"mappings":";AAGO,SAAS,SAAS,MAAqB;AAC5C,MAAI,OAAO,SAAS,YAAY;AAC9B,QAAI,SAAS;AACb,UAAM,QAAQ;AACd,aAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,OAAO;AAC3C,gBAAU,OAAO,aAAa,GAAG,KAAK,SAAS,GAAG,IAAI,KAAK,CAAC;AAAA,IAC9D;AACA,WAAO,KAAK,MAAM;AAAA,EACpB;AAEA,SAAO,OAAO,KAAK,IAAI,EAAE,SAAS,QAAQ;AAC5C;AAGO,SAAS,WAAW,OAAsB;AAC/C,MAAI,OAAO,SAAS,YAAY;AAC9B,UAAM,SAAS,KAAK,KAAK;AACzB,UAAMA,OAAM,IAAI,WAAW,OAAO,MAAM;AACxC,aAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,IAAK,CAAAA,KAAI,CAAC,IAAI,OAAO,WAAW,CAAC;AACpE,WAAOA;AAAA,EACT;AACA,QAAM,MAAM,OAAO,KAAK,OAAO,QAAQ;AACvC,QAAM,MAAM,IAAI,WAAW,IAAI,MAAM;AACrC,MAAI,IAAI,GAAG;AACX,SAAO;AACT;;;ACtBA,eAAsB,YAAmC;AACvD,MAAI,WAAW,QAAQ,OAAQ,QAAO,WAAW,OAAO;AACxD,QAAM,EAAE,UAAU,IAAI,MAAM,OAAO,QAAa;AAChD,SAAO,UAAU;AACnB;AAGO,SAAS,eAAe,QAAyC;AACtE,QAAM,MAAM,IAAI,WAAW,MAAM;AACjC,QAAM,IAAI,WAAW;AACrB,MAAI,GAAG,iBAAiB;AACtB,MAAE,gBAAgB,GAAG;AACrB,WAAO;AAAA,EACT;AACA,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;","names":["out"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,108 +0,0 @@
1
- /** Shorthand for the `Uint8Array` we use everywhere in the crypto package. */
2
- type Bytes = Uint8Array<ArrayBuffer>;
3
- /**
4
- * Options accepted by `encrypt` / `decrypt`. Both calls must receive the
5
- * same `fingerprint` value to round-trip; passing different fingerprints
6
- * produces undecryptable ciphertext.
7
- */
8
- interface CryptoOperationOptions {
9
- /**
10
- * Optional extra key material mixed into the PBKDF2 input. Typically a
11
- * device fingerprint produced server-side from an HttpOnly device-ID
12
- * cookie via `getClientFingerprint()` from `@alikhalilll/nuxt-crypto/server`.
13
- *
14
- * ⚠️ Binding ciphertext to a fingerprint means the payload becomes
15
- * undecryptable the moment the device cookie is cleared. Use only for
16
- * ephemeral tokens (short-lived session cookies, one-time links), never
17
- * for long-lived user data.
18
- */
19
- fingerprint?: string;
20
- }
21
- /**
22
- * A CryptoService encrypts plaintext to a serialisable string payload and
23
- * decrypts it back. Every operation is async because it hits the Web Crypto
24
- * subtle API.
25
- */
26
- interface CryptoService {
27
- /** Encrypt a UTF-8 string to a versioned, base64-encoded payload. */
28
- encrypt(plainText: string, options?: CryptoOperationOptions): Promise<string>;
29
- /** Decrypt a payload produced by `encrypt`. Pass the same `fingerprint` used at encrypt time, if any. */
30
- decrypt(payload: string, options?: CryptoOperationOptions): Promise<string>;
31
- /** Clear any cached derived keys. Safe to call at any time. */
32
- clearKeyCache(): void;
33
- }
34
- /** Config for `createCryptoService`. */
35
- interface CryptoServiceConfig {
36
- /** Passphrase used to derive the symmetric key. */
37
- passphrase: string;
38
- /** PBKDF2 iteration count. Default 100_000. */
39
- iterations?: number;
40
- /**
41
- * Algorithm implementation. Defaults to AES-256-GCM with PBKDF2-SHA256 key
42
- * derivation. Replace to plug in a different cipher.
43
- */
44
- algorithm?: CryptoAlgorithm;
45
- /**
46
- * Max number of derived keys held in memory. Derived keys are expensive —
47
- * caching them by salt dramatically speeds up bulk decrypt. Default 64.
48
- * Set to 0 to disable caching.
49
- */
50
- keyCacheSize?: number;
51
- /** Override the SubtleCrypto implementation. Useful for testing. */
52
- subtle?: SubtleCrypto;
53
- }
54
- /**
55
- * A pluggable symmetric-encryption algorithm. Each implementation owns the
56
- * payload format (via its version tag), key derivation, and the actual
57
- * encrypt/decrypt work.
58
- */
59
- interface CryptoAlgorithm {
60
- /** Short identifier stored as the first segment of every payload (e.g. `'v1'`). */
61
- readonly version: string;
62
- /**
63
- * Derive a CryptoKey from a passphrase + salt pair. Expected to be idempotent.
64
- * When `fingerprint` is provided it is mixed into the key-derivation input
65
- * (alongside the passphrase) so ciphertext becomes bound to that fingerprint.
66
- */
67
- deriveKey(args: {
68
- subtle: SubtleCrypto;
69
- passphrase: string;
70
- fingerprint?: string;
71
- salt: Bytes;
72
- iterations: number;
73
- }): Promise<CryptoKey>;
74
- /** Encrypt plaintext bytes using a pre-derived key. Returns cipher bytes + any per-call params (e.g. IV). */
75
- encrypt(args: {
76
- subtle: SubtleCrypto;
77
- key: CryptoKey;
78
- plainText: Bytes;
79
- }): Promise<{
80
- cipher: Bytes;
81
- iv: Bytes;
82
- }>;
83
- /** Decrypt cipher bytes using a pre-derived key and the IV produced during encrypt. */
84
- decrypt(args: {
85
- subtle: SubtleCrypto;
86
- key: CryptoKey;
87
- cipher: Bytes;
88
- iv: Bytes;
89
- }): Promise<Bytes>;
90
- }
91
-
92
- /**
93
- * Payload serialisation format (v1): `${version}.${saltB64}.${ivB64}.${cipherB64}`.
94
- *
95
- * Each part is URL-safe base64, all joined with `.`. The version tag is
96
- * always the first segment so consumers can route to the correct algorithm
97
- * during decrypt.
98
- */
99
- interface ParsedPayload {
100
- version: string;
101
- salt: Bytes;
102
- iv: Bytes;
103
- cipher: Bytes;
104
- }
105
- declare function encodePayload(parts: ParsedPayload): string;
106
- declare function parsePayload(payload: string): ParsedPayload;
107
-
108
- export { type Bytes as B, type CryptoAlgorithm as C, type ParsedPayload as P, type CryptoOperationOptions as a, type CryptoService as b, type CryptoServiceConfig as c, encodePayload as e, parsePayload as p };
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}