UNPKG

657 BJavaScriptView Raw
1import { randomAsU8a } from '../random/asU8a.js';
2import { naclSecretbox } from './tweetnacl.js';
3/**
4 * @name naclEncrypt
5 * @summary Encrypts a message using the supplied secretKey and nonce
6 * @description
7 * Returns an encrypted message, using the `secretKey` and `nonce`. If the `nonce` was not supplied, a random value is generated.
8 * @example
9 * <BR>
10 *
11 * ```javascript
12 * import { naclEncrypt } from '@polkadot/util-crypto';
13 *
14 * naclEncrypt([...], [...]); // => [...]
15 * ```
16 */
17export function naclEncrypt(message, secret, nonce = randomAsU8a(24)) {
18 return {
19 encrypted: naclSecretbox(message, nonce, secret),
20 nonce
21 };
22}