cipher-kit 3.0.0-beta.0 → 3.0.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 +3 -1
- package/dist/{chunk-ZYX6MYHS.cjs → chunk-3A4RTUKO.cjs} +101 -101
- package/dist/chunk-3A4RTUKO.cjs.map +1 -0
- package/dist/{chunk-LC5HP75W.cjs → chunk-7254PEID.cjs} +102 -102
- package/dist/chunk-7254PEID.cjs.map +1 -0
- package/dist/{chunk-ZN7EFZ7Z.js → chunk-GL32EZRA.js} +5 -5
- package/dist/chunk-GL32EZRA.js.map +1 -0
- package/dist/{chunk-4YR2SBOH.js → chunk-IY6XGUYO.js} +8 -2
- package/dist/chunk-IY6XGUYO.js.map +1 -0
- package/dist/{chunk-X7IPAA7B.cjs → chunk-VCBHSRCS.cjs} +8 -2
- package/dist/chunk-VCBHSRCS.cjs.map +1 -0
- package/dist/{chunk-3NFJMV33.js → chunk-X6MX4NDE.js} +3 -3
- package/dist/chunk-X6MX4NDE.js.map +1 -0
- package/dist/{export-DAIDJRX3.d.ts → export-B-3CCZIO.d.cts} +2 -1
- package/dist/{export-BEp4DFEB.d.cts → export-BPo6yPV-.d.ts} +2 -1
- package/dist/{export-Cxfev8fi.d.ts → export-C0_UEEg8.d.ts} +2 -1
- package/dist/{export-e7IN_bU5.d.cts → export-DPuocAr3.d.cts} +2 -1
- package/dist/index.cjs +11 -11
- package/dist/index.d.cts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +3 -3
- package/dist/node.cjs +33 -33
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{validate-DN59A2-Y.d.cts → validate-vDTesb-X.d.cts} +4 -1
- package/dist/{validate-DN59A2-Y.d.ts → validate-vDTesb-X.d.ts} +4 -1
- package/dist/web-api.cjs +33 -33
- package/dist/web-api.d.cts +2 -2
- package/dist/web-api.d.ts +2 -2
- package/dist/web-api.js +2 -2
- package/package.json +2 -2
- package/dist/chunk-3NFJMV33.js.map +0 -1
- package/dist/chunk-4YR2SBOH.js.map +0 -1
- package/dist/chunk-LC5HP75W.cjs.map +0 -1
- package/dist/chunk-X7IPAA7B.cjs.map +0 -1
- package/dist/chunk-ZN7EFZ7Z.js.map +0 -1
- package/dist/chunk-ZYX6MYHS.cjs.map +0 -1
package/README.md
CHANGED
|
@@ -137,6 +137,8 @@ if (result.success) {
|
|
|
137
137
|
```
|
|
138
138
|
|
|
139
139
|
> **Wire format:** Both platforms output `iv.cipher.tag.` (3 dot-separated segments with trailing dot). The format is **cross-platform compatible** — data encrypted on Node can be decrypted on Web and vice versa.
|
|
140
|
+
>
|
|
141
|
+
> **Nonce exhaustion:** AES-GCM uses random 96-bit IVs. Rotate keys before ~2^32 encryptions with the same key to avoid nonce collision.
|
|
140
142
|
|
|
141
143
|
### `encryptObj` / `decryptObj` / `tryEncryptObj` / `tryDecryptObj`
|
|
142
144
|
|
|
@@ -245,7 +247,7 @@ isWebSecretKey(key); // true if key is WebSecretKey
|
|
|
245
247
|
|
|
246
248
|
### Regex Utilities
|
|
247
249
|
|
|
248
|
-
Validate the structural shape of encrypted payloads before decryption.
|
|
250
|
+
Validate the structural shape of encrypted payloads before decryption. This is a **structural** check only — it validates the dot-separated format but does not verify whether individual segments contain valid base64, base64url, or hex encoding.
|
|
249
251
|
|
|
250
252
|
```typescript
|
|
251
253
|
import { ENCRYPTED_REGEX, matchEncryptedPattern } from "cipher-kit";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkVCBHSRCS_cjs = require('./chunk-VCBHSRCS.cjs');
|
|
4
4
|
var nodeCrypto3 = require('crypto');
|
|
5
5
|
var buffer = require('buffer');
|
|
6
6
|
|
|
@@ -10,7 +10,7 @@ var nodeCrypto3__default = /*#__PURE__*/_interopDefault(nodeCrypto3);
|
|
|
10
10
|
|
|
11
11
|
// src/node/kit.ts
|
|
12
12
|
var kit_exports = {};
|
|
13
|
-
|
|
13
|
+
chunkVCBHSRCS_cjs.__export(kit_exports, {
|
|
14
14
|
convertBytesToStr: () => convertBytesToStr,
|
|
15
15
|
convertEncoding: () => convertEncoding,
|
|
16
16
|
convertStrToBytes: () => convertStrToBytes,
|
|
@@ -39,13 +39,13 @@ chunkX7IPAA7B_cjs.__export(kit_exports, {
|
|
|
39
39
|
});
|
|
40
40
|
function $convertStrToBytes(data, inputEncoding = "utf8") {
|
|
41
41
|
if (typeof data !== "string") {
|
|
42
|
-
return
|
|
42
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
43
43
|
message: "node strToBytes: Data must be a string",
|
|
44
44
|
description: `Expected a string value, received ${typeof data}`
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
|
-
if (!
|
|
48
|
-
return
|
|
47
|
+
if (!chunkVCBHSRCS_cjs.ENCODING.includes(inputEncoding)) {
|
|
48
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
49
49
|
message: `node strToBytes: Unsupported encoding: ${inputEncoding}`,
|
|
50
50
|
description: "Use base64, base64url, hex, utf8, or latin1"
|
|
51
51
|
});
|
|
@@ -53,16 +53,16 @@ function $convertStrToBytes(data, inputEncoding = "utf8") {
|
|
|
53
53
|
if (inputEncoding === "hex") {
|
|
54
54
|
const clean = /^0x/i.test(data) ? data.slice(2) : data;
|
|
55
55
|
if (clean.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(clean)) {
|
|
56
|
-
return
|
|
56
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
57
57
|
message: "node strToBytes: Invalid hex string",
|
|
58
58
|
description: "Hex string contains non-hex characters or has odd length"
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
|
-
return
|
|
61
|
+
return chunkVCBHSRCS_cjs.$ok({ result: buffer.Buffer.from(clean, "hex") });
|
|
62
62
|
}
|
|
63
63
|
if (inputEncoding === "base64") {
|
|
64
64
|
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}={2}|[A-Za-z0-9+/]{3}=)?$/.test(data)) {
|
|
65
|
-
return
|
|
65
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
66
66
|
message: "node strToBytes: Invalid base64 string",
|
|
67
67
|
description: "Base64 string contains invalid characters or has incorrect padding"
|
|
68
68
|
});
|
|
@@ -70,58 +70,58 @@ function $convertStrToBytes(data, inputEncoding = "utf8") {
|
|
|
70
70
|
}
|
|
71
71
|
if (inputEncoding === "base64url") {
|
|
72
72
|
if (!/^[A-Za-z0-9_-]*={0,2}$/.test(data) || data.replace(/=+$/, "").length % 4 === 1) {
|
|
73
|
-
return
|
|
73
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
74
74
|
message: "node strToBytes: Invalid base64url string",
|
|
75
75
|
description: "Base64url string contains invalid characters or has incorrect length"
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
try {
|
|
80
|
-
return
|
|
80
|
+
return chunkVCBHSRCS_cjs.$ok({ result: buffer.Buffer.from(data, inputEncoding) });
|
|
81
81
|
} catch (error) {
|
|
82
|
-
return
|
|
82
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node strToBytes: Failed to convert data", description: chunkVCBHSRCS_cjs.$fmtError(error) });
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
function $convertBytesToStr(data, outputEncoding = "utf8") {
|
|
86
86
|
if (!(data instanceof buffer.Buffer)) {
|
|
87
|
-
return
|
|
87
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
88
88
|
message: "node bytesToStr: Data must be a Buffer",
|
|
89
89
|
description: "Received a non-Buffer value"
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
|
-
if (!
|
|
93
|
-
return
|
|
92
|
+
if (!chunkVCBHSRCS_cjs.ENCODING.includes(outputEncoding)) {
|
|
93
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
94
94
|
message: `node bytesToStr: Unsupported encoding: ${outputEncoding}`,
|
|
95
95
|
description: "Use base64, base64url, hex, utf8, or latin1"
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
try {
|
|
99
|
-
return
|
|
99
|
+
return chunkVCBHSRCS_cjs.$ok(data.toString(outputEncoding));
|
|
100
100
|
} catch (error) {
|
|
101
|
-
return
|
|
101
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node bytesToStr: Failed to convert data", description: chunkVCBHSRCS_cjs.$fmtError(error) });
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
function $convertEncoding(data, from, to) {
|
|
105
|
-
if (!
|
|
106
|
-
return
|
|
105
|
+
if (!chunkVCBHSRCS_cjs.$isStr(data)) {
|
|
106
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
107
107
|
message: "node convertEncoding: Data must be a non-empty string",
|
|
108
108
|
description: "Received empty or non-string value"
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
|
-
if (!
|
|
112
|
-
return
|
|
111
|
+
if (!chunkVCBHSRCS_cjs.ENCODING.includes(from) || !chunkVCBHSRCS_cjs.ENCODING.includes(to)) {
|
|
112
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
113
113
|
message: `node convertEncoding: Unsupported encoding: from ${from} to ${to}`,
|
|
114
114
|
description: "Use base64, base64url, hex, utf8, or latin1"
|
|
115
115
|
});
|
|
116
116
|
}
|
|
117
117
|
const bytes = $convertStrToBytes(data, from);
|
|
118
|
-
if (bytes.error) return
|
|
118
|
+
if (bytes.error) return chunkVCBHSRCS_cjs.$err(bytes.error);
|
|
119
119
|
const str = $convertBytesToStr(bytes.result, to);
|
|
120
|
-
if (str.error) return
|
|
121
|
-
return
|
|
120
|
+
if (str.error) return chunkVCBHSRCS_cjs.$err(str.error);
|
|
121
|
+
return chunkVCBHSRCS_cjs.$ok(str.result);
|
|
122
122
|
}
|
|
123
123
|
function $isNodeSecretKey(x) {
|
|
124
|
-
const base =
|
|
124
|
+
const base = chunkVCBHSRCS_cjs.$validateSecretKeyBase(x, "node");
|
|
125
125
|
if (!base) return null;
|
|
126
126
|
if (!(base.obj.key instanceof nodeCrypto3__default.default.KeyObject) || typeof base.obj.key.symmetricKeySize === "number" && base.obj.key.symmetricKeySize !== base.algorithm.keyBytes) {
|
|
127
127
|
return null;
|
|
@@ -129,8 +129,8 @@ function $isNodeSecretKey(x) {
|
|
|
129
129
|
return x;
|
|
130
130
|
}
|
|
131
131
|
function $createSecretKey(secret, options) {
|
|
132
|
-
const validated =
|
|
133
|
-
if (validated.error) return
|
|
132
|
+
const validated = chunkVCBHSRCS_cjs.$validateCreateSecretKeyOptions(secret, options, "node");
|
|
133
|
+
if (validated.error) return chunkVCBHSRCS_cjs.$err(validated.error);
|
|
134
134
|
const { algorithm, digest, salt, info, encryptAlgo, digestAlgo } = validated;
|
|
135
135
|
try {
|
|
136
136
|
const derivedKey = Buffer.from(
|
|
@@ -151,47 +151,47 @@ function $createSecretKey(secret, options) {
|
|
|
151
151
|
key,
|
|
152
152
|
injected: encryptAlgo
|
|
153
153
|
});
|
|
154
|
-
return
|
|
154
|
+
return chunkVCBHSRCS_cjs.$ok({ result: secretKey });
|
|
155
155
|
} finally {
|
|
156
156
|
derivedKey.fill(0);
|
|
157
157
|
}
|
|
158
158
|
} catch (error) {
|
|
159
|
-
return
|
|
159
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node createSecretKey: Failed to derive key", description: chunkVCBHSRCS_cjs.$fmtError(error) });
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
// src/node/node-encrypt.ts
|
|
164
164
|
function $encrypt(data, secretKey, options) {
|
|
165
|
-
if (!
|
|
166
|
-
return
|
|
165
|
+
if (!chunkVCBHSRCS_cjs.$isStr(data)) {
|
|
166
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
167
167
|
message: "node encrypt: Data must be a non-empty string",
|
|
168
168
|
description: "Received empty or non-string value"
|
|
169
169
|
});
|
|
170
170
|
}
|
|
171
|
-
if (!
|
|
172
|
-
return
|
|
171
|
+
if (!chunkVCBHSRCS_cjs.$isPlainObj(options)) {
|
|
172
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
173
173
|
message: "node encrypt: Options must be a plain object",
|
|
174
174
|
description: 'Pass an object like { outputEncoding: "base64url" }'
|
|
175
175
|
});
|
|
176
176
|
}
|
|
177
177
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
178
|
-
if (!
|
|
179
|
-
return
|
|
178
|
+
if (!chunkVCBHSRCS_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
179
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
180
180
|
message: `node encrypt: Unsupported output encoding: ${outputEncoding}`,
|
|
181
181
|
description: "Use base64, base64url, or hex"
|
|
182
182
|
});
|
|
183
183
|
}
|
|
184
184
|
const injectedKey = $isNodeSecretKey(secretKey);
|
|
185
185
|
if (!injectedKey) {
|
|
186
|
-
return
|
|
186
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
187
187
|
message: "node encrypt: Invalid secret key",
|
|
188
188
|
description: "Expected a NodeSecretKey created by nodeKit.createSecretKey()"
|
|
189
189
|
});
|
|
190
190
|
}
|
|
191
191
|
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
192
|
-
if (error) return
|
|
192
|
+
if (error) return chunkVCBHSRCS_cjs.$err(error);
|
|
193
193
|
try {
|
|
194
|
-
const iv = nodeCrypto3__default.default.randomBytes(
|
|
194
|
+
const iv = nodeCrypto3__default.default.randomBytes(chunkVCBHSRCS_cjs.GCM_IV_LENGTH);
|
|
195
195
|
const cipher = nodeCrypto3__default.default.createCipheriv(injectedKey.injected.node, injectedKey.key, iv);
|
|
196
196
|
const encrypted = buffer.Buffer.concat([cipher.update(result), cipher.final()]);
|
|
197
197
|
const tag = cipher.getAuthTag();
|
|
@@ -199,34 +199,34 @@ function $encrypt(data, secretKey, options) {
|
|
|
199
199
|
const cipherStr = $convertBytesToStr(encrypted, outputEncoding);
|
|
200
200
|
const tagStr = $convertBytesToStr(tag, outputEncoding);
|
|
201
201
|
if (ivStr.error || cipherStr.error || tagStr.error) {
|
|
202
|
-
return
|
|
202
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
203
203
|
message: "node encrypt: Failed to encode output",
|
|
204
|
-
description: `Conversion error: ${
|
|
204
|
+
description: `Conversion error: ${chunkVCBHSRCS_cjs.$fmtResultErr(ivStr.error || cipherStr.error || tagStr.error)}`
|
|
205
205
|
});
|
|
206
206
|
}
|
|
207
|
-
return
|
|
207
|
+
return chunkVCBHSRCS_cjs.$ok(`${ivStr.result}.${cipherStr.result}.${tagStr.result}.`);
|
|
208
208
|
} catch (error2) {
|
|
209
|
-
return
|
|
209
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node encrypt: Failed to encrypt data", description: chunkVCBHSRCS_cjs.$fmtError(error2) });
|
|
210
210
|
} finally {
|
|
211
211
|
result.fill(0);
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
function $decrypt(encrypted, secretKey, options) {
|
|
215
|
-
if (!
|
|
216
|
-
return
|
|
215
|
+
if (!chunkVCBHSRCS_cjs.matchEncryptedPattern(encrypted)) {
|
|
216
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
217
217
|
message: "node decrypt: Invalid encrypted data format",
|
|
218
218
|
description: 'Encrypted data must be in the format "iv.cipher.tag."'
|
|
219
219
|
});
|
|
220
220
|
}
|
|
221
|
-
if (!
|
|
222
|
-
return
|
|
221
|
+
if (!chunkVCBHSRCS_cjs.$isPlainObj(options)) {
|
|
222
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
223
223
|
message: "node decrypt: Options must be a plain object",
|
|
224
224
|
description: 'Pass an object like { inputEncoding: "base64url" }'
|
|
225
225
|
});
|
|
226
226
|
}
|
|
227
227
|
const inputEncoding = options.inputEncoding ?? "base64url";
|
|
228
|
-
if (!
|
|
229
|
-
return
|
|
228
|
+
if (!chunkVCBHSRCS_cjs.CIPHER_ENCODING.includes(inputEncoding)) {
|
|
229
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
230
230
|
message: `node decrypt: Unsupported input encoding: ${inputEncoding}`,
|
|
231
231
|
description: "Use base64, base64url, or hex"
|
|
232
232
|
});
|
|
@@ -234,7 +234,7 @@ function $decrypt(encrypted, secretKey, options) {
|
|
|
234
234
|
const [iv, cipher, tag] = encrypted.split(".", 4);
|
|
235
235
|
const injectedKey = $isNodeSecretKey(secretKey);
|
|
236
236
|
if (!injectedKey) {
|
|
237
|
-
return
|
|
237
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
238
238
|
message: "node decrypt: Invalid secret key",
|
|
239
239
|
description: "Expected a NodeSecretKey created by nodeKit.createSecretKey()"
|
|
240
240
|
});
|
|
@@ -243,21 +243,21 @@ function $decrypt(encrypted, secretKey, options) {
|
|
|
243
243
|
const cipherBytes = $convertStrToBytes(cipher, inputEncoding);
|
|
244
244
|
const tagBytes = $convertStrToBytes(tag, inputEncoding);
|
|
245
245
|
if (ivBytes.error || cipherBytes.error || tagBytes.error) {
|
|
246
|
-
return
|
|
246
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
247
247
|
message: "node decrypt: Failed to decode input",
|
|
248
|
-
description: `Conversion error: ${
|
|
248
|
+
description: `Conversion error: ${chunkVCBHSRCS_cjs.$fmtResultErr(ivBytes.error || cipherBytes.error || tagBytes.error)}`
|
|
249
249
|
});
|
|
250
250
|
}
|
|
251
|
-
if (ivBytes.result.byteLength !==
|
|
252
|
-
return
|
|
251
|
+
if (ivBytes.result.byteLength !== chunkVCBHSRCS_cjs.GCM_IV_LENGTH) {
|
|
252
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
253
253
|
message: "node decrypt: Invalid IV length",
|
|
254
|
-
description: `Expected ${
|
|
254
|
+
description: `Expected ${chunkVCBHSRCS_cjs.GCM_IV_LENGTH} bytes, got ${ivBytes.result.byteLength}`
|
|
255
255
|
});
|
|
256
256
|
}
|
|
257
|
-
if (tagBytes.result.byteLength !==
|
|
258
|
-
return
|
|
257
|
+
if (tagBytes.result.byteLength !== chunkVCBHSRCS_cjs.GCM_TAG_BYTES) {
|
|
258
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
259
259
|
message: "node decrypt: Invalid auth tag length",
|
|
260
|
-
description: `Expected ${
|
|
260
|
+
description: `Expected ${chunkVCBHSRCS_cjs.GCM_TAG_BYTES} bytes, got ${tagBytes.result.byteLength}`
|
|
261
261
|
});
|
|
262
262
|
}
|
|
263
263
|
let decrypted;
|
|
@@ -267,86 +267,86 @@ function $decrypt(encrypted, secretKey, options) {
|
|
|
267
267
|
decrypted = buffer.Buffer.concat([decipher.update(cipherBytes.result), decipher.final()]);
|
|
268
268
|
return $convertBytesToStr(decrypted, "utf8");
|
|
269
269
|
} catch (error) {
|
|
270
|
-
return
|
|
270
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node decrypt: Failed to decrypt data", description: chunkVCBHSRCS_cjs.$fmtError(error) });
|
|
271
271
|
} finally {
|
|
272
272
|
decrypted?.fill(0);
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
275
|
function $encryptObj(data, secretKey, options) {
|
|
276
|
-
const { result, error } =
|
|
277
|
-
if (error) return
|
|
276
|
+
const { result, error } = chunkVCBHSRCS_cjs.$stringifyObj(data);
|
|
277
|
+
if (error) return chunkVCBHSRCS_cjs.$err(error);
|
|
278
278
|
return $encrypt(result, secretKey, options);
|
|
279
279
|
}
|
|
280
280
|
function $decryptObj(encrypted, secretKey, options) {
|
|
281
281
|
const { result, error } = $decrypt(encrypted, secretKey, options);
|
|
282
|
-
if (error) return
|
|
283
|
-
return
|
|
282
|
+
if (error) return chunkVCBHSRCS_cjs.$err(error);
|
|
283
|
+
return chunkVCBHSRCS_cjs.$parseToObj(result);
|
|
284
284
|
}
|
|
285
285
|
function $hash(data, options = {}) {
|
|
286
|
-
if (!
|
|
287
|
-
return
|
|
286
|
+
if (!chunkVCBHSRCS_cjs.$isStr(data)) {
|
|
287
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
288
288
|
message: "node hash: Data must be a non-empty string",
|
|
289
289
|
description: "Received empty or non-string value"
|
|
290
290
|
});
|
|
291
291
|
}
|
|
292
|
-
if (!
|
|
293
|
-
return
|
|
292
|
+
if (!chunkVCBHSRCS_cjs.$isPlainObj(options)) {
|
|
293
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
294
294
|
message: "node hash: Options must be a plain object",
|
|
295
295
|
description: 'Pass an object like { digest: "sha256" }'
|
|
296
296
|
});
|
|
297
297
|
}
|
|
298
298
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
299
|
-
if (!
|
|
300
|
-
return
|
|
299
|
+
if (!chunkVCBHSRCS_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
300
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
301
301
|
message: `node hash: Unsupported output encoding: ${outputEncoding}`,
|
|
302
302
|
description: "Use base64, base64url, or hex"
|
|
303
303
|
});
|
|
304
304
|
}
|
|
305
305
|
const digest = options.digest ?? "sha256";
|
|
306
|
-
if (!(digest in
|
|
307
|
-
return
|
|
306
|
+
if (!(digest in chunkVCBHSRCS_cjs.DIGEST_ALGORITHMS)) {
|
|
307
|
+
return chunkVCBHSRCS_cjs.$err({
|
|
308
308
|
message: `node hash: Unsupported digest: ${digest}`,
|
|
309
|
-
description: `Supported digests are: ${Object.keys(
|
|
309
|
+
description: `Supported digests are: ${Object.keys(chunkVCBHSRCS_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
310
310
|
});
|
|
311
311
|
}
|
|
312
|
-
const digestAlgo =
|
|
312
|
+
const digestAlgo = chunkVCBHSRCS_cjs.DIGEST_ALGORITHMS[digest];
|
|
313
313
|
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
314
|
-
if (error) return
|
|
314
|
+
if (error) return chunkVCBHSRCS_cjs.$err(error);
|
|
315
315
|
try {
|
|
316
316
|
const hashed = nodeCrypto3__default.default.createHash(digestAlgo.node).update(result).digest();
|
|
317
317
|
return $convertBytesToStr(hashed, outputEncoding);
|
|
318
318
|
} catch (error2) {
|
|
319
|
-
return
|
|
319
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node hash: Failed to hash data", description: chunkVCBHSRCS_cjs.$fmtError(error2) });
|
|
320
320
|
}
|
|
321
321
|
}
|
|
322
322
|
function $hashPassword(password, options) {
|
|
323
|
-
const validated =
|
|
324
|
-
if (validated.error) return
|
|
323
|
+
const validated = chunkVCBHSRCS_cjs.$validateHashPasswordOptions(password, options, "node");
|
|
324
|
+
if (validated.error) return chunkVCBHSRCS_cjs.$err(validated.error);
|
|
325
325
|
const { digestAlgo, outputEncoding, saltLength, iterations, keyLength } = validated;
|
|
326
326
|
const salt = nodeCrypto3__default.default.randomBytes(saltLength);
|
|
327
327
|
const hash2 = nodeCrypto3__default.default.pbkdf2Sync(password.normalize("NFKC"), salt, iterations, keyLength, digestAlgo.node);
|
|
328
328
|
try {
|
|
329
329
|
const saltStr = $convertBytesToStr(salt, outputEncoding);
|
|
330
|
-
if (saltStr.error) return
|
|
330
|
+
if (saltStr.error) return chunkVCBHSRCS_cjs.$err(saltStr.error);
|
|
331
331
|
const hashStr = $convertBytesToStr(hash2, outputEncoding);
|
|
332
|
-
if (hashStr.error) return
|
|
333
|
-
return
|
|
332
|
+
if (hashStr.error) return chunkVCBHSRCS_cjs.$err(hashStr.error);
|
|
333
|
+
return chunkVCBHSRCS_cjs.$ok({ result: hashStr.result, salt: saltStr.result });
|
|
334
334
|
} catch (error) {
|
|
335
|
-
return
|
|
335
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node hashPassword: Failed to hash password", description: chunkVCBHSRCS_cjs.$fmtError(error) });
|
|
336
336
|
} finally {
|
|
337
337
|
salt.fill(0);
|
|
338
338
|
hash2.fill(0);
|
|
339
339
|
}
|
|
340
340
|
}
|
|
341
341
|
function $verifyPassword(password, hashedPassword, salt, options) {
|
|
342
|
-
const validated =
|
|
343
|
-
if (validated.error) return
|
|
342
|
+
const validated = chunkVCBHSRCS_cjs.$validateVerifyPasswordOptions(password, hashedPassword, salt, options, "node");
|
|
343
|
+
if (validated.error) return chunkVCBHSRCS_cjs.$err(validated.error);
|
|
344
344
|
const { digestAlgo, inputEncoding, iterations, keyLength } = validated;
|
|
345
345
|
const saltBytes = $convertStrToBytes(salt, inputEncoding);
|
|
346
|
-
if (saltBytes.error) return
|
|
346
|
+
if (saltBytes.error) return chunkVCBHSRCS_cjs.$err(saltBytes.error);
|
|
347
347
|
const hashedPasswordBytes = $convertStrToBytes(hashedPassword, inputEncoding);
|
|
348
|
-
if (hashedPasswordBytes.error) return
|
|
349
|
-
if (hashedPasswordBytes.result.byteLength !== keyLength) return
|
|
348
|
+
if (hashedPasswordBytes.error) return chunkVCBHSRCS_cjs.$err(hashedPasswordBytes.error);
|
|
349
|
+
if (hashedPasswordBytes.result.byteLength !== keyLength) return chunkVCBHSRCS_cjs.$ok(false);
|
|
350
350
|
try {
|
|
351
351
|
const derived = nodeCrypto3__default.default.pbkdf2Sync(
|
|
352
352
|
password.normalize("NFKC"),
|
|
@@ -362,14 +362,14 @@ function $verifyPassword(password, hashedPassword, salt, options) {
|
|
|
362
362
|
expected.copy(right);
|
|
363
363
|
try {
|
|
364
364
|
const matches = nodeCrypto3__default.default.timingSafeEqual(left, right);
|
|
365
|
-
return
|
|
365
|
+
return chunkVCBHSRCS_cjs.$ok(matches);
|
|
366
366
|
} finally {
|
|
367
367
|
left.fill(0);
|
|
368
368
|
right.fill(0);
|
|
369
369
|
derived.fill(0);
|
|
370
370
|
}
|
|
371
371
|
} catch (error) {
|
|
372
|
-
return
|
|
372
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node verifyPassword: Verification failed", description: chunkVCBHSRCS_cjs.$fmtError(error) });
|
|
373
373
|
} finally {
|
|
374
374
|
saltBytes.result.fill(0);
|
|
375
375
|
hashedPasswordBytes.result.fill(0);
|
|
@@ -382,9 +382,9 @@ function isNodeSecretKey(x) {
|
|
|
382
382
|
}
|
|
383
383
|
function tryGenerateUuid() {
|
|
384
384
|
try {
|
|
385
|
-
return
|
|
385
|
+
return chunkVCBHSRCS_cjs.$ok(nodeCrypto3__default.default.randomUUID());
|
|
386
386
|
} catch (error) {
|
|
387
|
-
return
|
|
387
|
+
return chunkVCBHSRCS_cjs.$err({ message: "node generateUuid: Failed to generate UUID", description: chunkVCBHSRCS_cjs.$fmtError(error) });
|
|
388
388
|
}
|
|
389
389
|
}
|
|
390
390
|
function generateUuid() {
|
|
@@ -395,7 +395,7 @@ function tryCreateSecretKey(secret, options = {}) {
|
|
|
395
395
|
}
|
|
396
396
|
function createSecretKey(secret, options = {}) {
|
|
397
397
|
const { result, error } = $createSecretKey(secret, options);
|
|
398
|
-
if (error) throw new Error(
|
|
398
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
399
399
|
return result;
|
|
400
400
|
}
|
|
401
401
|
function tryEncrypt(data, secretKey, options = {}) {
|
|
@@ -403,7 +403,7 @@ function tryEncrypt(data, secretKey, options = {}) {
|
|
|
403
403
|
}
|
|
404
404
|
function encrypt(data, secretKey, options = {}) {
|
|
405
405
|
const { result, error } = $encrypt(data, secretKey, options);
|
|
406
|
-
if (error) throw new Error(
|
|
406
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
407
407
|
return result;
|
|
408
408
|
}
|
|
409
409
|
function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
@@ -411,7 +411,7 @@ function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
|
411
411
|
}
|
|
412
412
|
function decrypt(encrypted, secretKey, options = {}) {
|
|
413
413
|
const { result, error } = $decrypt(encrypted, secretKey, options);
|
|
414
|
-
if (error) throw new Error(
|
|
414
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
415
415
|
return result;
|
|
416
416
|
}
|
|
417
417
|
function tryEncryptObj(obj, secretKey, options = {}) {
|
|
@@ -419,7 +419,7 @@ function tryEncryptObj(obj, secretKey, options = {}) {
|
|
|
419
419
|
}
|
|
420
420
|
function encryptObj(obj, secretKey, options = {}) {
|
|
421
421
|
const { result, error } = $encryptObj(obj, secretKey, options);
|
|
422
|
-
if (error) throw new Error(
|
|
422
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
423
423
|
return result;
|
|
424
424
|
}
|
|
425
425
|
function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
@@ -427,7 +427,7 @@ function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
|
427
427
|
}
|
|
428
428
|
function decryptObj(encrypted, secretKey, options = {}) {
|
|
429
429
|
const { result, error } = $decryptObj(encrypted, secretKey, options);
|
|
430
|
-
if (error) throw new Error(
|
|
430
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
431
431
|
return result;
|
|
432
432
|
}
|
|
433
433
|
function tryHash(data, options = {}) {
|
|
@@ -435,7 +435,7 @@ function tryHash(data, options = {}) {
|
|
|
435
435
|
}
|
|
436
436
|
function hash(data, options = {}) {
|
|
437
437
|
const { result, error } = $hash(data, options);
|
|
438
|
-
if (error) throw new Error(
|
|
438
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
439
439
|
return result;
|
|
440
440
|
}
|
|
441
441
|
function tryHashPassword(password, options = {}) {
|
|
@@ -443,7 +443,7 @@ function tryHashPassword(password, options = {}) {
|
|
|
443
443
|
}
|
|
444
444
|
function hashPassword(password, options = {}) {
|
|
445
445
|
const { result, salt, error } = $hashPassword(password, options);
|
|
446
|
-
if (error) throw new Error(
|
|
446
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
447
447
|
return { result, salt };
|
|
448
448
|
}
|
|
449
449
|
function tryVerifyPassword(password, hashedPassword, salt, options = {}) {
|
|
@@ -451,7 +451,7 @@ function tryVerifyPassword(password, hashedPassword, salt, options = {}) {
|
|
|
451
451
|
}
|
|
452
452
|
function verifyPassword(password, hashedPassword, salt, options = {}) {
|
|
453
453
|
const { result, error } = $verifyPassword(password, hashedPassword, salt, options);
|
|
454
|
-
if (error) throw new Error(
|
|
454
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
455
455
|
return result;
|
|
456
456
|
}
|
|
457
457
|
function tryConvertStrToBytes(data, inputEncoding = "utf8") {
|
|
@@ -459,7 +459,7 @@ function tryConvertStrToBytes(data, inputEncoding = "utf8") {
|
|
|
459
459
|
}
|
|
460
460
|
function convertStrToBytes(data, inputEncoding = "utf8") {
|
|
461
461
|
const { result, error } = $convertStrToBytes(data, inputEncoding);
|
|
462
|
-
if (error) throw new Error(
|
|
462
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
463
463
|
return result;
|
|
464
464
|
}
|
|
465
465
|
function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
@@ -467,7 +467,7 @@ function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
|
467
467
|
}
|
|
468
468
|
function convertBytesToStr(data, outputEncoding = "utf8") {
|
|
469
469
|
const { result, error } = $convertBytesToStr(data, outputEncoding);
|
|
470
|
-
if (error) throw new Error(
|
|
470
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
471
471
|
return result;
|
|
472
472
|
}
|
|
473
473
|
function tryConvertEncoding(data, from, to) {
|
|
@@ -475,7 +475,7 @@ function tryConvertEncoding(data, from, to) {
|
|
|
475
475
|
}
|
|
476
476
|
function convertEncoding(data, from, to) {
|
|
477
477
|
const { result, error } = $convertEncoding(data, from, to);
|
|
478
|
-
if (error) throw new Error(
|
|
478
|
+
if (error) throw new Error(chunkVCBHSRCS_cjs.$fmtResultErr(error));
|
|
479
479
|
return result;
|
|
480
480
|
}
|
|
481
481
|
|
|
@@ -505,5 +505,5 @@ exports.tryHash = tryHash;
|
|
|
505
505
|
exports.tryHashPassword = tryHashPassword;
|
|
506
506
|
exports.tryVerifyPassword = tryVerifyPassword;
|
|
507
507
|
exports.verifyPassword = verifyPassword;
|
|
508
|
-
//# sourceMappingURL=chunk-
|
|
509
|
-
//# sourceMappingURL=chunk-
|
|
508
|
+
//# sourceMappingURL=chunk-3A4RTUKO.cjs.map
|
|
509
|
+
//# sourceMappingURL=chunk-3A4RTUKO.cjs.map
|