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