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