@optimystic/quereus-plugin-crypto 0.3.0 → 0.3.2

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 CHANGED
@@ -1,279 +1,279 @@
1
- # @optimystic/quereus-plugin-crypto
2
-
3
- Quereus plugin providing cryptographic functions for SQL queries with base64url encoding by default.
4
-
5
- ## Features
6
-
7
- - **Hash Functions**: SHA-256, SHA-512, BLAKE3 hashing with base64url output
8
- - **Hash Modulo**: Fixed-size hash values (16-bit, 32-bit, etc.) for sharding and partitioning
9
- - **Random Bytes**: Generate cryptographically secure random bytes (default: 256 bits)
10
- - **Signature Functions**: secp256k1, P-256, Ed25519 signing with base64url encoding
11
- - **Verification Functions**: Signature verification for all supported curves
12
- - **Key Generation**: Generate and derive cryptographic keys
13
- - **Multiple Encodings**: Support for base64url, base64, hex, utf8, and raw bytes
14
-
15
- ## Installation
16
-
17
- ```bash
18
- npm install @optimystic/quereus-plugin-crypto
19
- ```
20
-
21
- ## Usage
22
-
23
- ### As a Quereus Plugin
24
-
25
- ```typescript
26
- import { Database } from '@quereus/quereus';
27
- import { loadPlugin } from '@quereus/quereus/util/plugin-loader.js';
28
-
29
- const db = new Database();
30
- await loadPlugin('npm:@optimystic/quereus-plugin-crypto', db);
31
- ```
32
-
33
- ### Direct Import (for JavaScript/TypeScript code)
34
-
35
- All cryptographic functions are also exported directly so you can use the same implementations in your JavaScript code:
36
-
37
- ```typescript
38
- import {
39
- digest,
40
- hashMod,
41
- randomBytes,
42
- sign,
43
- verify,
44
- generatePrivateKey,
45
- getPublicKey
46
- } from '@optimystic/quereus-plugin-crypto';
47
-
48
- // Hash data (base64url by default)
49
- const hash = digest('hello world', 'sha256', 'utf8', 'base64url');
50
-
51
- // Get a 16-bit hash for sharding
52
- const shard = hashMod('user@example.com', 16, 'sha256', 'utf8');
53
-
54
- // Generate random bytes (256 bits by default)
55
- const nonce = randomBytes(256, 'base64url');
56
- const salt = randomBytes(128, 'hex');
57
-
58
- // Generate keys
59
- const privateKey = generatePrivateKey('secp256k1', 'base64url');
60
- const publicKey = getPublicKey(privateKey, 'secp256k1', 'base64url', 'base64url');
61
-
62
- // Sign and verify
63
- const signature = sign(hash, privateKey, 'secp256k1', 'base64url', 'base64url', 'base64url');
64
- const isValid = verify(hash, signature, publicKey, 'secp256k1', 'base64url', 'base64url', 'base64url');
65
- ```
66
-
67
- ## SQL Functions
68
-
69
- All SQL functions use **base64url encoding by default** for inputs and outputs. This is URL-safe and SQL-friendly.
70
-
71
- ### digest(data, algorithm?, inputEncoding?, outputEncoding?)
72
-
73
- Hash data using SHA-256 (default), SHA-512, or BLAKE3.
74
-
75
- ```sql
76
- -- Hash base64url data (default)
77
- SELECT digest('aGVsbG8gd29ybGQ') as hash;
78
-
79
- -- Hash UTF-8 text
80
- SELECT digest('hello world', 'sha256', 'utf8', 'base64url') as hash;
81
-
82
- -- Hash with SHA-512
83
- SELECT digest('data', 'sha512', 'utf8', 'base64url') as sha512_hash;
84
-
85
- -- Hash with BLAKE3, output as hex
86
- SELECT digest('data', 'blake3', 'utf8', 'hex') as blake3_hex;
87
- ```
88
-
89
- ### hash_mod(data, bits, algorithm?, inputEncoding?)
90
-
91
- Hash data and return modulo 2^bits for fixed-size hash values.
92
-
93
- ```sql
94
- -- Get 16-bit hash (0-65535) for sharding
95
- SELECT hash_mod('user@example.com', 16, 'sha256', 'utf8') as shard_id;
96
-
97
- -- Get 32-bit hash
98
- SELECT hash_mod('session_token', 32, 'sha256', 'utf8') as hash32;
99
-
100
- -- Use with base64url input
101
- SELECT hash_mod('aGVsbG8', 16) as hash16;
102
- ```
103
-
104
- ### random_bytes(bits?, encoding?)
105
-
106
- Generate cryptographically secure random bytes.
107
-
108
- ```sql
109
- -- Generate 256 bits (32 bytes) of random data (default)
110
- SELECT random_bytes() as nonce;
111
-
112
- -- Generate 128 bits as hex
113
- SELECT random_bytes(128, 'hex') as salt;
114
-
115
- -- Generate 512 bits as base64url
116
- SELECT random_bytes(512, 'base64url') as token;
117
-
118
- -- Generate 64 bits for a random ID
119
- SELECT random_bytes(64) as random_id;
120
- ```
121
-
122
- ### sign(data, privateKey, curve?, inputEncoding?, keyEncoding?, outputEncoding?)
123
-
124
- Sign data using secp256k1 (default), P-256, or Ed25519.
125
-
126
- ```sql
127
- -- Sign with secp256k1 (Bitcoin/Ethereum)
128
- SELECT sign('aGVsbG8', 'cHJpdmF0ZUtleQ') as signature;
129
-
130
- -- Sign UTF-8 text
131
- SELECT sign('hello', 'cHJpdmF0ZUtleQ', 'secp256k1', 'utf8') as signature;
132
-
133
- -- Sign with P-256 (NIST)
134
- SELECT sign('data', 'cHJpdmF0ZUtleQ', 'p256', 'utf8') as p256_sig;
135
-
136
- -- Sign with Ed25519
137
- SELECT sign('message', 'cHJpdmF0ZUtleQ', 'ed25519', 'utf8') as ed25519_sig;
138
- ```
139
-
140
- ### verify(data, signature, publicKey, curve?, inputEncoding?, sigEncoding?, keyEncoding?)
141
-
142
- Verify signatures. Returns true for valid, false for invalid.
143
-
144
- ```sql
145
- -- Verify secp256k1 signature
146
- SELECT verify('aGVsbG8', 'c2lnbmF0dXJl', 'cHVibGljS2V5') as is_valid;
147
-
148
- -- Verify UTF-8 text signature
149
- SELECT verify('hello', 'c2lnbmF0dXJl', 'cHVibGljS2V5', 'secp256k1', 'utf8') as is_valid;
150
-
151
- -- Verify P-256 signature
152
- SELECT verify('data', 'c2lnbmF0dXJl', 'cHVibGljS2V5', 'p256', 'utf8') as is_valid;
153
- ```
154
-
155
- ## Supported Algorithms
156
-
157
- ### Hash Algorithms
158
- - `sha256` - SHA-256 (default) - 256-bit secure hash
159
- - `sha512` - SHA-512 - 512-bit secure hash
160
- - `blake3` - BLAKE3 - Fast, secure, parallel hash
161
-
162
- ### Elliptic Curves
163
- - `secp256k1` - Bitcoin/Ethereum curve (default)
164
- - `p256` - NIST P-256 curve (secp256r1)
165
- - `ed25519` - Edwards curve for EdDSA
166
-
167
- ### Encoding Formats
168
- - `base64url` - URL-safe base64 without padding (default)
169
- - `base64` - Standard base64 encoding
170
- - `hex` - Hexadecimal encoding
171
- - `utf8` - UTF-8 text encoding
172
- - `bytes` - Raw Uint8Array (JavaScript only)
173
-
174
- ## Why base64url?
175
-
176
- Base64url is the default encoding because it's:
177
- - **URL-safe**: No `+`, `/`, or `=` characters
178
- - **SQL-friendly**: No special characters that need escaping
179
- - **Compact**: More efficient than hex (33% smaller)
180
- - **Standard**: Defined in RFC 4648
181
-
182
- ## Complete Example
183
-
184
- ```typescript
185
- import { Database } from '@quereus/quereus';
186
- import { loadPlugin } from '@quereus/quereus/util/plugin-loader.js';
187
- import {
188
- digest,
189
- randomBytes,
190
- sign,
191
- verify,
192
- generatePrivateKey,
193
- getPublicKey
194
- } from '@optimystic/quereus-plugin-crypto';
195
-
196
- // Load plugin
197
- const db = new Database();
198
- await loadPlugin('npm:@optimystic/quereus-plugin-crypto', db);
199
-
200
- // Use in SQL
201
- db.exec(`
202
- CREATE TABLE users (
203
- id INTEGER PRIMARY KEY,
204
- email TEXT,
205
- shard_id INTEGER AS (hash_mod(email, 16, 'sha256', 'utf8')),
206
- nonce TEXT DEFAULT (random_bytes(256))
207
- );
208
-
209
- INSERT INTO users (id, email) VALUES (1, 'alice@example.com');
210
-
211
- SELECT id, email, shard_id, nonce FROM users;
212
- `);
213
-
214
- // Use in JavaScript
215
- const privateKey = generatePrivateKey('secp256k1', 'base64url');
216
- const publicKey = getPublicKey(privateKey);
217
-
218
- const message = 'Hello, World!';
219
- const hash = digest(message, 'sha256', 'utf8', 'base64url');
220
- const signature = sign(hash, privateKey);
221
- const isValid = verify(hash, signature, publicKey);
222
-
223
- console.log('Valid signature:', isValid); // true
224
-
225
- // Generate random nonce
226
- const nonce = randomBytes(256, 'base64url');
227
- console.log('Random nonce:', nonce);
228
- ```
229
-
230
- ## JavaScript API Reference
231
-
232
- ### digest(data, algorithm?, inputEncoding?, outputEncoding?)
233
- Hash data using SHA-256, SHA-512, or BLAKE3.
234
- - **Returns**: Hash as string (or Uint8Array if encoding is 'bytes')
235
-
236
- ### hashMod(data, bits, algorithm?, inputEncoding?)
237
- Hash data and return modulo 2^bits for fixed-size hash values.
238
- - **Returns**: Number between 0 and 2^bits - 1
239
-
240
- ### randomBytes(bits?, encoding?)
241
- Generate cryptographically secure random bytes.
242
- - **Default**: 256 bits, base64url encoding
243
- - **Returns**: Random bytes as string (or Uint8Array if encoding is 'bytes')
244
-
245
- ### sign(data, privateKey, curve?, inputEncoding?, keyEncoding?, outputEncoding?)
246
- Sign data using ECC (secp256k1, P-256, or Ed25519).
247
- - **Returns**: Signature as string (or Uint8Array if encoding is 'bytes')
248
-
249
- ### verify(data, signature, publicKey, curve?, inputEncoding?, sigEncoding?, keyEncoding?)
250
- Verify an ECC signature.
251
- - **Returns**: Boolean (true if valid, false if invalid)
252
-
253
- ### generatePrivateKey(curve?, encoding?)
254
- Generate a random private key for the specified curve.
255
- - **Returns**: Private key as string (or Uint8Array if encoding is 'bytes')
256
-
257
- ### getPublicKey(privateKey, curve?, keyEncoding?, outputEncoding?)
258
- Derive the public key from a private key.
259
- - **Returns**: Public key as string (or Uint8Array if encoding is 'bytes')
260
-
261
- ## Security Notes
262
-
263
- - All cryptographic operations use audited libraries (@noble/*)
264
- - Private keys should be handled securely and never exposed
265
- - Random number generation uses platform CSPRNG (crypto.getRandomValues)
266
- - Signatures use deterministic k-generation (RFC 6979)
267
- - All algorithms provide industry-standard security levels
268
-
269
- ## React Native Compatibility
270
-
271
- These functions are fully compatible with React Native and don't require any native modules. They use:
272
- - `@noble/curves` for elliptic curve operations
273
- - `@noble/hashes` for hashing
274
- - `crypto.getRandomValues()` for secure randomness (polyfill required for React Native)
275
-
276
- ## License
277
-
278
- MIT
279
-
1
+ # @optimystic/quereus-plugin-crypto
2
+
3
+ Quereus plugin providing cryptographic functions for SQL queries with base64url encoding by default.
4
+
5
+ ## Features
6
+
7
+ - **Hash Functions**: SHA-256, SHA-512, BLAKE3 hashing with base64url output
8
+ - **Hash Modulo**: Fixed-size hash values (16-bit, 32-bit, etc.) for sharding and partitioning
9
+ - **Random Bytes**: Generate cryptographically secure random bytes (default: 256 bits)
10
+ - **Signature Functions**: secp256k1, P-256, Ed25519 signing with base64url encoding
11
+ - **Verification Functions**: Signature verification for all supported curves
12
+ - **Key Generation**: Generate and derive cryptographic keys
13
+ - **Multiple Encodings**: Support for base64url, base64, hex, utf8, and raw bytes
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @optimystic/quereus-plugin-crypto
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### As a Quereus Plugin
24
+
25
+ ```typescript
26
+ import { Database } from '@quereus/quereus';
27
+ import { loadPlugin } from '@quereus/quereus/util/plugin-loader.js';
28
+
29
+ const db = new Database();
30
+ await loadPlugin('npm:@optimystic/quereus-plugin-crypto', db);
31
+ ```
32
+
33
+ ### Direct Import (for JavaScript/TypeScript code)
34
+
35
+ All cryptographic functions are also exported directly so you can use the same implementations in your JavaScript code:
36
+
37
+ ```typescript
38
+ import {
39
+ digest,
40
+ hashMod,
41
+ randomBytes,
42
+ sign,
43
+ verify,
44
+ generatePrivateKey,
45
+ getPublicKey
46
+ } from '@optimystic/quereus-plugin-crypto';
47
+
48
+ // Hash data (base64url by default)
49
+ const hash = digest('hello world', 'sha256', 'utf8', 'base64url');
50
+
51
+ // Get a 16-bit hash for sharding
52
+ const shard = hashMod('user@example.com', 16, 'sha256', 'utf8');
53
+
54
+ // Generate random bytes (256 bits by default)
55
+ const nonce = randomBytes(256, 'base64url');
56
+ const salt = randomBytes(128, 'hex');
57
+
58
+ // Generate keys
59
+ const privateKey = generatePrivateKey('secp256k1', 'base64url');
60
+ const publicKey = getPublicKey(privateKey, 'secp256k1', 'base64url', 'base64url');
61
+
62
+ // Sign and verify
63
+ const signature = sign(hash, privateKey, 'secp256k1', 'base64url', 'base64url', 'base64url');
64
+ const isValid = verify(hash, signature, publicKey, 'secp256k1', 'base64url', 'base64url', 'base64url');
65
+ ```
66
+
67
+ ## SQL Functions
68
+
69
+ All SQL functions use **base64url encoding by default** for inputs and outputs. This is URL-safe and SQL-friendly.
70
+
71
+ ### digest(data, algorithm?, inputEncoding?, outputEncoding?)
72
+
73
+ Hash data using SHA-256 (default), SHA-512, or BLAKE3.
74
+
75
+ ```sql
76
+ -- Hash base64url data (default)
77
+ SELECT digest('aGVsbG8gd29ybGQ') as hash;
78
+
79
+ -- Hash UTF-8 text
80
+ SELECT digest('hello world', 'sha256', 'utf8', 'base64url') as hash;
81
+
82
+ -- Hash with SHA-512
83
+ SELECT digest('data', 'sha512', 'utf8', 'base64url') as sha512_hash;
84
+
85
+ -- Hash with BLAKE3, output as hex
86
+ SELECT digest('data', 'blake3', 'utf8', 'hex') as blake3_hex;
87
+ ```
88
+
89
+ ### hash_mod(data, bits, algorithm?, inputEncoding?)
90
+
91
+ Hash data and return modulo 2^bits for fixed-size hash values.
92
+
93
+ ```sql
94
+ -- Get 16-bit hash (0-65535) for sharding
95
+ SELECT hash_mod('user@example.com', 16, 'sha256', 'utf8') as shard_id;
96
+
97
+ -- Get 32-bit hash
98
+ SELECT hash_mod('session_token', 32, 'sha256', 'utf8') as hash32;
99
+
100
+ -- Use with base64url input
101
+ SELECT hash_mod('aGVsbG8', 16) as hash16;
102
+ ```
103
+
104
+ ### random_bytes(bits?, encoding?)
105
+
106
+ Generate cryptographically secure random bytes.
107
+
108
+ ```sql
109
+ -- Generate 256 bits (32 bytes) of random data (default)
110
+ SELECT random_bytes() as nonce;
111
+
112
+ -- Generate 128 bits as hex
113
+ SELECT random_bytes(128, 'hex') as salt;
114
+
115
+ -- Generate 512 bits as base64url
116
+ SELECT random_bytes(512, 'base64url') as token;
117
+
118
+ -- Generate 64 bits for a random ID
119
+ SELECT random_bytes(64) as random_id;
120
+ ```
121
+
122
+ ### sign(data, privateKey, curve?, inputEncoding?, keyEncoding?, outputEncoding?)
123
+
124
+ Sign data using secp256k1 (default), P-256, or Ed25519.
125
+
126
+ ```sql
127
+ -- Sign with secp256k1 (Bitcoin/Ethereum)
128
+ SELECT sign('aGVsbG8', 'cHJpdmF0ZUtleQ') as signature;
129
+
130
+ -- Sign UTF-8 text
131
+ SELECT sign('hello', 'cHJpdmF0ZUtleQ', 'secp256k1', 'utf8') as signature;
132
+
133
+ -- Sign with P-256 (NIST)
134
+ SELECT sign('data', 'cHJpdmF0ZUtleQ', 'p256', 'utf8') as p256_sig;
135
+
136
+ -- Sign with Ed25519
137
+ SELECT sign('message', 'cHJpdmF0ZUtleQ', 'ed25519', 'utf8') as ed25519_sig;
138
+ ```
139
+
140
+ ### verify(data, signature, publicKey, curve?, inputEncoding?, sigEncoding?, keyEncoding?)
141
+
142
+ Verify signatures. Returns true for valid, false for invalid.
143
+
144
+ ```sql
145
+ -- Verify secp256k1 signature
146
+ SELECT verify('aGVsbG8', 'c2lnbmF0dXJl', 'cHVibGljS2V5') as is_valid;
147
+
148
+ -- Verify UTF-8 text signature
149
+ SELECT verify('hello', 'c2lnbmF0dXJl', 'cHVibGljS2V5', 'secp256k1', 'utf8') as is_valid;
150
+
151
+ -- Verify P-256 signature
152
+ SELECT verify('data', 'c2lnbmF0dXJl', 'cHVibGljS2V5', 'p256', 'utf8') as is_valid;
153
+ ```
154
+
155
+ ## Supported Algorithms
156
+
157
+ ### Hash Algorithms
158
+ - `sha256` - SHA-256 (default) - 256-bit secure hash
159
+ - `sha512` - SHA-512 - 512-bit secure hash
160
+ - `blake3` - BLAKE3 - Fast, secure, parallel hash
161
+
162
+ ### Elliptic Curves
163
+ - `secp256k1` - Bitcoin/Ethereum curve (default)
164
+ - `p256` - NIST P-256 curve (secp256r1)
165
+ - `ed25519` - Edwards curve for EdDSA
166
+
167
+ ### Encoding Formats
168
+ - `base64url` - URL-safe base64 without padding (default)
169
+ - `base64` - Standard base64 encoding
170
+ - `hex` - Hexadecimal encoding
171
+ - `utf8` - UTF-8 text encoding
172
+ - `bytes` - Raw Uint8Array (JavaScript only)
173
+
174
+ ## Why base64url?
175
+
176
+ Base64url is the default encoding because it's:
177
+ - **URL-safe**: No `+`, `/`, or `=` characters
178
+ - **SQL-friendly**: No special characters that need escaping
179
+ - **Compact**: More efficient than hex (33% smaller)
180
+ - **Standard**: Defined in RFC 4648
181
+
182
+ ## Complete Example
183
+
184
+ ```typescript
185
+ import { Database } from '@quereus/quereus';
186
+ import { loadPlugin } from '@quereus/quereus/util/plugin-loader.js';
187
+ import {
188
+ digest,
189
+ randomBytes,
190
+ sign,
191
+ verify,
192
+ generatePrivateKey,
193
+ getPublicKey
194
+ } from '@optimystic/quereus-plugin-crypto';
195
+
196
+ // Load plugin
197
+ const db = new Database();
198
+ await loadPlugin('npm:@optimystic/quereus-plugin-crypto', db);
199
+
200
+ // Use in SQL
201
+ db.exec(`
202
+ CREATE TABLE users (
203
+ id INTEGER PRIMARY KEY,
204
+ email TEXT,
205
+ shard_id INTEGER AS (hash_mod(email, 16, 'sha256', 'utf8')),
206
+ nonce TEXT DEFAULT (random_bytes(256))
207
+ );
208
+
209
+ INSERT INTO users (id, email) VALUES (1, 'alice@example.com');
210
+
211
+ SELECT id, email, shard_id, nonce FROM users;
212
+ `);
213
+
214
+ // Use in JavaScript
215
+ const privateKey = generatePrivateKey('secp256k1', 'base64url');
216
+ const publicKey = getPublicKey(privateKey);
217
+
218
+ const message = 'Hello, World!';
219
+ const hash = digest(message, 'sha256', 'utf8', 'base64url');
220
+ const signature = sign(hash, privateKey);
221
+ const isValid = verify(hash, signature, publicKey);
222
+
223
+ console.log('Valid signature:', isValid); // true
224
+
225
+ // Generate random nonce
226
+ const nonce = randomBytes(256, 'base64url');
227
+ console.log('Random nonce:', nonce);
228
+ ```
229
+
230
+ ## JavaScript API Reference
231
+
232
+ ### digest(data, algorithm?, inputEncoding?, outputEncoding?)
233
+ Hash data using SHA-256, SHA-512, or BLAKE3.
234
+ - **Returns**: Hash as string (or Uint8Array if encoding is 'bytes')
235
+
236
+ ### hashMod(data, bits, algorithm?, inputEncoding?)
237
+ Hash data and return modulo 2^bits for fixed-size hash values.
238
+ - **Returns**: Number between 0 and 2^bits - 1
239
+
240
+ ### randomBytes(bits?, encoding?)
241
+ Generate cryptographically secure random bytes.
242
+ - **Default**: 256 bits, base64url encoding
243
+ - **Returns**: Random bytes as string (or Uint8Array if encoding is 'bytes')
244
+
245
+ ### sign(data, privateKey, curve?, inputEncoding?, keyEncoding?, outputEncoding?)
246
+ Sign data using ECC (secp256k1, P-256, or Ed25519).
247
+ - **Returns**: Signature as string (or Uint8Array if encoding is 'bytes')
248
+
249
+ ### verify(data, signature, publicKey, curve?, inputEncoding?, sigEncoding?, keyEncoding?)
250
+ Verify an ECC signature.
251
+ - **Returns**: Boolean (true if valid, false if invalid)
252
+
253
+ ### generatePrivateKey(curve?, encoding?)
254
+ Generate a random private key for the specified curve.
255
+ - **Returns**: Private key as string (or Uint8Array if encoding is 'bytes')
256
+
257
+ ### getPublicKey(privateKey, curve?, keyEncoding?, outputEncoding?)
258
+ Derive the public key from a private key.
259
+ - **Returns**: Public key as string (or Uint8Array if encoding is 'bytes')
260
+
261
+ ## Security Notes
262
+
263
+ - All cryptographic operations use audited libraries (@noble/*)
264
+ - Private keys should be handled securely and never exposed
265
+ - Random number generation uses platform CSPRNG (crypto.getRandomValues)
266
+ - Signatures use deterministic k-generation (RFC 6979)
267
+ - All algorithms provide industry-standard security levels
268
+
269
+ ## React Native Compatibility
270
+
271
+ These functions are fully compatible with React Native and don't require any native modules. They use:
272
+ - `@noble/curves` for elliptic curve operations
273
+ - `@noble/hashes` for hashing
274
+ - `crypto.getRandomValues()` for secure randomness (polyfill required for React Native)
275
+
276
+ ## License
277
+
278
+ MIT
279
+