cipher-kit 2.1.1 → 2.1.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/dist/{chunk-LTVOIZP5.cjs → chunk-66WIL32E.cjs} +145 -150
- package/dist/chunk-66WIL32E.cjs.map +1 -0
- package/dist/{chunk-X4CS7UXE.cjs → chunk-HTRGOBZF.cjs} +10 -2
- package/dist/chunk-HTRGOBZF.cjs.map +1 -0
- package/dist/{chunk-FSEA3UXJ.js → chunk-JLFVTZY4.js} +4 -8
- package/dist/chunk-JLFVTZY4.js.map +1 -0
- package/dist/{chunk-6C4NIWQ4.js → chunk-LU7QOSQH.js} +9 -3
- package/dist/chunk-LU7QOSQH.js.map +1 -0
- package/dist/{chunk-PWTFVMW6.js → chunk-S6SNCTU6.js} +4 -8
- package/dist/chunk-S6SNCTU6.js.map +1 -0
- package/dist/{chunk-56PVVFVM.cjs → chunk-ZNM5M6RD.cjs} +140 -145
- package/dist/chunk-ZNM5M6RD.cjs.map +1 -0
- package/dist/{export-DUgIcobC.d.ts → export-BaM_OTFk.d.ts} +13 -31
- package/dist/{export-C4DbO5zM.d.ts → export-CCTGAosO.d.ts} +13 -31
- package/dist/{export-CpZ7s25O.d.cts → export-FYHgb-8E.d.cts} +13 -31
- package/dist/{export-DO9n7Np-.d.cts → export-KFT0YyMg.d.cts} +13 -31
- package/dist/index.cjs +19 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +35 -35
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{validate-cJEdGlj1.d.cts → validate-lkJAHCeJ.d.cts} +39 -11
- package/dist/{validate-cJEdGlj1.d.ts → validate-lkJAHCeJ.d.ts} +39 -11
- package/dist/web-api.cjs +35 -35
- 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 +1 -1
- package/dist/chunk-56PVVFVM.cjs.map +0 -1
- package/dist/chunk-6C4NIWQ4.js.map +0 -1
- package/dist/chunk-FSEA3UXJ.js.map +0 -1
- package/dist/chunk-LTVOIZP5.cjs.map +0 -1
- package/dist/chunk-PWTFVMW6.js.map +0 -1
- package/dist/chunk-X4CS7UXE.cjs.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkHTRGOBZF_cjs = require('./chunk-HTRGOBZF.cjs');
|
|
4
4
|
var buffer = require('buffer');
|
|
5
5
|
var nodeCrypto = require('crypto');
|
|
6
6
|
|
|
@@ -10,7 +10,7 @@ var nodeCrypto__default = /*#__PURE__*/_interopDefault(nodeCrypto);
|
|
|
10
10
|
|
|
11
11
|
// src/node/kit.ts
|
|
12
12
|
var kit_exports = {};
|
|
13
|
-
|
|
13
|
+
chunkHTRGOBZF_cjs.__export(kit_exports, {
|
|
14
14
|
convertBytesToStr: () => convertBytesToStr,
|
|
15
15
|
convertEncoding: () => convertEncoding,
|
|
16
16
|
convertStrToBytes: () => convertStrToBytes,
|
|
@@ -22,7 +22,6 @@ chunkX4CS7UXE_cjs.__export(kit_exports, {
|
|
|
22
22
|
generateUuid: () => generateUuid,
|
|
23
23
|
hash: () => hash,
|
|
24
24
|
hashPassword: () => hashPassword,
|
|
25
|
-
isNodeSecretKey: () => isNodeSecretKey,
|
|
26
25
|
tryConvertBytesToStr: () => tryConvertBytesToStr,
|
|
27
26
|
tryConvertEncoding: () => tryConvertEncoding,
|
|
28
27
|
tryConvertStrToBytes: () => tryConvertStrToBytes,
|
|
@@ -37,112 +36,112 @@ chunkX4CS7UXE_cjs.__export(kit_exports, {
|
|
|
37
36
|
verifyPassword: () => verifyPassword
|
|
38
37
|
});
|
|
39
38
|
function $convertStrToBytes(data, inputEncoding = "utf8") {
|
|
40
|
-
if (!
|
|
41
|
-
return
|
|
39
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
40
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
42
41
|
msg: "Crypto NodeJS API - String to Bytes: Empty data",
|
|
43
42
|
desc: "Data must be a non-empty string"
|
|
44
43
|
});
|
|
45
44
|
}
|
|
46
|
-
if (!
|
|
47
|
-
return
|
|
45
|
+
if (!chunkHTRGOBZF_cjs.ENCODING.includes(inputEncoding)) {
|
|
46
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
48
47
|
msg: `Crypto NodeJS API - String to Bytes: Unsupported encoding: ${inputEncoding}`,
|
|
49
48
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
50
49
|
});
|
|
51
50
|
}
|
|
52
51
|
try {
|
|
53
|
-
return
|
|
52
|
+
return chunkHTRGOBZF_cjs.$ok({ result: buffer.Buffer.from(data, inputEncoding) });
|
|
54
53
|
} catch (error) {
|
|
55
|
-
return
|
|
54
|
+
return chunkHTRGOBZF_cjs.$err({ msg: "Crypto NodeJS API - String to Bytes: Failed to convert data", desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
57
|
function $convertBytesToStr(data, outputEncoding = "utf8") {
|
|
59
58
|
if (!(data instanceof buffer.Buffer)) {
|
|
60
|
-
return
|
|
59
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
61
60
|
msg: "Crypto NodeJS API - Bytes to String: Invalid data type",
|
|
62
61
|
desc: "Data must be a Buffer"
|
|
63
62
|
});
|
|
64
63
|
}
|
|
65
|
-
if (!
|
|
66
|
-
return
|
|
64
|
+
if (!chunkHTRGOBZF_cjs.ENCODING.includes(outputEncoding)) {
|
|
65
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
67
66
|
msg: `Crypto NodeJS API - Bytes to String: Unsupported encoding: ${outputEncoding}`,
|
|
68
67
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
69
68
|
});
|
|
70
69
|
}
|
|
71
70
|
try {
|
|
72
|
-
return
|
|
71
|
+
return chunkHTRGOBZF_cjs.$ok(buffer.Buffer.from(data).toString(outputEncoding));
|
|
73
72
|
} catch (error) {
|
|
74
|
-
return
|
|
73
|
+
return chunkHTRGOBZF_cjs.$err({ msg: "Crypto NodeJS API - Bytes to String: Failed to convert data", desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
75
74
|
}
|
|
76
75
|
}
|
|
77
76
|
function $convertEncoding(data, from, to) {
|
|
78
|
-
if (!
|
|
79
|
-
return
|
|
77
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
78
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
80
79
|
msg: "Crypto NodeJS API - Convert Format: Empty data",
|
|
81
80
|
desc: "Data must be a non-empty string"
|
|
82
81
|
});
|
|
83
82
|
}
|
|
84
|
-
if (!
|
|
85
|
-
return
|
|
83
|
+
if (!chunkHTRGOBZF_cjs.ENCODING.includes(from) || !chunkHTRGOBZF_cjs.ENCODING.includes(to)) {
|
|
84
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
86
85
|
msg: `Crypto NodeJS API - Convert Format: Unsupported encoding: from ${from} to ${to}`,
|
|
87
86
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
88
87
|
});
|
|
89
88
|
}
|
|
90
89
|
const bytes = $convertStrToBytes(data, from);
|
|
91
|
-
if (bytes.error) return
|
|
90
|
+
if (bytes.error) return chunkHTRGOBZF_cjs.$err({ msg: bytes.error.message, desc: bytes.error.description });
|
|
92
91
|
const str = $convertBytesToStr(bytes.result, to);
|
|
93
|
-
if (str.error) return
|
|
94
|
-
return
|
|
92
|
+
if (str.error) return chunkHTRGOBZF_cjs.$err({ msg: str.error.message, desc: str.error.description });
|
|
93
|
+
return chunkHTRGOBZF_cjs.$ok({ result: str.result });
|
|
95
94
|
}
|
|
96
95
|
function $generateUuid() {
|
|
97
96
|
try {
|
|
98
|
-
return
|
|
97
|
+
return chunkHTRGOBZF_cjs.$ok(nodeCrypto__default.default.randomUUID());
|
|
99
98
|
} catch (error) {
|
|
100
|
-
return
|
|
99
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("node", "UUID Generation")}: Failed to generate UUID`, desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
101
100
|
}
|
|
102
101
|
}
|
|
103
102
|
function $createSecretKey(secret, options) {
|
|
104
|
-
if (!
|
|
105
|
-
return
|
|
106
|
-
msg: `${
|
|
103
|
+
if (!chunkHTRGOBZF_cjs.$isStr(secret, 8)) {
|
|
104
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
105
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Key Generation")}: Empty Secret`,
|
|
107
106
|
desc: "Secret must be a non-empty string with at least 8 characters"
|
|
108
107
|
});
|
|
109
108
|
}
|
|
110
|
-
if (!
|
|
111
|
-
return
|
|
112
|
-
msg: `${
|
|
109
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
110
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
111
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Key Generation")}: Invalid options`,
|
|
113
112
|
desc: "Options must be an object"
|
|
114
113
|
});
|
|
115
114
|
}
|
|
116
115
|
const algorithm = options.algorithm ?? "aes256gcm";
|
|
117
|
-
if (!(algorithm in
|
|
118
|
-
return
|
|
119
|
-
msg: `${
|
|
120
|
-
desc: `Supported algorithms are: ${Object.keys(
|
|
116
|
+
if (!(algorithm in chunkHTRGOBZF_cjs.ENCRYPTION_ALGORITHMS)) {
|
|
117
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
118
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Key Generation")}: Unsupported algorithm: ${algorithm}`,
|
|
119
|
+
desc: `Supported algorithms are: ${Object.keys(chunkHTRGOBZF_cjs.ENCRYPTION_ALGORITHMS).join(", ")}`
|
|
121
120
|
});
|
|
122
121
|
}
|
|
123
122
|
const digest = options.digest ?? "sha256";
|
|
124
|
-
if (!(digest in
|
|
125
|
-
return
|
|
126
|
-
msg: `${
|
|
127
|
-
desc: `Supported digests are: ${Object.keys(
|
|
123
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) {
|
|
124
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
125
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Key Generation")}: Unsupported digest: ${digest}`,
|
|
126
|
+
desc: `Supported digests are: ${Object.keys(chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
128
127
|
});
|
|
129
128
|
}
|
|
130
129
|
const salt = options.salt ?? "cipher-kit-salt";
|
|
131
|
-
if (!
|
|
132
|
-
return
|
|
133
|
-
msg: `${
|
|
130
|
+
if (!chunkHTRGOBZF_cjs.$isStr(salt, 8)) {
|
|
131
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
132
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Key Generation")}: Weak salt`,
|
|
134
133
|
desc: "Salt must be a non-empty string with at least 8 characters"
|
|
135
134
|
});
|
|
136
135
|
}
|
|
137
136
|
const info = options.info ?? "cipher-kit";
|
|
138
|
-
if (!
|
|
139
|
-
return
|
|
140
|
-
msg: `${
|
|
137
|
+
if (!chunkHTRGOBZF_cjs.$isStr(info)) {
|
|
138
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
139
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Key Generation")}: Invalid info`,
|
|
141
140
|
desc: "Info must be a non-empty string"
|
|
142
141
|
});
|
|
143
142
|
}
|
|
144
|
-
const encryptAlgo =
|
|
145
|
-
const digestAlgo =
|
|
143
|
+
const encryptAlgo = chunkHTRGOBZF_cjs.ENCRYPTION_ALGORITHMS[algorithm];
|
|
144
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
146
145
|
try {
|
|
147
146
|
const derivedKey = nodeCrypto__default.default.hkdfSync(
|
|
148
147
|
digestAlgo.node,
|
|
@@ -158,40 +157,40 @@ function $createSecretKey(secret, options) {
|
|
|
158
157
|
algorithm,
|
|
159
158
|
key
|
|
160
159
|
});
|
|
161
|
-
return
|
|
160
|
+
return chunkHTRGOBZF_cjs.$ok({ result: secretKey });
|
|
162
161
|
} catch (error) {
|
|
163
|
-
return
|
|
162
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("node", "Key Generation")}: Failed to create secret key`, desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
164
163
|
}
|
|
165
164
|
}
|
|
166
165
|
function $encrypt(data, secretKey, options) {
|
|
167
|
-
if (!
|
|
168
|
-
return
|
|
169
|
-
msg: `${
|
|
166
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
167
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
168
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Encryption")}: Empty data for encryption`,
|
|
170
169
|
desc: "Data must be a non-empty string"
|
|
171
170
|
});
|
|
172
171
|
}
|
|
173
|
-
if (!
|
|
174
|
-
return
|
|
175
|
-
msg: `${
|
|
172
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
173
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
174
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Encryption")}: Invalid options`,
|
|
176
175
|
desc: "Options must be an object"
|
|
177
176
|
});
|
|
178
177
|
}
|
|
179
178
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
180
|
-
if (!
|
|
181
|
-
return
|
|
182
|
-
msg: `${
|
|
179
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
180
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
181
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Encryption")}: Unsupported output encoding: ${outputEncoding}`,
|
|
183
182
|
desc: "Use base64, base64url, or hex"
|
|
184
183
|
});
|
|
185
184
|
}
|
|
186
|
-
const injectedKey =
|
|
185
|
+
const injectedKey = chunkHTRGOBZF_cjs.$isSecretKey(secretKey, "node");
|
|
187
186
|
if (!injectedKey) {
|
|
188
|
-
return
|
|
189
|
-
msg: `${
|
|
187
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
188
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Encryption")}: Invalid Secret Key`,
|
|
190
189
|
desc: "Expected a Node SecretKey"
|
|
191
190
|
});
|
|
192
191
|
}
|
|
193
192
|
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
194
|
-
if (error) return
|
|
193
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
195
194
|
try {
|
|
196
195
|
const iv = nodeCrypto__default.default.randomBytes(injectedKey.injected.ivLength);
|
|
197
196
|
const cipher = nodeCrypto__default.default.createCipheriv(injectedKey.injected.node, injectedKey.key, iv);
|
|
@@ -201,46 +200,46 @@ function $encrypt(data, secretKey, options) {
|
|
|
201
200
|
const cipherStr = $convertBytesToStr(encrypted, outputEncoding);
|
|
202
201
|
const tagStr = $convertBytesToStr(tag, outputEncoding);
|
|
203
202
|
if (ivStr.error || cipherStr.error || tagStr.error) {
|
|
204
|
-
return
|
|
203
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
205
204
|
msg: "Crypto NodeJS API - Encryption: Failed to convert IV or encrypted data or tag",
|
|
206
|
-
desc: `Conversion error: ${
|
|
205
|
+
desc: `Conversion error: ${chunkHTRGOBZF_cjs.$fmtResultErr(ivStr.error || cipherStr.error || tagStr.error)}`
|
|
207
206
|
});
|
|
208
207
|
}
|
|
209
|
-
return
|
|
208
|
+
return chunkHTRGOBZF_cjs.$ok(`${ivStr.result}.${cipherStr.result}.${tagStr.result}.`);
|
|
210
209
|
} catch (error2) {
|
|
211
|
-
return
|
|
210
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("node", "Encryption")}: Failed to encrypt data`, desc: chunkHTRGOBZF_cjs.$fmtError(error2) });
|
|
212
211
|
}
|
|
213
212
|
}
|
|
214
213
|
function $decrypt(encrypted, secretKey, options) {
|
|
215
|
-
if (!
|
|
216
|
-
return
|
|
217
|
-
msg: `${
|
|
214
|
+
if (!chunkHTRGOBZF_cjs.matchEncryptedPattern(encrypted, "node")) {
|
|
215
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
216
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Decryption")}: Invalid encrypted data format`,
|
|
218
217
|
desc: 'Encrypted data must be in the format "iv.cipher.tag."'
|
|
219
218
|
});
|
|
220
219
|
}
|
|
221
|
-
if (!
|
|
222
|
-
return
|
|
223
|
-
msg: `${
|
|
220
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
221
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
222
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Decryption")}: Invalid options`,
|
|
224
223
|
desc: "Options must be an object"
|
|
225
224
|
});
|
|
226
225
|
}
|
|
227
226
|
const inputEncoding = options.inputEncoding ?? "base64url";
|
|
228
|
-
if (!
|
|
229
|
-
return
|
|
230
|
-
msg: `${
|
|
227
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(inputEncoding)) {
|
|
228
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
229
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Decryption")}: Unsupported input encoding: ${inputEncoding}`,
|
|
231
230
|
desc: "Use base64, base64url, or hex"
|
|
232
231
|
});
|
|
233
232
|
}
|
|
234
233
|
const [iv, cipher, tag] = encrypted.split(".", 4);
|
|
235
|
-
if (!
|
|
236
|
-
return
|
|
237
|
-
msg: `${
|
|
234
|
+
if (!chunkHTRGOBZF_cjs.$isStr(iv) || !chunkHTRGOBZF_cjs.$isStr(cipher) || !chunkHTRGOBZF_cjs.$isStr(tag)) {
|
|
235
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
236
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Decryption")}: Invalid encrypted data`,
|
|
238
237
|
desc: "Encrypted data must contain valid IV, encrypted data, and tag components"
|
|
239
238
|
});
|
|
240
239
|
}
|
|
241
|
-
const injectedKey =
|
|
240
|
+
const injectedKey = chunkHTRGOBZF_cjs.$isSecretKey(secretKey, "node");
|
|
242
241
|
if (!injectedKey) {
|
|
243
|
-
return
|
|
242
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
244
243
|
msg: "Crypto NodeJS API - Decryption: Invalid Secret Key",
|
|
245
244
|
desc: "Expected a Node SecretKey"
|
|
246
245
|
});
|
|
@@ -249,9 +248,9 @@ function $decrypt(encrypted, secretKey, options) {
|
|
|
249
248
|
const cipherBytes = $convertStrToBytes(cipher, inputEncoding);
|
|
250
249
|
const tagBytes = $convertStrToBytes(tag, inputEncoding);
|
|
251
250
|
if (ivBytes.error || cipherBytes.error || tagBytes.error) {
|
|
252
|
-
return
|
|
253
|
-
msg: `${
|
|
254
|
-
desc: `Conversion error: ${
|
|
251
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
252
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Decryption")}: Failed to convert IV or encrypted data or tag`,
|
|
253
|
+
desc: `Conversion error: ${chunkHTRGOBZF_cjs.$fmtResultErr(ivBytes.error || cipherBytes.error || tagBytes.error)}`
|
|
255
254
|
});
|
|
256
255
|
}
|
|
257
256
|
try {
|
|
@@ -260,122 +259,122 @@ function $decrypt(encrypted, secretKey, options) {
|
|
|
260
259
|
const decrypted = buffer.Buffer.concat([decipher.update(cipherBytes.result), decipher.final()]);
|
|
261
260
|
return $convertBytesToStr(decrypted, "utf8");
|
|
262
261
|
} catch (error) {
|
|
263
|
-
return
|
|
262
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("node", "Decryption")}: Failed to decrypt data`, desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
264
263
|
}
|
|
265
264
|
}
|
|
266
265
|
function $encryptObj(data, secretKey, options) {
|
|
267
|
-
const { result, error } =
|
|
268
|
-
if (error) return
|
|
266
|
+
const { result, error } = chunkHTRGOBZF_cjs.$stringifyObj(data);
|
|
267
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
269
268
|
return $encrypt(result, secretKey, options);
|
|
270
269
|
}
|
|
271
270
|
function $decryptObj(encrypted, secretKey, options) {
|
|
272
271
|
const { result, error } = $decrypt(encrypted, secretKey, options);
|
|
273
|
-
if (error) return
|
|
274
|
-
return
|
|
272
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
273
|
+
return chunkHTRGOBZF_cjs.$parseToObj(result);
|
|
275
274
|
}
|
|
276
275
|
function $hash(data, options = {}) {
|
|
277
|
-
if (!
|
|
278
|
-
return
|
|
279
|
-
msg: `${
|
|
276
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
277
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
278
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Hashing")}: Empty data for hashing`,
|
|
280
279
|
desc: "Data must be a non-empty string"
|
|
281
280
|
});
|
|
282
281
|
}
|
|
283
|
-
if (!
|
|
284
|
-
return
|
|
285
|
-
msg: `${
|
|
282
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
283
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
284
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Hashing")}: Invalid options`,
|
|
286
285
|
desc: "Options must be an object"
|
|
287
286
|
});
|
|
288
287
|
}
|
|
289
288
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
290
|
-
if (!
|
|
291
|
-
return
|
|
292
|
-
msg: `${
|
|
289
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
290
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
291
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Hashing")}: Unsupported output encoding: ${outputEncoding}`,
|
|
293
292
|
desc: "Use base64, base64url, or hex"
|
|
294
293
|
});
|
|
295
294
|
}
|
|
296
295
|
const digest = options.digest ?? "sha256";
|
|
297
|
-
if (!(digest in
|
|
298
|
-
return
|
|
299
|
-
msg: `${
|
|
300
|
-
desc: `Supported digests are: ${Object.keys(
|
|
296
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) {
|
|
297
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
298
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Hashing")}: Unsupported digest: ${digest}`,
|
|
299
|
+
desc: `Supported digests are: ${Object.keys(chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
301
300
|
});
|
|
302
301
|
}
|
|
303
|
-
const digestAlgo =
|
|
302
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
304
303
|
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
305
|
-
if (error) return
|
|
304
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
306
305
|
try {
|
|
307
306
|
const hashed = nodeCrypto__default.default.createHash(digestAlgo.node).update(result).digest();
|
|
308
307
|
return $convertBytesToStr(hashed, outputEncoding);
|
|
309
308
|
} catch (error2) {
|
|
310
|
-
return
|
|
309
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("node", "Hashing")}: Failed to hash data with Crypto NodeJS`, desc: chunkHTRGOBZF_cjs.$fmtError(error2) });
|
|
311
310
|
}
|
|
312
311
|
}
|
|
313
312
|
function $hashPassword(password, options) {
|
|
314
|
-
if (!
|
|
315
|
-
return
|
|
316
|
-
msg: `${
|
|
313
|
+
if (!chunkHTRGOBZF_cjs.$isStr(password)) {
|
|
314
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
315
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Empty password for hashing`,
|
|
317
316
|
desc: "Password must be a non-empty string"
|
|
318
317
|
});
|
|
319
318
|
}
|
|
320
|
-
if (!
|
|
321
|
-
return
|
|
322
|
-
msg: `${
|
|
319
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
320
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
321
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Invalid options`,
|
|
323
322
|
desc: "Options must be an object"
|
|
324
323
|
});
|
|
325
324
|
}
|
|
326
325
|
const digest = options.digest ?? "sha512";
|
|
327
|
-
if (!(digest in
|
|
328
|
-
return
|
|
329
|
-
msg: `${
|
|
330
|
-
desc: `Supported digests are: ${Object.keys(
|
|
326
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) {
|
|
327
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
328
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Unsupported digest: ${digest}`,
|
|
329
|
+
desc: `Supported digests are: ${Object.keys(chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
331
330
|
});
|
|
332
331
|
}
|
|
333
|
-
const digestAlgo =
|
|
332
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
334
333
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
335
|
-
if (!
|
|
336
|
-
return
|
|
337
|
-
msg: `${
|
|
334
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
335
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
336
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Unsupported encoding: ${outputEncoding}`,
|
|
338
337
|
desc: "Use base64, base64url, or hex"
|
|
339
338
|
});
|
|
340
339
|
}
|
|
341
340
|
const saltLength = options.saltLength ?? 16;
|
|
342
341
|
if (typeof saltLength !== "number" || saltLength < 8) {
|
|
343
|
-
return
|
|
344
|
-
msg: `${
|
|
342
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
343
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Weak salt length`,
|
|
345
344
|
desc: "Salt length must be a number and at least 8 bytes (recommended 16 or more)"
|
|
346
345
|
});
|
|
347
346
|
}
|
|
348
347
|
const iterations = options.iterations ?? 32e4;
|
|
349
348
|
if (typeof iterations !== "number" || iterations < 1e3) {
|
|
350
|
-
return
|
|
351
|
-
msg: `${
|
|
349
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
350
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Weak iterations count`,
|
|
352
351
|
desc: "Iterations must be a number and at least 1000 (recommended 320,000 or more)"
|
|
353
352
|
});
|
|
354
353
|
}
|
|
355
354
|
const keyLength = options.keyLength ?? 64;
|
|
356
355
|
if (typeof keyLength !== "number" || keyLength < 16) {
|
|
357
|
-
return
|
|
358
|
-
msg: `${
|
|
356
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
357
|
+
msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Invalid key length`,
|
|
359
358
|
desc: "Key length must be a number and at least 16 bytes (recommended 64 or more)"
|
|
360
359
|
});
|
|
361
360
|
}
|
|
362
361
|
try {
|
|
363
362
|
const salt = nodeCrypto__default.default.randomBytes(saltLength);
|
|
364
363
|
const hash2 = nodeCrypto__default.default.pbkdf2Sync(password.normalize("NFKC"), salt, iterations, keyLength, digestAlgo.node);
|
|
365
|
-
return
|
|
364
|
+
return chunkHTRGOBZF_cjs.$ok({ result: hash2.toString(outputEncoding), salt: salt.toString(outputEncoding) });
|
|
366
365
|
} catch (error) {
|
|
367
|
-
return
|
|
366
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("node", "Password Hashing")}: Failed to hash password`, desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
368
367
|
}
|
|
369
368
|
}
|
|
370
369
|
function $verifyPassword(password, hashedPassword, salt, options) {
|
|
371
|
-
if (!
|
|
370
|
+
if (!chunkHTRGOBZF_cjs.$isStr(password) || !chunkHTRGOBZF_cjs.$isStr(hashedPassword) || !chunkHTRGOBZF_cjs.$isStr(salt) || !chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
372
371
|
return false;
|
|
373
372
|
}
|
|
374
373
|
const digest = options.digest ?? "sha512";
|
|
375
|
-
if (!(digest in
|
|
376
|
-
const digestAlgo =
|
|
374
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) return false;
|
|
375
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
377
376
|
const inputEncoding = options.inputEncoding ?? "base64url";
|
|
378
|
-
if (!
|
|
377
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(inputEncoding)) return false;
|
|
379
378
|
const iterations = options.iterations ?? 32e4;
|
|
380
379
|
if (typeof iterations !== "number" || iterations < 1e3) return false;
|
|
381
380
|
const keyLength = options.keyLength ?? 64;
|
|
@@ -395,15 +394,12 @@ function $verifyPassword(password, hashedPassword, salt, options) {
|
|
|
395
394
|
}
|
|
396
395
|
|
|
397
396
|
// src/node/kit.ts
|
|
398
|
-
function isNodeSecretKey(x) {
|
|
399
|
-
return chunkX4CS7UXE_cjs.$isSecretKey(x, "node") !== null;
|
|
400
|
-
}
|
|
401
397
|
function tryGenerateUuid() {
|
|
402
398
|
return $generateUuid();
|
|
403
399
|
}
|
|
404
400
|
function generateUuid() {
|
|
405
401
|
const { result, error } = $generateUuid();
|
|
406
|
-
if (error) throw new Error(
|
|
402
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
407
403
|
return result;
|
|
408
404
|
}
|
|
409
405
|
function tryCreateSecretKey(secret, options = {}) {
|
|
@@ -411,7 +407,7 @@ function tryCreateSecretKey(secret, options = {}) {
|
|
|
411
407
|
}
|
|
412
408
|
function createSecretKey(secret, options = {}) {
|
|
413
409
|
const { result, error } = $createSecretKey(secret, options);
|
|
414
|
-
if (error) throw new Error(
|
|
410
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
415
411
|
return result;
|
|
416
412
|
}
|
|
417
413
|
function tryEncrypt(data, secretKey, options = {}) {
|
|
@@ -419,7 +415,7 @@ function tryEncrypt(data, secretKey, options = {}) {
|
|
|
419
415
|
}
|
|
420
416
|
function encrypt(data, secretKey, options = {}) {
|
|
421
417
|
const { result, error } = $encrypt(data, secretKey, options);
|
|
422
|
-
if (error) throw new Error(
|
|
418
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
423
419
|
return result;
|
|
424
420
|
}
|
|
425
421
|
function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
@@ -427,7 +423,7 @@ function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
|
427
423
|
}
|
|
428
424
|
function decrypt(encrypted, secretKey, options = {}) {
|
|
429
425
|
const { result, error } = $decrypt(encrypted, secretKey, options);
|
|
430
|
-
if (error) throw new Error(
|
|
426
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
431
427
|
return result;
|
|
432
428
|
}
|
|
433
429
|
function tryEncryptObj(obj, secretKey, options = {}) {
|
|
@@ -435,7 +431,7 @@ function tryEncryptObj(obj, secretKey, options = {}) {
|
|
|
435
431
|
}
|
|
436
432
|
function encryptObj(obj, secretKey, options = {}) {
|
|
437
433
|
const { result, error } = $encryptObj(obj, secretKey, options);
|
|
438
|
-
if (error) throw new Error(
|
|
434
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
439
435
|
return result;
|
|
440
436
|
}
|
|
441
437
|
function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
@@ -443,7 +439,7 @@ function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
|
443
439
|
}
|
|
444
440
|
function decryptObj(encrypted, secretKey, options = {}) {
|
|
445
441
|
const { result, error } = $decryptObj(encrypted, secretKey, options);
|
|
446
|
-
if (error) throw new Error(
|
|
442
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
447
443
|
return result;
|
|
448
444
|
}
|
|
449
445
|
function tryHash(data, options = {}) {
|
|
@@ -451,7 +447,7 @@ function tryHash(data, options = {}) {
|
|
|
451
447
|
}
|
|
452
448
|
function hash(data, options = {}) {
|
|
453
449
|
const { result, error } = $hash(data, options);
|
|
454
|
-
if (error) throw new Error(
|
|
450
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
455
451
|
return result;
|
|
456
452
|
}
|
|
457
453
|
function tryHashPassword(password, options = {}) {
|
|
@@ -459,7 +455,7 @@ function tryHashPassword(password, options = {}) {
|
|
|
459
455
|
}
|
|
460
456
|
function hashPassword(password, options = {}) {
|
|
461
457
|
const { result, salt, error } = $hashPassword(password, options);
|
|
462
|
-
if (error) throw new Error(
|
|
458
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
463
459
|
return { result, salt };
|
|
464
460
|
}
|
|
465
461
|
function verifyPassword(password, hashedPassword, salt, options = {}) {
|
|
@@ -470,7 +466,7 @@ function tryConvertStrToBytes(data, inputEncoding = "utf8") {
|
|
|
470
466
|
}
|
|
471
467
|
function convertStrToBytes(data, inputEncoding = "utf8") {
|
|
472
468
|
const { result, error } = $convertStrToBytes(data, inputEncoding);
|
|
473
|
-
if (error) throw new Error(
|
|
469
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
474
470
|
return result;
|
|
475
471
|
}
|
|
476
472
|
function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
@@ -478,7 +474,7 @@ function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
|
478
474
|
}
|
|
479
475
|
function convertBytesToStr(data, outputEncoding = "utf8") {
|
|
480
476
|
const { result, error } = $convertBytesToStr(data, outputEncoding);
|
|
481
|
-
if (error) throw new Error(
|
|
477
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
482
478
|
return result;
|
|
483
479
|
}
|
|
484
480
|
function tryConvertEncoding(data, from, to) {
|
|
@@ -486,7 +482,7 @@ function tryConvertEncoding(data, from, to) {
|
|
|
486
482
|
}
|
|
487
483
|
function convertEncoding(data, from, to) {
|
|
488
484
|
const { result, error } = $convertEncoding(data, from, to);
|
|
489
|
-
if (error) throw new Error(
|
|
485
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
490
486
|
return result;
|
|
491
487
|
}
|
|
492
488
|
|
|
@@ -501,7 +497,6 @@ exports.encryptObj = encryptObj;
|
|
|
501
497
|
exports.generateUuid = generateUuid;
|
|
502
498
|
exports.hash = hash;
|
|
503
499
|
exports.hashPassword = hashPassword;
|
|
504
|
-
exports.isNodeSecretKey = isNodeSecretKey;
|
|
505
500
|
exports.kit_exports = kit_exports;
|
|
506
501
|
exports.tryConvertBytesToStr = tryConvertBytesToStr;
|
|
507
502
|
exports.tryConvertEncoding = tryConvertEncoding;
|
|
@@ -515,5 +510,5 @@ exports.tryGenerateUuid = tryGenerateUuid;
|
|
|
515
510
|
exports.tryHash = tryHash;
|
|
516
511
|
exports.tryHashPassword = tryHashPassword;
|
|
517
512
|
exports.verifyPassword = verifyPassword;
|
|
518
|
-
//# sourceMappingURL=chunk-
|
|
519
|
-
//# sourceMappingURL=chunk-
|
|
513
|
+
//# sourceMappingURL=chunk-ZNM5M6RD.cjs.map
|
|
514
|
+
//# sourceMappingURL=chunk-ZNM5M6RD.cjs.map
|