1 | import { u8aConcat } from '@polkadot/util';
|
2 | import { naclEncrypt } from '../nacl/index.js';
|
3 | import { scryptEncode, scryptToU8a } from '../scrypt/index.js';
|
4 | import { jsonEncryptFormat } from './encryptFormat.js';
|
5 | export function jsonEncrypt(data, contentType, passphrase) {
|
6 | let isEncrypted = false;
|
7 | let encoded = data;
|
8 | if (passphrase) {
|
9 | const { params, password, salt } = scryptEncode(passphrase);
|
10 | const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
|
11 | isEncrypted = true;
|
12 | encoded = u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
|
13 | }
|
14 | return jsonEncryptFormat(encoded, contentType, isEncrypted);
|
15 | }
|