cipher-kit 0.0.4 → 0.0.6
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-65XPPPAR.cjs +216 -0
- package/dist/chunk-65XPPPAR.cjs.map +1 -0
- package/dist/chunk-F52DT5C4.js +70 -0
- package/dist/chunk-F52DT5C4.js.map +1 -0
- package/dist/chunk-IMSNCUFU.cjs +84 -0
- package/dist/chunk-IMSNCUFU.cjs.map +1 -0
- package/dist/{chunk-HSRKAY2V.js → chunk-PKIT6YQN.js} +41 -27
- package/dist/chunk-PKIT6YQN.js.map +1 -0
- package/dist/chunk-RF735UV3.cjs +153 -0
- package/dist/chunk-RF735UV3.cjs.map +1 -0
- package/dist/{chunk-VJ4BV4K2.js → chunk-UYBB4K56.js} +43 -35
- package/dist/chunk-UYBB4K56.js.map +1 -0
- package/dist/index.cjs +34 -90
- package/dist/index.d.cts +24 -3
- package/dist/index.d.ts +24 -3
- package/dist/index.js +3 -3
- package/dist/node.cjs +26 -22
- package/dist/node.d.cts +5 -6
- package/dist/node.d.ts +5 -6
- package/dist/node.js +2 -2
- package/dist/{utils-1Ds0QKBI.d.cts → types-C7UlGfO2.d.cts} +15 -8
- package/dist/{utils-1Ds0QKBI.d.ts → types-C7UlGfO2.d.ts} +15 -8
- package/dist/web-api.cjs +26 -22
- package/dist/web-api.d.cts +5 -6
- package/dist/web-api.d.ts +5 -6
- package/dist/web-api.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-6EC6NPNV.cjs +0 -139
- package/dist/chunk-6EC6NPNV.cjs.map +0 -1
- package/dist/chunk-DAMEE4F4.cjs +0 -208
- package/dist/chunk-DAMEE4F4.cjs.map +0 -1
- package/dist/chunk-HSRKAY2V.js.map +0 -1
- package/dist/chunk-IHTNNCLY.js +0 -48
- package/dist/chunk-IHTNNCLY.js.map +0 -1
- package/dist/chunk-SSMTQMF4.cjs +0 -58
- package/dist/chunk-SSMTQMF4.cjs.map +0 -1
- package/dist/chunk-VJ4BV4K2.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export { createSecretKey, decode, decrypt, decryptObj, encode, encrypt, encryptObj, generateUuid, hash, isNodeKey, export_exports as node } from './chunk-PKIT6YQN.js';
|
|
2
|
+
export { export_exports as webApi } from './chunk-UYBB4K56.js';
|
|
3
|
+
export { ENCRYPTION_REGEX, NODE_ALGORITHM, WEB_API_ALGORITHM, isInEncryptionFormat, isInNodeEncryptionFormat, isInWebApiEncryptionFormat, parseToObj, stringifyObj } from './chunk-F52DT5C4.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
|
5
5
|
//# sourceMappingURL=index.js.map
|
package/dist/node.cjs
CHANGED
|
@@ -1,69 +1,73 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var chunkRF735UV3_cjs = require('./chunk-RF735UV3.cjs');
|
|
4
|
+
var chunkIMSNCUFU_cjs = require('./chunk-IMSNCUFU.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
Object.defineProperty(exports, "
|
|
8
|
+
Object.defineProperty(exports, "createSecretKey", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkRF735UV3_cjs.createSecretKey; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "decode", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkRF735UV3_cjs.decode; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "decrypt", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkRF735UV3_cjs.decrypt; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "decryptObj", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkRF735UV3_cjs.decryptObj; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "encode", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunkRF735UV3_cjs.encode; }
|
|
27
27
|
});
|
|
28
28
|
Object.defineProperty(exports, "encrypt", {
|
|
29
29
|
enumerable: true,
|
|
30
|
-
get: function () { return
|
|
30
|
+
get: function () { return chunkRF735UV3_cjs.encrypt; }
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(exports, "encryptObj", {
|
|
33
33
|
enumerable: true,
|
|
34
|
-
get: function () { return
|
|
34
|
+
get: function () { return chunkRF735UV3_cjs.encryptObj; }
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "generateUuid", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () { return chunkRF735UV3_cjs.generateUuid; }
|
|
35
39
|
});
|
|
36
40
|
Object.defineProperty(exports, "hash", {
|
|
37
41
|
enumerable: true,
|
|
38
|
-
get: function () { return
|
|
42
|
+
get: function () { return chunkRF735UV3_cjs.hash; }
|
|
39
43
|
});
|
|
40
44
|
Object.defineProperty(exports, "isNodeKey", {
|
|
41
45
|
enumerable: true,
|
|
42
|
-
get: function () { return
|
|
46
|
+
get: function () { return chunkRF735UV3_cjs.isNodeKey; }
|
|
43
47
|
});
|
|
44
|
-
Object.defineProperty(exports, "
|
|
48
|
+
Object.defineProperty(exports, "ENCRYPTION_REGEX", {
|
|
45
49
|
enumerable: true,
|
|
46
|
-
get: function () { return
|
|
50
|
+
get: function () { return chunkIMSNCUFU_cjs.ENCRYPTION_REGEX; }
|
|
47
51
|
});
|
|
48
|
-
Object.defineProperty(exports, "
|
|
52
|
+
Object.defineProperty(exports, "NODE_ALGORITHM", {
|
|
49
53
|
enumerable: true,
|
|
50
|
-
get: function () { return
|
|
54
|
+
get: function () { return chunkIMSNCUFU_cjs.NODE_ALGORITHM; }
|
|
51
55
|
});
|
|
52
|
-
Object.defineProperty(exports, "
|
|
56
|
+
Object.defineProperty(exports, "isInEncryptionFormat", {
|
|
53
57
|
enumerable: true,
|
|
54
|
-
get: function () { return
|
|
58
|
+
get: function () { return chunkIMSNCUFU_cjs.isInEncryptionFormat; }
|
|
55
59
|
});
|
|
56
|
-
Object.defineProperty(exports, "
|
|
60
|
+
Object.defineProperty(exports, "isInNodeEncryptionFormat", {
|
|
57
61
|
enumerable: true,
|
|
58
|
-
get: function () { return
|
|
62
|
+
get: function () { return chunkIMSNCUFU_cjs.isInNodeEncryptionFormat; }
|
|
59
63
|
});
|
|
60
64
|
Object.defineProperty(exports, "parseToObj", {
|
|
61
65
|
enumerable: true,
|
|
62
|
-
get: function () { return
|
|
66
|
+
get: function () { return chunkIMSNCUFU_cjs.parseToObj; }
|
|
63
67
|
});
|
|
64
68
|
Object.defineProperty(exports, "stringifyObj", {
|
|
65
69
|
enumerable: true,
|
|
66
|
-
get: function () { return
|
|
70
|
+
get: function () { return chunkIMSNCUFU_cjs.stringifyObj; }
|
|
67
71
|
});
|
|
68
72
|
//# sourceMappingURL=node.cjs.map
|
|
69
73
|
//# sourceMappingURL=node.cjs.map
|
package/dist/node.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { E as
|
|
1
|
+
import { e as EncodingFormat, R as Result, c as NodeKey } from './types-C7UlGfO2.cjs';
|
|
2
|
+
export { E as ENCRYPTION_REGEX, N as NODE_ALGORITHM, i as isInEncryptionFormat, a as isInNodeEncryptionFormat, p as parseToObj, s as stringifyObj } from './types-C7UlGfO2.cjs';
|
|
3
3
|
import { Buffer } from 'node:buffer';
|
|
4
4
|
import nodeCrypto from 'node:crypto';
|
|
5
5
|
|
|
@@ -8,11 +8,10 @@ declare function encode(data: string, format?: EncodingFormat): Result<{
|
|
|
8
8
|
}>;
|
|
9
9
|
declare function decode(data: Buffer, format?: EncodingFormat): Result<string>;
|
|
10
10
|
|
|
11
|
-
declare
|
|
12
|
-
declare function newUuid(): Result<string>;
|
|
11
|
+
declare function generateUuid(): Result<string>;
|
|
13
12
|
declare function isNodeKey(key: unknown): key is nodeCrypto.KeyObject;
|
|
14
13
|
declare function hash(data: string): Result<string>;
|
|
15
|
-
declare function
|
|
14
|
+
declare function createSecretKey(key: string | NodeKey): Result<{
|
|
16
15
|
secretKey: NodeKey;
|
|
17
16
|
}>;
|
|
18
17
|
declare function encrypt(data: string, secretKey: NodeKey): Result<string>;
|
|
@@ -22,4 +21,4 @@ declare function decryptObj(encrypted: string, secretKey: NodeKey): Result<{
|
|
|
22
21
|
result: Record<string, unknown>;
|
|
23
22
|
}>;
|
|
24
23
|
|
|
25
|
-
export {
|
|
24
|
+
export { NodeKey, createSecretKey, decode, decrypt, decryptObj, encode, encrypt, encryptObj, generateUuid, hash, isNodeKey };
|
package/dist/node.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { E as
|
|
1
|
+
import { e as EncodingFormat, R as Result, c as NodeKey } from './types-C7UlGfO2.js';
|
|
2
|
+
export { E as ENCRYPTION_REGEX, N as NODE_ALGORITHM, i as isInEncryptionFormat, a as isInNodeEncryptionFormat, p as parseToObj, s as stringifyObj } from './types-C7UlGfO2.js';
|
|
3
3
|
import { Buffer } from 'node:buffer';
|
|
4
4
|
import nodeCrypto from 'node:crypto';
|
|
5
5
|
|
|
@@ -8,11 +8,10 @@ declare function encode(data: string, format?: EncodingFormat): Result<{
|
|
|
8
8
|
}>;
|
|
9
9
|
declare function decode(data: Buffer, format?: EncodingFormat): Result<string>;
|
|
10
10
|
|
|
11
|
-
declare
|
|
12
|
-
declare function newUuid(): Result<string>;
|
|
11
|
+
declare function generateUuid(): Result<string>;
|
|
13
12
|
declare function isNodeKey(key: unknown): key is nodeCrypto.KeyObject;
|
|
14
13
|
declare function hash(data: string): Result<string>;
|
|
15
|
-
declare function
|
|
14
|
+
declare function createSecretKey(key: string | NodeKey): Result<{
|
|
16
15
|
secretKey: NodeKey;
|
|
17
16
|
}>;
|
|
18
17
|
declare function encrypt(data: string, secretKey: NodeKey): Result<string>;
|
|
@@ -22,4 +21,4 @@ declare function decryptObj(encrypted: string, secretKey: NodeKey): Result<{
|
|
|
22
21
|
result: Record<string, unknown>;
|
|
23
22
|
}>;
|
|
24
23
|
|
|
25
|
-
export {
|
|
24
|
+
export { NodeKey, createSecretKey, decode, decrypt, decryptObj, encode, encrypt, encryptObj, generateUuid, hash, isNodeKey };
|
package/dist/node.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { createSecretKey, decode, decrypt, decryptObj, encode, encrypt, encryptObj, generateUuid, hash, isNodeKey } from './chunk-PKIT6YQN.js';
|
|
2
|
+
export { ENCRYPTION_REGEX, NODE_ALGORITHM, isInEncryptionFormat, isInNodeEncryptionFormat, parseToObj, stringifyObj } from './chunk-F52DT5C4.js';
|
|
3
3
|
//# sourceMappingURL=node.js.map
|
|
4
4
|
//# sourceMappingURL=node.js.map
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import nodeCrypto, { webcrypto } from 'node:crypto';
|
|
2
2
|
|
|
3
|
-
type WebApiKey = webcrypto.CryptoKey;
|
|
4
|
-
type NodeKey = nodeCrypto.KeyObject;
|
|
5
|
-
type EncodingFormat = 'base64' | 'base64url' | 'hex' | 'utf8';
|
|
6
|
-
|
|
7
3
|
interface ResultErr {
|
|
8
4
|
readonly message: string;
|
|
9
5
|
readonly description: string;
|
|
@@ -28,12 +24,23 @@ type Result<T, E = ResultErr> = T extends object ? ({
|
|
|
28
24
|
readonly result?: undefined;
|
|
29
25
|
};
|
|
30
26
|
|
|
31
|
-
declare const
|
|
32
|
-
declare const
|
|
33
|
-
declare const
|
|
27
|
+
declare const NODE_ALGORITHM = "aes-256-gcm";
|
|
28
|
+
declare const WEB_API_ALGORITHM = "AES-GCM";
|
|
29
|
+
declare const ENCRYPTION_REGEX: Readonly<{
|
|
30
|
+
GENERAL: RegExp;
|
|
31
|
+
NODE: RegExp;
|
|
32
|
+
WEB_API: RegExp;
|
|
33
|
+
}>;
|
|
34
|
+
declare function isInEncryptionFormat(data: string): boolean;
|
|
35
|
+
declare function isInNodeEncryptionFormat(data: string): boolean;
|
|
36
|
+
declare function isInWebApiEncryptionFormat(data: string): boolean;
|
|
34
37
|
declare function stringifyObj(obj: Record<string, unknown>): Result<string>;
|
|
35
38
|
declare function parseToObj(str: string): Result<{
|
|
36
39
|
result: Record<string, unknown>;
|
|
37
40
|
}>;
|
|
38
41
|
|
|
39
|
-
|
|
42
|
+
type WebApiKey = webcrypto.CryptoKey;
|
|
43
|
+
type NodeKey = nodeCrypto.KeyObject;
|
|
44
|
+
type EncodingFormat = 'base64' | 'base64url' | 'hex' | 'utf8';
|
|
45
|
+
|
|
46
|
+
export { ENCRYPTION_REGEX as E, NODE_ALGORITHM as N, type Result as R, WEB_API_ALGORITHM as W, isInNodeEncryptionFormat as a, isInWebApiEncryptionFormat as b, type NodeKey as c, type WebApiKey as d, type EncodingFormat as e, isInEncryptionFormat as i, parseToObj as p, stringifyObj as s };
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import nodeCrypto, { webcrypto } from 'node:crypto';
|
|
2
2
|
|
|
3
|
-
type WebApiKey = webcrypto.CryptoKey;
|
|
4
|
-
type NodeKey = nodeCrypto.KeyObject;
|
|
5
|
-
type EncodingFormat = 'base64' | 'base64url' | 'hex' | 'utf8';
|
|
6
|
-
|
|
7
3
|
interface ResultErr {
|
|
8
4
|
readonly message: string;
|
|
9
5
|
readonly description: string;
|
|
@@ -28,12 +24,23 @@ type Result<T, E = ResultErr> = T extends object ? ({
|
|
|
28
24
|
readonly result?: undefined;
|
|
29
25
|
};
|
|
30
26
|
|
|
31
|
-
declare const
|
|
32
|
-
declare const
|
|
33
|
-
declare const
|
|
27
|
+
declare const NODE_ALGORITHM = "aes-256-gcm";
|
|
28
|
+
declare const WEB_API_ALGORITHM = "AES-GCM";
|
|
29
|
+
declare const ENCRYPTION_REGEX: Readonly<{
|
|
30
|
+
GENERAL: RegExp;
|
|
31
|
+
NODE: RegExp;
|
|
32
|
+
WEB_API: RegExp;
|
|
33
|
+
}>;
|
|
34
|
+
declare function isInEncryptionFormat(data: string): boolean;
|
|
35
|
+
declare function isInNodeEncryptionFormat(data: string): boolean;
|
|
36
|
+
declare function isInWebApiEncryptionFormat(data: string): boolean;
|
|
34
37
|
declare function stringifyObj(obj: Record<string, unknown>): Result<string>;
|
|
35
38
|
declare function parseToObj(str: string): Result<{
|
|
36
39
|
result: Record<string, unknown>;
|
|
37
40
|
}>;
|
|
38
41
|
|
|
39
|
-
|
|
42
|
+
type WebApiKey = webcrypto.CryptoKey;
|
|
43
|
+
type NodeKey = nodeCrypto.KeyObject;
|
|
44
|
+
type EncodingFormat = 'base64' | 'base64url' | 'hex' | 'utf8';
|
|
45
|
+
|
|
46
|
+
export { ENCRYPTION_REGEX as E, NODE_ALGORITHM as N, type Result as R, WEB_API_ALGORITHM as W, isInNodeEncryptionFormat as a, isInWebApiEncryptionFormat as b, type NodeKey as c, type WebApiKey as d, type EncodingFormat as e, isInEncryptionFormat as i, parseToObj as p, stringifyObj as s };
|
package/dist/web-api.cjs
CHANGED
|
@@ -1,69 +1,73 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var chunk65XPPPAR_cjs = require('./chunk-65XPPPAR.cjs');
|
|
4
|
+
var chunkIMSNCUFU_cjs = require('./chunk-IMSNCUFU.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
Object.defineProperty(exports, "
|
|
8
|
+
Object.defineProperty(exports, "createSecretKey", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunk65XPPPAR_cjs.createSecretKey; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "decode", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunk65XPPPAR_cjs.decode; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "decrypt", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunk65XPPPAR_cjs.decrypt; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "decryptObj", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunk65XPPPAR_cjs.decryptObj; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "encode", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunk65XPPPAR_cjs.encode; }
|
|
27
27
|
});
|
|
28
28
|
Object.defineProperty(exports, "encrypt", {
|
|
29
29
|
enumerable: true,
|
|
30
|
-
get: function () { return
|
|
30
|
+
get: function () { return chunk65XPPPAR_cjs.encrypt; }
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(exports, "encryptObj", {
|
|
33
33
|
enumerable: true,
|
|
34
|
-
get: function () { return
|
|
34
|
+
get: function () { return chunk65XPPPAR_cjs.encryptObj; }
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "generateUuid", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () { return chunk65XPPPAR_cjs.generateUuid; }
|
|
35
39
|
});
|
|
36
40
|
Object.defineProperty(exports, "hash", {
|
|
37
41
|
enumerable: true,
|
|
38
|
-
get: function () { return
|
|
42
|
+
get: function () { return chunk65XPPPAR_cjs.hash; }
|
|
39
43
|
});
|
|
40
44
|
Object.defineProperty(exports, "isWebApiKey", {
|
|
41
45
|
enumerable: true,
|
|
42
|
-
get: function () { return
|
|
46
|
+
get: function () { return chunk65XPPPAR_cjs.isWebApiKey; }
|
|
43
47
|
});
|
|
44
|
-
Object.defineProperty(exports, "
|
|
48
|
+
Object.defineProperty(exports, "ENCRYPTION_REGEX", {
|
|
45
49
|
enumerable: true,
|
|
46
|
-
get: function () { return
|
|
50
|
+
get: function () { return chunkIMSNCUFU_cjs.ENCRYPTION_REGEX; }
|
|
47
51
|
});
|
|
48
|
-
Object.defineProperty(exports, "
|
|
52
|
+
Object.defineProperty(exports, "WEB_API_ALGORITHM", {
|
|
49
53
|
enumerable: true,
|
|
50
|
-
get: function () { return
|
|
54
|
+
get: function () { return chunkIMSNCUFU_cjs.WEB_API_ALGORITHM; }
|
|
51
55
|
});
|
|
52
|
-
Object.defineProperty(exports, "
|
|
56
|
+
Object.defineProperty(exports, "isInEncryptionFormat", {
|
|
53
57
|
enumerable: true,
|
|
54
|
-
get: function () { return
|
|
58
|
+
get: function () { return chunkIMSNCUFU_cjs.isInEncryptionFormat; }
|
|
55
59
|
});
|
|
56
|
-
Object.defineProperty(exports, "
|
|
60
|
+
Object.defineProperty(exports, "isInWebApiEncryptionFormat", {
|
|
57
61
|
enumerable: true,
|
|
58
|
-
get: function () { return
|
|
62
|
+
get: function () { return chunkIMSNCUFU_cjs.isInWebApiEncryptionFormat; }
|
|
59
63
|
});
|
|
60
64
|
Object.defineProperty(exports, "parseToObj", {
|
|
61
65
|
enumerable: true,
|
|
62
|
-
get: function () { return
|
|
66
|
+
get: function () { return chunkIMSNCUFU_cjs.parseToObj; }
|
|
63
67
|
});
|
|
64
68
|
Object.defineProperty(exports, "stringifyObj", {
|
|
65
69
|
enumerable: true,
|
|
66
|
-
get: function () { return
|
|
70
|
+
get: function () { return chunkIMSNCUFU_cjs.stringifyObj; }
|
|
67
71
|
});
|
|
68
72
|
//# sourceMappingURL=web-api.cjs.map
|
|
69
73
|
//# sourceMappingURL=web-api.cjs.map
|
package/dist/web-api.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { e as EncodingFormat, R as Result, d as WebApiKey } from './types-C7UlGfO2.cjs';
|
|
2
|
+
export { E as ENCRYPTION_REGEX, W as WEB_API_ALGORITHM, i as isInEncryptionFormat, b as isInWebApiEncryptionFormat, p as parseToObj, s as stringifyObj } from './types-C7UlGfO2.cjs';
|
|
3
3
|
import 'node:crypto';
|
|
4
4
|
|
|
5
5
|
declare function encode(data: string, format?: EncodingFormat): Result<{
|
|
@@ -7,11 +7,10 @@ declare function encode(data: string, format?: EncodingFormat): Result<{
|
|
|
7
7
|
}>;
|
|
8
8
|
declare function decode(data: ArrayBuffer | Uint8Array, format?: EncodingFormat): Result<string>;
|
|
9
9
|
|
|
10
|
-
declare
|
|
11
|
-
declare function newUuid(): Result<string>;
|
|
10
|
+
declare function generateUuid(): Result<string>;
|
|
12
11
|
declare function isWebApiKey(key: unknown): key is WebApiKey;
|
|
13
12
|
declare function hash(data: string): Promise<Result<string>>;
|
|
14
|
-
declare function
|
|
13
|
+
declare function createSecretKey(key: string | WebApiKey): Promise<Result<{
|
|
15
14
|
secretKey: WebApiKey;
|
|
16
15
|
}>>;
|
|
17
16
|
declare function encrypt(data: string, secretKey: WebApiKey): Promise<Result<string>>;
|
|
@@ -21,4 +20,4 @@ declare function decryptObj(encrypted: string, secretKey: WebApiKey): Promise<Re
|
|
|
21
20
|
result: Record<string, unknown>;
|
|
22
21
|
}>>;
|
|
23
22
|
|
|
24
|
-
export {
|
|
23
|
+
export { WebApiKey, createSecretKey, decode, decrypt, decryptObj, encode, encrypt, encryptObj, generateUuid, hash, isWebApiKey };
|
package/dist/web-api.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { e as EncodingFormat, R as Result, d as WebApiKey } from './types-C7UlGfO2.js';
|
|
2
|
+
export { E as ENCRYPTION_REGEX, W as WEB_API_ALGORITHM, i as isInEncryptionFormat, b as isInWebApiEncryptionFormat, p as parseToObj, s as stringifyObj } from './types-C7UlGfO2.js';
|
|
3
3
|
import 'node:crypto';
|
|
4
4
|
|
|
5
5
|
declare function encode(data: string, format?: EncodingFormat): Result<{
|
|
@@ -7,11 +7,10 @@ declare function encode(data: string, format?: EncodingFormat): Result<{
|
|
|
7
7
|
}>;
|
|
8
8
|
declare function decode(data: ArrayBuffer | Uint8Array, format?: EncodingFormat): Result<string>;
|
|
9
9
|
|
|
10
|
-
declare
|
|
11
|
-
declare function newUuid(): Result<string>;
|
|
10
|
+
declare function generateUuid(): Result<string>;
|
|
12
11
|
declare function isWebApiKey(key: unknown): key is WebApiKey;
|
|
13
12
|
declare function hash(data: string): Promise<Result<string>>;
|
|
14
|
-
declare function
|
|
13
|
+
declare function createSecretKey(key: string | WebApiKey): Promise<Result<{
|
|
15
14
|
secretKey: WebApiKey;
|
|
16
15
|
}>>;
|
|
17
16
|
declare function encrypt(data: string, secretKey: WebApiKey): Promise<Result<string>>;
|
|
@@ -21,4 +20,4 @@ declare function decryptObj(encrypted: string, secretKey: WebApiKey): Promise<Re
|
|
|
21
20
|
result: Record<string, unknown>;
|
|
22
21
|
}>>;
|
|
23
22
|
|
|
24
|
-
export {
|
|
23
|
+
export { WebApiKey, createSecretKey, decode, decrypt, decryptObj, encode, encrypt, encryptObj, generateUuid, hash, isWebApiKey };
|
package/dist/web-api.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { createSecretKey, decode, decrypt, decryptObj, encode, encrypt, encryptObj, generateUuid, hash, isWebApiKey } from './chunk-UYBB4K56.js';
|
|
2
|
+
export { ENCRYPTION_REGEX, WEB_API_ALGORITHM, isInEncryptionFormat, isInWebApiEncryptionFormat, parseToObj, stringifyObj } from './chunk-F52DT5C4.js';
|
|
3
3
|
//# sourceMappingURL=web-api.js.map
|
|
4
4
|
//# sourceMappingURL=web-api.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cipher-kit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "🔐 Secure, Lightweight, and Cross-Platform Encryption & Decryption for Web, Node.js, Deno, and Bun",
|
|
5
5
|
"homepage": "https://github.com/WolfieLeader/npm/tree/main/packages/cipher-kit#readme",
|
|
6
6
|
"repository": {
|
package/dist/chunk-6EC6NPNV.cjs
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkSSMTQMF4_cjs = require('./chunk-SSMTQMF4.cjs');
|
|
4
|
-
var buffer = require('buffer');
|
|
5
|
-
var nodeCrypto = require('crypto');
|
|
6
|
-
|
|
7
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
-
|
|
9
|
-
var nodeCrypto__default = /*#__PURE__*/_interopDefault(nodeCrypto);
|
|
10
|
-
|
|
11
|
-
function encode(data, format = "utf8") {
|
|
12
|
-
try {
|
|
13
|
-
return chunkSSMTQMF4_cjs.$ok({ bytes: buffer.Buffer.from(data, format) });
|
|
14
|
-
} catch (error) {
|
|
15
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to encode data", description: chunkSSMTQMF4_cjs.$stringifyError(error) });
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
function decode(data, format = "utf8") {
|
|
19
|
-
try {
|
|
20
|
-
return chunkSSMTQMF4_cjs.$ok(buffer.Buffer.from(data).toString(format));
|
|
21
|
-
} catch (error) {
|
|
22
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to decode data", description: chunkSSMTQMF4_cjs.$stringifyError(error) });
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
var NODE_ALGORITHM = "aes-256-gcm";
|
|
26
|
-
function newUuid() {
|
|
27
|
-
try {
|
|
28
|
-
return chunkSSMTQMF4_cjs.$ok(nodeCrypto__default.default.randomUUID());
|
|
29
|
-
} catch (error) {
|
|
30
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to generate UUID with Crypto NodeJS", description: chunkSSMTQMF4_cjs.$stringifyError(error) });
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function isNodeKey(key) {
|
|
34
|
-
return key instanceof nodeCrypto__default.default.KeyObject;
|
|
35
|
-
}
|
|
36
|
-
function hash(data) {
|
|
37
|
-
if (!chunkSSMTQMF4_cjs.$isStr(data)) {
|
|
38
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Empty data for hashing", description: "Data must be a non-empty string" });
|
|
39
|
-
}
|
|
40
|
-
try {
|
|
41
|
-
const hashed = nodeCrypto__default.default.createHash("sha256").update(data).digest();
|
|
42
|
-
return decode(hashed, "base64url");
|
|
43
|
-
} catch (error) {
|
|
44
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to hash data with Crypto NodeJS", description: chunkSSMTQMF4_cjs.$stringifyError(error) });
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function newSecretKey(key) {
|
|
48
|
-
if (typeof key === "string") {
|
|
49
|
-
if (!chunkSSMTQMF4_cjs.$isStr(key, 1)) return chunkSSMTQMF4_cjs.$err({ message: "Empty key for Crypto NodeJS", description: "Invalid secret key" });
|
|
50
|
-
try {
|
|
51
|
-
const hashedKey = nodeCrypto__default.default.createHash("sha256").update(key).digest();
|
|
52
|
-
const secretKey = nodeCrypto__default.default.createSecretKey(hashedKey);
|
|
53
|
-
return chunkSSMTQMF4_cjs.$ok({ secretKey });
|
|
54
|
-
} catch (error) {
|
|
55
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to create secret key with Crypto NodeJS", description: chunkSSMTQMF4_cjs.$stringifyError(error) });
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (!isNodeKey(key)) return chunkSSMTQMF4_cjs.$err({ message: "Invalid secret key", description: "Expected a crypto.KeyObject" });
|
|
59
|
-
return chunkSSMTQMF4_cjs.$ok({ secretKey: key });
|
|
60
|
-
}
|
|
61
|
-
function encrypt(data, secretKey) {
|
|
62
|
-
if (!chunkSSMTQMF4_cjs.$isStr(data)) {
|
|
63
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Empty data for encryption", description: "Data must be a non-empty string" });
|
|
64
|
-
}
|
|
65
|
-
if (!isNodeKey(secretKey)) {
|
|
66
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Invalid encryption key", description: "Expected a crypto.KeyObject" });
|
|
67
|
-
}
|
|
68
|
-
try {
|
|
69
|
-
const iv = nodeCrypto__default.default.randomBytes(12);
|
|
70
|
-
const cipher = nodeCrypto__default.default.createCipheriv(NODE_ALGORITHM, secretKey, iv);
|
|
71
|
-
const encrypted = buffer.Buffer.concat([cipher.update(data, "utf8"), cipher.final()]);
|
|
72
|
-
const tag = cipher.getAuthTag();
|
|
73
|
-
const { result: decodedIv, error: ivError } = decode(iv, "base64url");
|
|
74
|
-
const { result: decodedEncrypted, error: encryptedError } = decode(encrypted, "base64url");
|
|
75
|
-
const { result: decodedTag, error: tagError } = decode(tag, "base64url");
|
|
76
|
-
if (ivError || encryptedError || tagError) {
|
|
77
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to encode encrypted data", description: "Encoding error" });
|
|
78
|
-
}
|
|
79
|
-
return chunkSSMTQMF4_cjs.$ok(`${decodedIv}.${decodedEncrypted}.${decodedTag}.`);
|
|
80
|
-
} catch (error) {
|
|
81
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to encrypt data with Crypto NodeJS", description: chunkSSMTQMF4_cjs.$stringifyError(error) });
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
function decrypt(encrypted, secretKey) {
|
|
85
|
-
if (chunkSSMTQMF4_cjs.ENCRYPTED_NODE_REGEX.test(encrypted) === false) {
|
|
86
|
-
return chunkSSMTQMF4_cjs.$err({
|
|
87
|
-
message: "Invalid encrypted data format",
|
|
88
|
-
description: 'Encrypted data must be in the format "iv.encrypted.tag."'
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
const [iv, encryptedData, tag] = encrypted.split(".", 4);
|
|
92
|
-
if (!chunkSSMTQMF4_cjs.$isStr(iv, 1) || !chunkSSMTQMF4_cjs.$isStr(encryptedData, 1) || !chunkSSMTQMF4_cjs.$isStr(tag, 1)) {
|
|
93
|
-
return chunkSSMTQMF4_cjs.$err({
|
|
94
|
-
message: "Invalid parameters for decryption",
|
|
95
|
-
description: "IV, encrypted data, and tag must be non-empty strings"
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
if (!isNodeKey(secretKey)) {
|
|
99
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Invalid decryption key", description: "Expected a crypto.KeyObject" });
|
|
100
|
-
}
|
|
101
|
-
const { bytes: ivBytes, error: ivError } = encode(iv, "base64url");
|
|
102
|
-
const { bytes: encryptedBytes, error: encryptedError } = encode(encryptedData, "base64url");
|
|
103
|
-
const { bytes: tagBytes, error: tagError } = encode(tag, "base64url");
|
|
104
|
-
if (ivError || encryptedError || tagError) {
|
|
105
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to encode IV or encrypted data", description: "Encoding error" });
|
|
106
|
-
}
|
|
107
|
-
try {
|
|
108
|
-
const decipher = nodeCrypto__default.default.createDecipheriv(NODE_ALGORITHM, secretKey, ivBytes);
|
|
109
|
-
decipher.setAuthTag(tagBytes);
|
|
110
|
-
const decrypted = buffer.Buffer.concat([decipher.update(encryptedBytes), decipher.final()]);
|
|
111
|
-
return decode(decrypted, "utf8");
|
|
112
|
-
} catch (error) {
|
|
113
|
-
return chunkSSMTQMF4_cjs.$err({ message: "Failed to decrypt data with Crypto NodeJS", description: chunkSSMTQMF4_cjs.$stringifyError(error) });
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function encryptObj(data, secretKey) {
|
|
117
|
-
const { result, error } = chunkSSMTQMF4_cjs.stringifyObj(data);
|
|
118
|
-
if (error) return chunkSSMTQMF4_cjs.$err(error);
|
|
119
|
-
return encrypt(result, secretKey);
|
|
120
|
-
}
|
|
121
|
-
function decryptObj(encrypted, secretKey) {
|
|
122
|
-
const { result, error } = decrypt(encrypted, secretKey);
|
|
123
|
-
if (error) return chunkSSMTQMF4_cjs.$err(error);
|
|
124
|
-
return chunkSSMTQMF4_cjs.parseToObj(result);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
exports.NODE_ALGORITHM = NODE_ALGORITHM;
|
|
128
|
-
exports.decode = decode;
|
|
129
|
-
exports.decrypt = decrypt;
|
|
130
|
-
exports.decryptObj = decryptObj;
|
|
131
|
-
exports.encode = encode;
|
|
132
|
-
exports.encrypt = encrypt;
|
|
133
|
-
exports.encryptObj = encryptObj;
|
|
134
|
-
exports.hash = hash;
|
|
135
|
-
exports.isNodeKey = isNodeKey;
|
|
136
|
-
exports.newSecretKey = newSecretKey;
|
|
137
|
-
exports.newUuid = newUuid;
|
|
138
|
-
//# sourceMappingURL=chunk-6EC6NPNV.cjs.map
|
|
139
|
-
//# sourceMappingURL=chunk-6EC6NPNV.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/node/encode.ts","../src/node/encrypt.ts"],"names":["$ok","Buffer","$err","$stringifyError","nodeCrypto","$isStr","ENCRYPTED_NODE_REGEX","stringifyObj","parseToObj"],"mappings":";;;;;;;;;;AAIO,SAAS,MAAA,CAAO,IAAA,EAAc,MAAA,GAAyB,MAAA,EAAmC;AAC/F,EAAA,IAAI;AACF,IAAA,OAAOA,qBAAA,CAAI,EAAE,KAAA,EAAOC,aAAA,CAAO,KAAK,IAAA,EAAM,MAAM,GAAG,CAAA;AAAA,EACjD,SAAS,KAAA,EAAO;AACd,IAAA,OAAOC,sBAAA,CAAK,EAAE,OAAA,EAAS,uBAAA,EAAyB,aAAaC,iCAAA,CAAgB,KAAK,GAAG,CAAA;AAAA,EACvF;AACF;AAEO,SAAS,MAAA,CAAO,IAAA,EAAc,MAAA,GAAyB,MAAA,EAAwB;AACpF,EAAA,IAAI;AACF,IAAA,OAAOH,sBAAIC,aAAA,CAAO,IAAA,CAAK,IAAI,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,sBAAA,CAAK,EAAE,OAAA,EAAS,2CAAA,EAA6C,aAAaC,iCAAA,CAAgB,KAAK,GAAG,CAAA;AAAA,EAC3G;AACF;AAEO,SAAS,UAAA,CAAW,MAA+B,SAAA,EAAoC;AAC5F,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAII,+BAAa,IAAI,CAAA;AAC3C,EAAA,IAAI,KAAA,EAAO,OAAOL,sBAAA,CAAK,KAAK,CAAA;AAC5B,EAAA,OAAO,OAAA,CAAQ,QAAQ,SAAS,CAAA;AAClC;AAEO,SAAS,UAAA,CAAW,WAAmB,SAAA,EAAiE;AAC7G,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAI,OAAA,CAAQ,WAAW,SAAS,CAAA;AACtD,EAAA,IAAI,KAAA,EAAO,OAAOA,sBAAA,CAAK,KAAK,CAAA;AAC5B,EAAA,OAAOM,6BAAW,MAAM,CAAA;AAC1B","file":"chunk-6EC6NPNV.cjs","sourcesContent":["import { Buffer } from 'node:buffer';\r\nimport { $err, $ok, $stringifyError, type Result } from '~/error';\r\nimport type { EncodingFormat } from '~/types';\r\n\r\nexport function encode(data: string, format: EncodingFormat = 'utf8'): Result<{ bytes: Buffer }> {\r\n try {\r\n return $ok({ bytes: Buffer.from(data, format) });\r\n } catch (error) {\r\n return $err({ message: 'Failed to encode data', description: $stringifyError(error) });\r\n }\r\n}\r\n\r\nexport function decode(data: Buffer, format: EncodingFormat = 'utf8'): Result<string> {\r\n try {\r\n return $ok(Buffer.from(data).toString(format));\r\n } catch (error) {\r\n return $err({ message: 'Failed to decode data', description: $stringifyError(error) });\r\n }\r\n}\r\n","import { Buffer } from 'node:buffer';\r\nimport nodeCrypto from 'node:crypto';\r\nimport { $err, $ok, $stringifyError, type Result } from '~/error';\r\nimport type { NodeKey } from '~/types';\r\nimport { $isStr, ENCRYPTED_NODE_REGEX, parseToObj, stringifyObj } from '~/utils';\r\nimport { decode, encode } from './encode';\r\n\r\nexport const NODE_ALGORITHM = 'aes-256-gcm';\r\n\r\nexport function newUuid(): Result<string> {\r\n try {\r\n return $ok(nodeCrypto.randomUUID());\r\n } catch (error) {\r\n return $err({ message: 'Failed to generate UUID with Crypto NodeJS', description: $stringifyError(error) });\r\n }\r\n}\r\n\r\nexport function isNodeKey(key: unknown): key is nodeCrypto.KeyObject {\r\n return key instanceof nodeCrypto.KeyObject;\r\n}\r\n\r\nexport function hash(data: string): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({ message: 'Empty data for hashing', description: 'Data must be a non-empty string' });\r\n }\r\n\r\n try {\r\n const hashed = nodeCrypto.createHash('sha256').update(data).digest();\r\n return decode(hashed, 'base64url');\r\n } catch (error) {\r\n return $err({ message: 'Failed to hash data with Crypto NodeJS', description: $stringifyError(error) });\r\n }\r\n}\r\n\r\nexport function newSecretKey(key: string | NodeKey): Result<{ secretKey: NodeKey }> {\r\n if (typeof key === 'string') {\r\n if (!$isStr(key, 1)) return $err({ message: 'Empty key for Crypto NodeJS', description: 'Invalid secret key' });\r\n\r\n try {\r\n const hashedKey = nodeCrypto.createHash('sha256').update(key).digest();\r\n const secretKey = nodeCrypto.createSecretKey(hashedKey);\r\n return $ok({ secretKey });\r\n } catch (error) {\r\n return $err({ message: 'Failed to create secret key with Crypto NodeJS', description: $stringifyError(error) });\r\n }\r\n }\r\n\r\n if (!isNodeKey(key)) return $err({ message: 'Invalid secret key', description: 'Expected a crypto.KeyObject' });\r\n return $ok({ secretKey: key });\r\n}\r\n\r\nexport function encrypt(data: string, secretKey: NodeKey): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({ message: 'Empty data for encryption', description: 'Data must be a non-empty string' });\r\n }\r\n\r\n if (!isNodeKey(secretKey)) {\r\n return $err({ message: 'Invalid encryption key', description: 'Expected a crypto.KeyObject' });\r\n }\r\n\r\n try {\r\n const iv = nodeCrypto.randomBytes(12);\r\n const cipher = nodeCrypto.createCipheriv(NODE_ALGORITHM, secretKey, iv);\r\n const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]);\r\n const tag = cipher.getAuthTag();\r\n\r\n const { result: decodedIv, error: ivError } = decode(iv, 'base64url');\r\n const { result: decodedEncrypted, error: encryptedError } = decode(encrypted, 'base64url');\r\n const { result: decodedTag, error: tagError } = decode(tag, 'base64url');\r\n if (ivError || encryptedError || tagError) {\r\n return $err({ message: 'Failed to encode encrypted data', description: 'Encoding error' });\r\n }\r\n\r\n return $ok(`${decodedIv}.${decodedEncrypted}.${decodedTag}.`);\r\n } catch (error) {\r\n return $err({ message: 'Failed to encrypt data with Crypto NodeJS', description: $stringifyError(error) });\r\n }\r\n}\r\n\r\nexport function decrypt(encrypted: string, secretKey: NodeKey): Result<string> {\r\n if (ENCRYPTED_NODE_REGEX.test(encrypted) === false) {\r\n return $err({\r\n message: 'Invalid encrypted data format',\r\n description: 'Encrypted data must be in the format \"iv.encrypted.tag.\"',\r\n });\r\n }\r\n\r\n const [iv, encryptedData, tag] = encrypted.split('.', 4);\r\n if (!$isStr(iv, 1) || !$isStr(encryptedData, 1) || !$isStr(tag, 1)) {\r\n return $err({\r\n message: 'Invalid parameters for decryption',\r\n description: 'IV, encrypted data, and tag must be non-empty strings',\r\n });\r\n }\r\n\r\n if (!isNodeKey(secretKey)) {\r\n return $err({ message: 'Invalid decryption key', description: 'Expected a crypto.KeyObject' });\r\n }\r\n\r\n const { bytes: ivBytes, error: ivError } = encode(iv, 'base64url');\r\n const { bytes: encryptedBytes, error: encryptedError } = encode(encryptedData, 'base64url');\r\n const { bytes: tagBytes, error: tagError } = encode(tag, 'base64url');\r\n if (ivError || encryptedError || tagError) {\r\n return $err({ message: 'Failed to encode IV or encrypted data', description: 'Encoding error' });\r\n }\r\n\r\n try {\r\n const decipher = nodeCrypto.createDecipheriv(NODE_ALGORITHM, secretKey, ivBytes);\r\n decipher.setAuthTag(tagBytes);\r\n\r\n const decrypted = Buffer.concat([decipher.update(encryptedBytes), decipher.final()]);\r\n return decode(decrypted, 'utf8');\r\n } catch (error) {\r\n return $err({ message: 'Failed to decrypt data with Crypto NodeJS', description: $stringifyError(error) });\r\n }\r\n}\r\n\r\nexport function encryptObj(data: Record<string, unknown>, secretKey: NodeKey): Result<string> {\r\n const { result, error } = stringifyObj(data);\r\n if (error) return $err(error);\r\n return encrypt(result, secretKey);\r\n}\r\n\r\nexport function decryptObj(encrypted: string, secretKey: NodeKey): Result<{ result: Record<string, unknown> }> {\r\n const { result, error } = decrypt(encrypted, secretKey);\r\n if (error) return $err(error);\r\n return parseToObj(result);\r\n}\r\n"]}
|