UNPKG

797 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { u8aConcat } from '@polkadot/util';
4import { naclEncrypt } from "../nacl/index.js";
5import { scryptEncode, scryptToU8a } from "../scrypt/index.js";
6import { jsonEncryptFormat } from "./encryptFormat.js";
7export 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