@gustavo-valsechi/utils 1.0.8 → 1.0.10
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/index.d.mts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +66 -0
- package/dist/index.mjs +66 -0
- package/dist/src/cryptor/index.d.mts +15 -0
- package/dist/src/cryptor/index.d.ts +15 -0
- package/dist/src/cryptor/index.js +88 -0
- package/dist/src/cryptor/index.mjs +67 -0
- package/package.json +6 -1
package/dist/index.d.mts
CHANGED
|
@@ -44,6 +44,19 @@ declare class Utils {
|
|
|
44
44
|
totalPage: number;
|
|
45
45
|
};
|
|
46
46
|
};
|
|
47
|
+
static cryptor: {
|
|
48
|
+
toBase64: (buffer: ArrayBuffer | Uint8Array) => string;
|
|
49
|
+
fromBase64: (base64: string) => Uint8Array;
|
|
50
|
+
deriveKey: (secret: string) => Promise<CryptoKey>;
|
|
51
|
+
encrypt: (data: any) => Promise<{
|
|
52
|
+
iv: string;
|
|
53
|
+
data: string;
|
|
54
|
+
}>;
|
|
55
|
+
decrypt: (payload: {
|
|
56
|
+
iv: string;
|
|
57
|
+
data: string;
|
|
58
|
+
}) => Promise<any>;
|
|
59
|
+
};
|
|
47
60
|
}
|
|
48
61
|
|
|
49
62
|
export { Utils as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -44,6 +44,19 @@ declare class Utils {
|
|
|
44
44
|
totalPage: number;
|
|
45
45
|
};
|
|
46
46
|
};
|
|
47
|
+
static cryptor: {
|
|
48
|
+
toBase64: (buffer: ArrayBuffer | Uint8Array) => string;
|
|
49
|
+
fromBase64: (base64: string) => Uint8Array;
|
|
50
|
+
deriveKey: (secret: string) => Promise<CryptoKey>;
|
|
51
|
+
encrypt: (data: any) => Promise<{
|
|
52
|
+
iv: string;
|
|
53
|
+
data: string;
|
|
54
|
+
}>;
|
|
55
|
+
decrypt: (payload: {
|
|
56
|
+
iv: string;
|
|
57
|
+
data: string;
|
|
58
|
+
}) => Promise<any>;
|
|
59
|
+
};
|
|
47
60
|
}
|
|
48
61
|
|
|
49
62
|
export { Utils as default };
|
package/dist/index.js
CHANGED
|
@@ -220,6 +220,71 @@ var Default = {
|
|
|
220
220
|
};
|
|
221
221
|
var default_default = Default;
|
|
222
222
|
|
|
223
|
+
// src/cryptor/index.ts
|
|
224
|
+
var IV_LENGTH = 12;
|
|
225
|
+
var SECRET = process.env.AUTH_SECRET || process.env.NEXT_PUBLIC_AUTH_SECRET;
|
|
226
|
+
var Cryptor = {
|
|
227
|
+
toBase64: (buffer) => {
|
|
228
|
+
const bytes = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
|
|
229
|
+
let binary = "";
|
|
230
|
+
bytes.forEach((b) => binary += String.fromCharCode(b));
|
|
231
|
+
return btoa(binary);
|
|
232
|
+
},
|
|
233
|
+
fromBase64: (base64) => {
|
|
234
|
+
const binary = atob(base64);
|
|
235
|
+
const bytes = new Uint8Array(binary.length);
|
|
236
|
+
for (let i = 0; i < binary.length; i++) {
|
|
237
|
+
bytes[i] = binary.charCodeAt(i);
|
|
238
|
+
}
|
|
239
|
+
return bytes;
|
|
240
|
+
},
|
|
241
|
+
deriveKey: async (secret) => {
|
|
242
|
+
const enc = new TextEncoder().encode(secret);
|
|
243
|
+
const hash = await crypto.subtle.digest("SHA-256", enc);
|
|
244
|
+
return crypto.subtle.importKey(
|
|
245
|
+
"raw",
|
|
246
|
+
hash,
|
|
247
|
+
{ name: "AES-GCM" },
|
|
248
|
+
false,
|
|
249
|
+
["encrypt", "decrypt"]
|
|
250
|
+
);
|
|
251
|
+
},
|
|
252
|
+
encrypt: async (data) => {
|
|
253
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
254
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
255
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
256
|
+
const encoded = new TextEncoder().encode(JSON.stringify(data));
|
|
257
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
258
|
+
{ name: "AES-GCM", iv },
|
|
259
|
+
key,
|
|
260
|
+
encoded
|
|
261
|
+
);
|
|
262
|
+
return {
|
|
263
|
+
iv: Cryptor.toBase64(iv),
|
|
264
|
+
data: Cryptor.toBase64(encrypted)
|
|
265
|
+
};
|
|
266
|
+
},
|
|
267
|
+
decrypt: async (payload) => {
|
|
268
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
269
|
+
console.log("SECRET", SECRET);
|
|
270
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
271
|
+
console.log("key", key);
|
|
272
|
+
const iv = Cryptor.fromBase64(payload.iv);
|
|
273
|
+
const encrypted = Cryptor.fromBase64(payload.data);
|
|
274
|
+
console.log("iv", iv);
|
|
275
|
+
console.log("encrypted", encrypted);
|
|
276
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
277
|
+
{ name: "AES-GCM", iv },
|
|
278
|
+
key,
|
|
279
|
+
encrypted
|
|
280
|
+
);
|
|
281
|
+
console.log("decrypted", decrypted);
|
|
282
|
+
console.log("return", JSON.parse(new TextDecoder().decode(decrypted)));
|
|
283
|
+
return JSON.parse(new TextDecoder().decode(decrypted));
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
var cryptor_default = Cryptor;
|
|
287
|
+
|
|
223
288
|
// src/index.ts
|
|
224
289
|
var Utils = class {
|
|
225
290
|
};
|
|
@@ -228,6 +293,7 @@ Utils.format = format_default;
|
|
|
228
293
|
Utils.mask = mask_default;
|
|
229
294
|
Utils.removeMask = remove_mask_default;
|
|
230
295
|
Utils.default = default_default;
|
|
296
|
+
Utils.cryptor = cryptor_default;
|
|
231
297
|
|
|
232
298
|
// index.ts
|
|
233
299
|
var index_default = Utils;
|
package/dist/index.mjs
CHANGED
|
@@ -184,6 +184,71 @@ var Default = {
|
|
|
184
184
|
};
|
|
185
185
|
var default_default = Default;
|
|
186
186
|
|
|
187
|
+
// src/cryptor/index.ts
|
|
188
|
+
var IV_LENGTH = 12;
|
|
189
|
+
var SECRET = process.env.AUTH_SECRET || process.env.NEXT_PUBLIC_AUTH_SECRET;
|
|
190
|
+
var Cryptor = {
|
|
191
|
+
toBase64: (buffer) => {
|
|
192
|
+
const bytes = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
|
|
193
|
+
let binary = "";
|
|
194
|
+
bytes.forEach((b) => binary += String.fromCharCode(b));
|
|
195
|
+
return btoa(binary);
|
|
196
|
+
},
|
|
197
|
+
fromBase64: (base64) => {
|
|
198
|
+
const binary = atob(base64);
|
|
199
|
+
const bytes = new Uint8Array(binary.length);
|
|
200
|
+
for (let i = 0; i < binary.length; i++) {
|
|
201
|
+
bytes[i] = binary.charCodeAt(i);
|
|
202
|
+
}
|
|
203
|
+
return bytes;
|
|
204
|
+
},
|
|
205
|
+
deriveKey: async (secret) => {
|
|
206
|
+
const enc = new TextEncoder().encode(secret);
|
|
207
|
+
const hash = await crypto.subtle.digest("SHA-256", enc);
|
|
208
|
+
return crypto.subtle.importKey(
|
|
209
|
+
"raw",
|
|
210
|
+
hash,
|
|
211
|
+
{ name: "AES-GCM" },
|
|
212
|
+
false,
|
|
213
|
+
["encrypt", "decrypt"]
|
|
214
|
+
);
|
|
215
|
+
},
|
|
216
|
+
encrypt: async (data) => {
|
|
217
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
218
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
219
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
220
|
+
const encoded = new TextEncoder().encode(JSON.stringify(data));
|
|
221
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
222
|
+
{ name: "AES-GCM", iv },
|
|
223
|
+
key,
|
|
224
|
+
encoded
|
|
225
|
+
);
|
|
226
|
+
return {
|
|
227
|
+
iv: Cryptor.toBase64(iv),
|
|
228
|
+
data: Cryptor.toBase64(encrypted)
|
|
229
|
+
};
|
|
230
|
+
},
|
|
231
|
+
decrypt: async (payload) => {
|
|
232
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
233
|
+
console.log("SECRET", SECRET);
|
|
234
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
235
|
+
console.log("key", key);
|
|
236
|
+
const iv = Cryptor.fromBase64(payload.iv);
|
|
237
|
+
const encrypted = Cryptor.fromBase64(payload.data);
|
|
238
|
+
console.log("iv", iv);
|
|
239
|
+
console.log("encrypted", encrypted);
|
|
240
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
241
|
+
{ name: "AES-GCM", iv },
|
|
242
|
+
key,
|
|
243
|
+
encrypted
|
|
244
|
+
);
|
|
245
|
+
console.log("decrypted", decrypted);
|
|
246
|
+
console.log("return", JSON.parse(new TextDecoder().decode(decrypted)));
|
|
247
|
+
return JSON.parse(new TextDecoder().decode(decrypted));
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
var cryptor_default = Cryptor;
|
|
251
|
+
|
|
187
252
|
// src/index.ts
|
|
188
253
|
var Utils = class {
|
|
189
254
|
};
|
|
@@ -192,6 +257,7 @@ Utils.format = format_default;
|
|
|
192
257
|
Utils.mask = mask_default;
|
|
193
258
|
Utils.removeMask = remove_mask_default;
|
|
194
259
|
Utils.default = default_default;
|
|
260
|
+
Utils.cryptor = cryptor_default;
|
|
195
261
|
|
|
196
262
|
// index.ts
|
|
197
263
|
var index_default = Utils;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const Cryptor: {
|
|
2
|
+
toBase64: (buffer: ArrayBuffer | Uint8Array) => string;
|
|
3
|
+
fromBase64: (base64: string) => Uint8Array;
|
|
4
|
+
deriveKey: (secret: string) => Promise<CryptoKey>;
|
|
5
|
+
encrypt: (data: any) => Promise<{
|
|
6
|
+
iv: string;
|
|
7
|
+
data: string;
|
|
8
|
+
}>;
|
|
9
|
+
decrypt: (payload: {
|
|
10
|
+
iv: string;
|
|
11
|
+
data: string;
|
|
12
|
+
}) => Promise<any>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { Cryptor as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const Cryptor: {
|
|
2
|
+
toBase64: (buffer: ArrayBuffer | Uint8Array) => string;
|
|
3
|
+
fromBase64: (base64: string) => Uint8Array;
|
|
4
|
+
deriveKey: (secret: string) => Promise<CryptoKey>;
|
|
5
|
+
encrypt: (data: any) => Promise<{
|
|
6
|
+
iv: string;
|
|
7
|
+
data: string;
|
|
8
|
+
}>;
|
|
9
|
+
decrypt: (payload: {
|
|
10
|
+
iv: string;
|
|
11
|
+
data: string;
|
|
12
|
+
}) => Promise<any>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { Cryptor as default };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/cryptor/index.ts
|
|
21
|
+
var cryptor_exports = {};
|
|
22
|
+
__export(cryptor_exports, {
|
|
23
|
+
default: () => cryptor_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(cryptor_exports);
|
|
26
|
+
var IV_LENGTH = 12;
|
|
27
|
+
var SECRET = process.env.AUTH_SECRET || process.env.NEXT_PUBLIC_AUTH_SECRET;
|
|
28
|
+
var Cryptor = {
|
|
29
|
+
toBase64: (buffer) => {
|
|
30
|
+
const bytes = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
|
|
31
|
+
let binary = "";
|
|
32
|
+
bytes.forEach((b) => binary += String.fromCharCode(b));
|
|
33
|
+
return btoa(binary);
|
|
34
|
+
},
|
|
35
|
+
fromBase64: (base64) => {
|
|
36
|
+
const binary = atob(base64);
|
|
37
|
+
const bytes = new Uint8Array(binary.length);
|
|
38
|
+
for (let i = 0; i < binary.length; i++) {
|
|
39
|
+
bytes[i] = binary.charCodeAt(i);
|
|
40
|
+
}
|
|
41
|
+
return bytes;
|
|
42
|
+
},
|
|
43
|
+
deriveKey: async (secret) => {
|
|
44
|
+
const enc = new TextEncoder().encode(secret);
|
|
45
|
+
const hash = await crypto.subtle.digest("SHA-256", enc);
|
|
46
|
+
return crypto.subtle.importKey(
|
|
47
|
+
"raw",
|
|
48
|
+
hash,
|
|
49
|
+
{ name: "AES-GCM" },
|
|
50
|
+
false,
|
|
51
|
+
["encrypt", "decrypt"]
|
|
52
|
+
);
|
|
53
|
+
},
|
|
54
|
+
encrypt: async (data) => {
|
|
55
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
56
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
57
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
58
|
+
const encoded = new TextEncoder().encode(JSON.stringify(data));
|
|
59
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
60
|
+
{ name: "AES-GCM", iv },
|
|
61
|
+
key,
|
|
62
|
+
encoded
|
|
63
|
+
);
|
|
64
|
+
return {
|
|
65
|
+
iv: Cryptor.toBase64(iv),
|
|
66
|
+
data: Cryptor.toBase64(encrypted)
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
decrypt: async (payload) => {
|
|
70
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
71
|
+
console.log("SECRET", SECRET);
|
|
72
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
73
|
+
console.log("key", key);
|
|
74
|
+
const iv = Cryptor.fromBase64(payload.iv);
|
|
75
|
+
const encrypted = Cryptor.fromBase64(payload.data);
|
|
76
|
+
console.log("iv", iv);
|
|
77
|
+
console.log("encrypted", encrypted);
|
|
78
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
79
|
+
{ name: "AES-GCM", iv },
|
|
80
|
+
key,
|
|
81
|
+
encrypted
|
|
82
|
+
);
|
|
83
|
+
console.log("decrypted", decrypted);
|
|
84
|
+
console.log("return", JSON.parse(new TextDecoder().decode(decrypted)));
|
|
85
|
+
return JSON.parse(new TextDecoder().decode(decrypted));
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var cryptor_default = Cryptor;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/cryptor/index.ts
|
|
2
|
+
var IV_LENGTH = 12;
|
|
3
|
+
var SECRET = process.env.AUTH_SECRET || process.env.NEXT_PUBLIC_AUTH_SECRET;
|
|
4
|
+
var Cryptor = {
|
|
5
|
+
toBase64: (buffer) => {
|
|
6
|
+
const bytes = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
|
|
7
|
+
let binary = "";
|
|
8
|
+
bytes.forEach((b) => binary += String.fromCharCode(b));
|
|
9
|
+
return btoa(binary);
|
|
10
|
+
},
|
|
11
|
+
fromBase64: (base64) => {
|
|
12
|
+
const binary = atob(base64);
|
|
13
|
+
const bytes = new Uint8Array(binary.length);
|
|
14
|
+
for (let i = 0; i < binary.length; i++) {
|
|
15
|
+
bytes[i] = binary.charCodeAt(i);
|
|
16
|
+
}
|
|
17
|
+
return bytes;
|
|
18
|
+
},
|
|
19
|
+
deriveKey: async (secret) => {
|
|
20
|
+
const enc = new TextEncoder().encode(secret);
|
|
21
|
+
const hash = await crypto.subtle.digest("SHA-256", enc);
|
|
22
|
+
return crypto.subtle.importKey(
|
|
23
|
+
"raw",
|
|
24
|
+
hash,
|
|
25
|
+
{ name: "AES-GCM" },
|
|
26
|
+
false,
|
|
27
|
+
["encrypt", "decrypt"]
|
|
28
|
+
);
|
|
29
|
+
},
|
|
30
|
+
encrypt: async (data) => {
|
|
31
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
32
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
33
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
34
|
+
const encoded = new TextEncoder().encode(JSON.stringify(data));
|
|
35
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
36
|
+
{ name: "AES-GCM", iv },
|
|
37
|
+
key,
|
|
38
|
+
encoded
|
|
39
|
+
);
|
|
40
|
+
return {
|
|
41
|
+
iv: Cryptor.toBase64(iv),
|
|
42
|
+
data: Cryptor.toBase64(encrypted)
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
decrypt: async (payload) => {
|
|
46
|
+
if (!SECRET) throw new Error("AUTH_SECRET or NEXT_PUBLIC_AUTH_SECRET is required!");
|
|
47
|
+
console.log("SECRET", SECRET);
|
|
48
|
+
const key = await Cryptor.deriveKey(SECRET);
|
|
49
|
+
console.log("key", key);
|
|
50
|
+
const iv = Cryptor.fromBase64(payload.iv);
|
|
51
|
+
const encrypted = Cryptor.fromBase64(payload.data);
|
|
52
|
+
console.log("iv", iv);
|
|
53
|
+
console.log("encrypted", encrypted);
|
|
54
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
55
|
+
{ name: "AES-GCM", iv },
|
|
56
|
+
key,
|
|
57
|
+
encrypted
|
|
58
|
+
);
|
|
59
|
+
console.log("decrypted", decrypted);
|
|
60
|
+
console.log("return", JSON.parse(new TextDecoder().decode(decrypted)));
|
|
61
|
+
return JSON.parse(new TextDecoder().decode(decrypted));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var cryptor_default = Cryptor;
|
|
65
|
+
export {
|
|
66
|
+
cryptor_default as default
|
|
67
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gustavo-valsechi/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -65,6 +65,11 @@
|
|
|
65
65
|
"types": "./dist/src/default/index.d.ts",
|
|
66
66
|
"require": "./dist/src/default/index.js",
|
|
67
67
|
"import": "./dist/src/default/index.js"
|
|
68
|
+
},
|
|
69
|
+
"./cryptor": {
|
|
70
|
+
"types": "./dist/src/cryptor/index.d.ts",
|
|
71
|
+
"require": "./dist/src/cryptor/index.js",
|
|
72
|
+
"import": "./dist/src/cryptor/index.js"
|
|
68
73
|
}
|
|
69
74
|
}
|
|
70
75
|
}
|