mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 09:08:03 +00:00
16 lines
676 B
JavaScript
16 lines
676 B
JavaScript
import { u8aConcat } from '@pezkuwi/util';
|
|
import { naclEncrypt } from '../nacl/index.js';
|
|
import { scryptEncode, scryptToU8a } from '../scrypt/index.js';
|
|
import { jsonEncryptFormat } from './encryptFormat.js';
|
|
export function jsonEncrypt(data, contentType, passphrase) {
|
|
let isEncrypted = false;
|
|
let encoded = data;
|
|
if (passphrase) {
|
|
const { params, password, salt } = scryptEncode(passphrase);
|
|
const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
|
|
isEncrypted = true;
|
|
encoded = u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
|
|
}
|
|
return jsonEncryptFormat(encoded, contentType, isEncrypted);
|
|
}
|