UNPKG

735 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import nacl from 'tweetnacl';
4import { randomAsU8a } from "../random/asU8a.js";
5
6/**
7 * @name naclEncrypt
8 * @summary Encrypts a message using the supplied secretKey and nonce
9 * @description
10 * Returns an encrypted message, using the `secretKey` and `nonce`. If the `nonce` was not supplied, a random value is generated.
11 * @example
12 * <BR>
13 *
14 * ```javascript
15 * import { naclEncrypt } from '@polkadot/util-crypto';
16 *
17 * naclEncrypt([...], [...]); // => [...]
18 * ```
19 */
20export function naclEncrypt(message, secret, nonce = randomAsU8a(24)) {
21 return {
22 encrypted: nacl.secretbox(message, nonce, secret),
23 nonce
24 };
25}
\No newline at end of file