1 |
|
2 |
|
3 | import { u8aConcat } from '@polkadot/util';
|
4 | import { naclEncrypt } from "../nacl/index.js";
|
5 | import { scryptEncode, scryptToU8a } from "../scrypt/index.js";
|
6 | import { jsonEncryptFormat } from "./encryptFormat.js";
|
7 | export function jsonEncrypt(data, contentType, passphrase) {
|
8 | let isEncrypted = false;
|
9 | let encoded = data;
|
10 |
|
11 | if (passphrase) {
|
12 | const {
|
13 | params,
|
14 | password,
|
15 | salt
|
16 | } = scryptEncode(passphrase);
|
17 | const {
|
18 | encrypted,
|
19 | nonce
|
20 | } = naclEncrypt(encoded, password.subarray(0, 32));
|
21 | isEncrypted = true;
|
22 | encoded = u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
|
23 | }
|
24 |
|
25 | return jsonEncryptFormat(encoded, contentType, isEncrypted);
|
26 | } |
\ | No newline at end of file |