@powerlines/plugin-crypto 0.10.108 → 0.10.110
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/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/components/crypto.cjs +147 -0
- package/dist/components/crypto.d.cts +13 -0
- package/dist/components/crypto.d.mts +13 -0
- package/dist/components/crypto.mjs +147 -0
- package/dist/components/index.cjs +1 -0
- package/dist/components/index.d.cts +1 -0
- package/dist/components/index.d.mts +1 -0
- package/dist/components/index.mjs +3 -0
- package/dist/deepkit/schemas/reflection.cjs +3998 -0
- package/dist/deepkit/schemas/reflection.d.cts +1560 -0
- package/dist/deepkit/schemas/reflection.d.mts +1560 -0
- package/dist/deepkit/schemas/reflection.mjs +3996 -0
- package/dist/deepkit/schemas/reflection2.cjs +4112 -0
- package/dist/deepkit/schemas/reflection2.mjs +4110 -0
- package/dist/deepkit/src/capnp.cjs +913 -0
- package/dist/deepkit/src/capnp.mjs +911 -0
- package/dist/deepkit/src/esbuild-plugin.cjs +47 -0
- package/dist/deepkit/src/esbuild-plugin.mjs +46 -0
- package/dist/deepkit/src/reflect-type.cjs +22 -0
- package/dist/deepkit/src/reflect-type.mjs +20 -0
- package/dist/deepkit/src/resolve-reflections.cjs +16 -0
- package/dist/deepkit/src/resolve-reflections.mjs +15 -0
- package/dist/deepkit/src/transformer.cjs +52 -0
- package/dist/deepkit/src/transformer.mjs +49 -0
- package/dist/deepkit/src/transpile.cjs +29 -0
- package/dist/deepkit/src/transpile.mjs +27 -0
- package/dist/deepkit/src/types.d.cts +10 -0
- package/dist/deepkit/src/types.d.mts +10 -0
- package/dist/deepkit/src/utilities.cjs +66 -0
- package/dist/deepkit/src/utilities.mjs +65 -0
- package/dist/index.cjs +8 -12056
- package/dist/index.d.cts +5 -12
- package/dist/index.d.mts +5 -12
- package/dist/index.mjs +3 -12018
- package/dist/plugin-alloy/src/core/components/output.cjs +38 -0
- package/dist/plugin-alloy/src/core/components/output.mjs +37 -0
- package/dist/plugin-alloy/src/core/components/single-line-comment.cjs +20 -0
- package/dist/plugin-alloy/src/core/components/single-line-comment.mjs +19 -0
- package/dist/plugin-alloy/src/core/components/source-file.cjs +60 -0
- package/dist/plugin-alloy/src/core/components/source-file.mjs +58 -0
- package/dist/plugin-alloy/src/core/contexts/context.cjs +60 -0
- package/dist/plugin-alloy/src/core/contexts/context.mjs +54 -0
- package/dist/plugin-alloy/src/core/contexts/index.cjs +2 -0
- package/dist/plugin-alloy/src/core/contexts/index.mjs +4 -0
- package/dist/plugin-alloy/src/core/contexts/reflection.cjs +46 -0
- package/dist/plugin-alloy/src/core/contexts/reflection.mjs +42 -0
- package/dist/plugin-alloy/src/helpers/refkey.cjs +16 -0
- package/dist/plugin-alloy/src/helpers/refkey.mjs +15 -0
- package/dist/plugin-alloy/src/index.cjs +96 -0
- package/dist/plugin-alloy/src/index.mjs +94 -0
- package/dist/plugin-alloy/src/markdown/components/markdown-file.cjs +7 -0
- package/dist/plugin-alloy/src/markdown/components/markdown-file.mjs +9 -0
- package/dist/plugin-alloy/src/markdown/components/markdown-table.cjs +5 -0
- package/dist/plugin-alloy/src/markdown/components/markdown-table.mjs +7 -0
- package/dist/plugin-alloy/src/markdown/contexts/markdown-table.cjs +17 -0
- package/dist/plugin-alloy/src/markdown/contexts/markdown-table.mjs +17 -0
- package/dist/plugin-alloy/src/types/components.d.mts +3 -0
- package/dist/plugin-alloy/src/types/index.d.mts +2 -0
- package/dist/plugin-alloy/src/types/plugin.d.cts +37 -0
- package/dist/plugin-alloy/src/types/plugin.d.mts +37 -0
- package/dist/plugin-alloy/src/typescript/components/builtin-file.cjs +47 -0
- package/dist/plugin-alloy/src/typescript/components/builtin-file.mjs +46 -0
- package/dist/plugin-alloy/src/typescript/components/tsdoc-reflection.cjs +75 -0
- package/dist/plugin-alloy/src/typescript/components/tsdoc-reflection.mjs +73 -0
- package/dist/plugin-alloy/src/typescript/components/tsdoc.cjs +359 -0
- package/dist/plugin-alloy/src/typescript/components/tsdoc.mjs +350 -0
- package/dist/plugin-alloy/src/typescript/components/typescript-file.cjs +143 -0
- package/dist/plugin-alloy/src/typescript/components/typescript-file.mjs +140 -0
- package/dist/plugin-alloy/src/typescript/components/typescript-interface.cjs +53 -0
- package/dist/plugin-alloy/src/typescript/components/typescript-interface.mjs +52 -0
- package/dist/plugin-alloy/src/typescript/components/typescript-object.cjs +94 -0
- package/dist/plugin-alloy/src/typescript/components/typescript-object.mjs +93 -0
- package/dist/plugin-automd/src/index.cjs +101 -0
- package/dist/plugin-automd/src/index.mjs +98 -0
- package/dist/plugin-automd/src/types/plugin.d.cts +38 -0
- package/dist/plugin-automd/src/types/plugin.d.mts +40 -0
- package/dist/plugin-automd/src/types/toc.d.cts +33 -0
- package/dist/plugin-automd/src/types/toc.d.mts +33 -0
- package/dist/plugin-babel/src/helpers/ast-utils.cjs +10 -0
- package/dist/plugin-babel/src/helpers/ast-utils.mjs +9 -0
- package/dist/plugin-babel/src/helpers/create-plugin.cjs +41 -0
- package/dist/plugin-babel/src/helpers/create-plugin.mjs +39 -0
- package/dist/plugin-babel/src/helpers/filters.cjs +23 -0
- package/dist/plugin-babel/src/helpers/filters.mjs +21 -0
- package/dist/plugin-babel/src/helpers/index.cjs +5 -0
- package/dist/plugin-babel/src/helpers/index.mjs +7 -0
- package/dist/plugin-babel/src/helpers/module-helpers.cjs +38 -0
- package/dist/plugin-babel/src/helpers/module-helpers.mjs +35 -0
- package/dist/plugin-babel/src/helpers/options.cjs +50 -0
- package/dist/plugin-babel/src/helpers/options.mjs +47 -0
- package/dist/plugin-babel/src/index.cjs +91 -0
- package/dist/plugin-babel/src/index.mjs +89 -0
- package/dist/plugin-babel/src/types/index.d.mts +1 -0
- package/dist/plugin-babel/src/types/plugin.d.cts +15 -0
- package/dist/plugin-babel/src/types/plugin.d.mts +15 -0
- package/dist/plugin-env/src/babel/index.cjs +1 -0
- package/dist/plugin-env/src/babel/index.mjs +3 -0
- package/dist/plugin-env/src/babel/plugin.cjs +121 -0
- package/dist/plugin-env/src/babel/plugin.mjs +119 -0
- package/dist/plugin-env/src/components/docs.cjs +9 -0
- package/dist/plugin-env/src/components/docs.mjs +11 -0
- package/dist/plugin-env/src/components/env.cjs +487 -0
- package/dist/plugin-env/src/components/env.mjs +484 -0
- package/dist/plugin-env/src/components/index.cjs +2 -0
- package/dist/plugin-env/src/components/index.mjs +4 -0
- package/dist/plugin-env/src/helpers/automd-generator.cjs +22 -0
- package/dist/plugin-env/src/helpers/automd-generator.mjs +21 -0
- package/dist/plugin-env/src/helpers/create-reflection-resource.cjs +55 -0
- package/dist/plugin-env/src/helpers/create-reflection-resource.mjs +54 -0
- package/dist/plugin-env/src/helpers/index.cjs +6 -0
- package/dist/plugin-env/src/helpers/index.mjs +8 -0
- package/dist/plugin-env/src/helpers/load.cjs +83 -0
- package/dist/plugin-env/src/helpers/load.mjs +80 -0
- package/dist/plugin-env/src/helpers/persistence.cjs +199 -0
- package/dist/plugin-env/src/helpers/persistence.mjs +188 -0
- package/dist/plugin-env/src/helpers/reflect.cjs +125 -0
- package/dist/plugin-env/src/helpers/reflect.mjs +117 -0
- package/dist/plugin-env/src/helpers/source-file-env.cjs +24 -0
- package/dist/plugin-env/src/helpers/source-file-env.mjs +23 -0
- package/dist/plugin-env/src/helpers/template-helpers.cjs +2 -0
- package/dist/plugin-env/src/helpers/template-helpers.mjs +4 -0
- package/dist/plugin-env/src/index.cjs +172 -0
- package/dist/plugin-env/src/index.mjs +170 -0
- package/dist/plugin-env/src/types/plugin.d.cts +151 -0
- package/dist/plugin-env/src/types/plugin.d.mts +153 -0
- package/dist/plugin-env/src/types/runtime.d.cts +792 -0
- package/dist/plugin-env/src/types/runtime.d.mts +792 -0
- package/dist/powerlines/src/lib/build/esbuild.cjs +102 -0
- package/dist/powerlines/src/lib/build/esbuild.mjs +100 -0
- package/dist/powerlines/src/lib/entry.cjs +12 -0
- package/dist/powerlines/src/lib/entry.mjs +14 -0
- package/dist/powerlines/src/lib/logger.cjs +41 -0
- package/dist/powerlines/src/lib/logger.mjs +39 -0
- package/dist/powerlines/src/lib/utilities/bundle.cjs +35 -0
- package/dist/powerlines/src/lib/utilities/bundle.mjs +34 -0
- package/dist/powerlines/src/lib/utilities/file-header.cjs +22 -0
- package/dist/powerlines/src/lib/utilities/file-header.mjs +21 -0
- package/dist/powerlines/src/lib/utilities/resolve.cjs +30 -0
- package/dist/powerlines/src/lib/utilities/resolve.mjs +29 -0
- package/dist/powerlines/src/plugin-utils/paths.cjs +1 -0
- package/dist/powerlines/src/plugin-utils/paths.mjs +3 -0
- package/dist/powerlines/src/types/babel.d.cts +22 -0
- package/dist/powerlines/src/types/babel.d.mts +22 -0
- package/dist/powerlines/src/types/build.d.cts +145 -0
- package/dist/powerlines/src/types/build.d.mts +147 -0
- package/dist/powerlines/src/types/commands.d.cts +8 -0
- package/dist/powerlines/src/types/commands.d.mts +9 -0
- package/dist/powerlines/src/types/config.d.cts +387 -0
- package/dist/powerlines/src/types/config.d.mts +388 -0
- package/dist/powerlines/src/types/context.d.cts +414 -0
- package/dist/powerlines/src/types/context.d.mts +416 -0
- package/dist/powerlines/src/types/fs.d.cts +486 -0
- package/dist/powerlines/src/types/fs.d.mts +486 -0
- package/dist/powerlines/src/types/hooks.d.mts +2 -0
- package/dist/powerlines/src/types/plugin.d.cts +231 -0
- package/dist/powerlines/src/types/plugin.d.mts +231 -0
- package/dist/powerlines/src/types/resolved.d.cts +82 -0
- package/dist/powerlines/src/types/resolved.d.mts +83 -0
- package/dist/powerlines/src/types/tsconfig.d.cts +69 -0
- package/dist/powerlines/src/types/tsconfig.d.mts +69 -0
- package/dist/types/index.cjs +0 -2
- package/dist/types/index.d.cts +1 -2
- package/dist/types/index.d.mts +1 -2
- package/dist/types/index.mjs +0 -3
- package/dist/types/plugin.cjs +0 -1
- package/dist/types/plugin.d.cts +41 -1
- package/dist/types/plugin.d.mts +41 -1
- package/dist/types/plugin.mjs +0 -2
- package/package.json +5 -5
- package/dist/index-BgAdqTbb.d.mts +0 -1
- package/dist/index-CEgs-Dz2.d.cts +0 -1
- package/dist/plugin-C3MaN5jp.mjs +0 -1
- package/dist/plugin-DHXHjv16.cjs +0 -0
- package/dist/plugin-Dhgjf-aI.d.mts +0 -4441
- package/dist/plugin-Dw1wCT6b.d.cts +0 -4438
- package/dist/types-CTUnla4x.mjs +0 -1
- package/dist/types-DHkg7xmX.cjs +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
const require_file_header = require('../powerlines/src/lib/utilities/file-header.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/components/crypto.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generates the crypto module content.
|
|
6
|
+
*
|
|
7
|
+
* @param context - The build context containing runtime information.
|
|
8
|
+
* @returns A string representing the crypto module code.
|
|
9
|
+
*/
|
|
10
|
+
function cryptoModule(context) {
|
|
11
|
+
return `
|
|
12
|
+
/**
|
|
13
|
+
* The cryptography module provides custom helper functions to support encrypting and decrypting data.
|
|
14
|
+
*
|
|
15
|
+
* @module ${context.config.output.builtinPrefix}:crypto
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
${require_file_header.getFileHeader(context)}
|
|
19
|
+
|
|
20
|
+
import { xchacha20poly1305, chacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
21
|
+
import { randomBytes, managedNonce, hexToBytes } from "@noble/ciphers/utils.js";
|
|
22
|
+
import { scrypt } from "@noble/hashes/scrypt.js";
|
|
23
|
+
import { blake3 } from "@noble/hashes/blake3.js";
|
|
24
|
+
${context.config.crypto.encryptionKey ? `
|
|
25
|
+
const nonce = randomBytes(24);
|
|
26
|
+
const chacha = xchacha20poly1305(hexToBytes("${context.config.crypto.encryptionKey}"), nonce);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
|
|
30
|
+
*
|
|
31
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
32
|
+
*
|
|
33
|
+
* @param plaintext - The data to encrypt.
|
|
34
|
+
* @returns The encrypted data.
|
|
35
|
+
*/
|
|
36
|
+
export function encrypt(plaintext: string): string {
|
|
37
|
+
return chacha.encrypt(
|
|
38
|
+
nonce,
|
|
39
|
+
new TextEncoder().encode(plaintext),
|
|
40
|
+
null
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
|
|
46
|
+
*
|
|
47
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
48
|
+
*
|
|
49
|
+
* @param encrypted - The encrypted data to decrypt.
|
|
50
|
+
* @returns The decrypted data.
|
|
51
|
+
*/
|
|
52
|
+
export function decrypt(encrypted: string): string {
|
|
53
|
+
const decrypted = chacha.decrypt(
|
|
54
|
+
nonce,
|
|
55
|
+
encrypted,
|
|
56
|
+
null
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return new TextDecoder().decode(decrypted);
|
|
60
|
+
}
|
|
61
|
+
` : ""}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher with a password.
|
|
65
|
+
*
|
|
66
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
67
|
+
*
|
|
68
|
+
* @param password - The password used to derive the encryption key.
|
|
69
|
+
* @param plaintext - The data to encrypt.
|
|
70
|
+
* @returns The encrypted data.
|
|
71
|
+
*/
|
|
72
|
+
export function encryptWithPassword(password: string, plaintext: string): string {
|
|
73
|
+
const key = scrypt(
|
|
74
|
+
new TextEncoder().encode(password),
|
|
75
|
+
hexToBytes("${context.config.crypto.salt ? context.config.crypto.salt : "nonce"}"),
|
|
76
|
+
1048576, // requires 1GB of RAM to calculate
|
|
77
|
+
8,
|
|
78
|
+
1,
|
|
79
|
+
32
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
return chacha20poly1305(key).encrypt(
|
|
83
|
+
nonce,
|
|
84
|
+
new TextEncoder().encode(plaintext),
|
|
85
|
+
null
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher with a password.
|
|
91
|
+
*
|
|
92
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
93
|
+
*
|
|
94
|
+
* @param password - The password used to derive the decryption key.
|
|
95
|
+
* @param encrypted - The encrypted data to decrypt.
|
|
96
|
+
* @returns The decrypted data.
|
|
97
|
+
*/
|
|
98
|
+
export function decryptWithPassword(password: string, encrypted: string): string {
|
|
99
|
+
const key = scrypt(
|
|
100
|
+
new TextEncoder().encode(password),
|
|
101
|
+
hexToBytes("${context.config.crypto.salt ? context.config.crypto.salt : "nonce"}"),
|
|
102
|
+
1048576, // requires 1GB of RAM to calculate
|
|
103
|
+
8,
|
|
104
|
+
1,
|
|
105
|
+
32
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const decrypted = chacha20poly1305(key).decrypt(
|
|
109
|
+
nonce,
|
|
110
|
+
encrypted,
|
|
111
|
+
null
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return new TextDecoder().decode(decrypted);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Hashes data using the [BLAKE3](https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3) hash function.
|
|
119
|
+
*
|
|
120
|
+
* @see https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3
|
|
121
|
+
*
|
|
122
|
+
* @param data - The data to hash.
|
|
123
|
+
* @returns The hashed data.
|
|
124
|
+
*/
|
|
125
|
+
export function hash(data: string): string {
|
|
126
|
+
return Buffer.from(
|
|
127
|
+
blake3(new TextEncoder().encode(data), {
|
|
128
|
+
key: ${context.config.crypto.salt ? `hexToBytes("${context.config.crypto.salt}")` : "new TextEncoder().encode(\"powerlines\")"})
|
|
129
|
+
})
|
|
130
|
+
).toString("hex");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Export noble cipher and hash functions for advanced usage
|
|
134
|
+
|
|
135
|
+
export * from "@noble/ciphers/chacha.js";
|
|
136
|
+
export * from "@noble/ciphers/aes.js";
|
|
137
|
+
export * from "@noble/ciphers/utils.js";
|
|
138
|
+
export * from '@noble/hashes/blake3.js';
|
|
139
|
+
export * from '@noble/hashes/pbkdf2.js';
|
|
140
|
+
export * from '@noble/hashes/scrypt.js';
|
|
141
|
+
export * from '@noble/hashes/utils.js';
|
|
142
|
+
|
|
143
|
+
`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
exports.cryptoModule = cryptoModule;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CryptoPluginContext } from "../types/plugin.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/components/crypto.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generates the crypto module content.
|
|
7
|
+
*
|
|
8
|
+
* @param context - The build context containing runtime information.
|
|
9
|
+
* @returns A string representing the crypto module code.
|
|
10
|
+
*/
|
|
11
|
+
declare function cryptoModule(context: CryptoPluginContext): string;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { cryptoModule };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CryptoPluginContext } from "../types/plugin.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/components/crypto.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generates the crypto module content.
|
|
7
|
+
*
|
|
8
|
+
* @param context - The build context containing runtime information.
|
|
9
|
+
* @returns A string representing the crypto module code.
|
|
10
|
+
*/
|
|
11
|
+
declare function cryptoModule(context: CryptoPluginContext): string;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { cryptoModule };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { getFileHeader } from "../powerlines/src/lib/utilities/file-header.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/components/crypto.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generates the crypto module content.
|
|
6
|
+
*
|
|
7
|
+
* @param context - The build context containing runtime information.
|
|
8
|
+
* @returns A string representing the crypto module code.
|
|
9
|
+
*/
|
|
10
|
+
function cryptoModule(context) {
|
|
11
|
+
return `
|
|
12
|
+
/**
|
|
13
|
+
* The cryptography module provides custom helper functions to support encrypting and decrypting data.
|
|
14
|
+
*
|
|
15
|
+
* @module ${context.config.output.builtinPrefix}:crypto
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
${getFileHeader(context)}
|
|
19
|
+
|
|
20
|
+
import { xchacha20poly1305, chacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
21
|
+
import { randomBytes, managedNonce, hexToBytes } from "@noble/ciphers/utils.js";
|
|
22
|
+
import { scrypt } from "@noble/hashes/scrypt.js";
|
|
23
|
+
import { blake3 } from "@noble/hashes/blake3.js";
|
|
24
|
+
${context.config.crypto.encryptionKey ? `
|
|
25
|
+
const nonce = randomBytes(24);
|
|
26
|
+
const chacha = xchacha20poly1305(hexToBytes("${context.config.crypto.encryptionKey}"), nonce);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
|
|
30
|
+
*
|
|
31
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
32
|
+
*
|
|
33
|
+
* @param plaintext - The data to encrypt.
|
|
34
|
+
* @returns The encrypted data.
|
|
35
|
+
*/
|
|
36
|
+
export function encrypt(plaintext: string): string {
|
|
37
|
+
return chacha.encrypt(
|
|
38
|
+
nonce,
|
|
39
|
+
new TextEncoder().encode(plaintext),
|
|
40
|
+
null
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
|
|
46
|
+
*
|
|
47
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
48
|
+
*
|
|
49
|
+
* @param encrypted - The encrypted data to decrypt.
|
|
50
|
+
* @returns The decrypted data.
|
|
51
|
+
*/
|
|
52
|
+
export function decrypt(encrypted: string): string {
|
|
53
|
+
const decrypted = chacha.decrypt(
|
|
54
|
+
nonce,
|
|
55
|
+
encrypted,
|
|
56
|
+
null
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return new TextDecoder().decode(decrypted);
|
|
60
|
+
}
|
|
61
|
+
` : ""}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher with a password.
|
|
65
|
+
*
|
|
66
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
67
|
+
*
|
|
68
|
+
* @param password - The password used to derive the encryption key.
|
|
69
|
+
* @param plaintext - The data to encrypt.
|
|
70
|
+
* @returns The encrypted data.
|
|
71
|
+
*/
|
|
72
|
+
export function encryptWithPassword(password: string, plaintext: string): string {
|
|
73
|
+
const key = scrypt(
|
|
74
|
+
new TextEncoder().encode(password),
|
|
75
|
+
hexToBytes("${context.config.crypto.salt ? context.config.crypto.salt : "nonce"}"),
|
|
76
|
+
1048576, // requires 1GB of RAM to calculate
|
|
77
|
+
8,
|
|
78
|
+
1,
|
|
79
|
+
32
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
return chacha20poly1305(key).encrypt(
|
|
83
|
+
nonce,
|
|
84
|
+
new TextEncoder().encode(plaintext),
|
|
85
|
+
null
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher with a password.
|
|
91
|
+
*
|
|
92
|
+
* @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
|
|
93
|
+
*
|
|
94
|
+
* @param password - The password used to derive the decryption key.
|
|
95
|
+
* @param encrypted - The encrypted data to decrypt.
|
|
96
|
+
* @returns The decrypted data.
|
|
97
|
+
*/
|
|
98
|
+
export function decryptWithPassword(password: string, encrypted: string): string {
|
|
99
|
+
const key = scrypt(
|
|
100
|
+
new TextEncoder().encode(password),
|
|
101
|
+
hexToBytes("${context.config.crypto.salt ? context.config.crypto.salt : "nonce"}"),
|
|
102
|
+
1048576, // requires 1GB of RAM to calculate
|
|
103
|
+
8,
|
|
104
|
+
1,
|
|
105
|
+
32
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const decrypted = chacha20poly1305(key).decrypt(
|
|
109
|
+
nonce,
|
|
110
|
+
encrypted,
|
|
111
|
+
null
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return new TextDecoder().decode(decrypted);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Hashes data using the [BLAKE3](https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3) hash function.
|
|
119
|
+
*
|
|
120
|
+
* @see https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3
|
|
121
|
+
*
|
|
122
|
+
* @param data - The data to hash.
|
|
123
|
+
* @returns The hashed data.
|
|
124
|
+
*/
|
|
125
|
+
export function hash(data: string): string {
|
|
126
|
+
return Buffer.from(
|
|
127
|
+
blake3(new TextEncoder().encode(data), {
|
|
128
|
+
key: ${context.config.crypto.salt ? `hexToBytes("${context.config.crypto.salt}")` : "new TextEncoder().encode(\"powerlines\")"})
|
|
129
|
+
})
|
|
130
|
+
).toString("hex");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Export noble cipher and hash functions for advanced usage
|
|
134
|
+
|
|
135
|
+
export * from "@noble/ciphers/chacha.js";
|
|
136
|
+
export * from "@noble/ciphers/aes.js";
|
|
137
|
+
export * from "@noble/ciphers/utils.js";
|
|
138
|
+
export * from '@noble/hashes/blake3.js';
|
|
139
|
+
export * from '@noble/hashes/pbkdf2.js';
|
|
140
|
+
export * from '@noble/hashes/scrypt.js';
|
|
141
|
+
export * from '@noble/hashes/utils.js';
|
|
142
|
+
|
|
143
|
+
`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
export { cryptoModule };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const require_crypto = require('./crypto.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { cryptoModule } from "./crypto.cjs";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { cryptoModule } from "./crypto.mjs";
|