UNPKG

828 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 naclSeal
8 * @summary Seals a message using the sender's encrypting secretKey, receiver's public key, and nonce
9 * @description
10 * Returns an encrypted message which can be open only by receiver's secretKey. If the `nonce` was not supplied, a random value is generated.
11 * @example
12 * <BR>
13 *
14 * ```javascript
15 * import { naclSeal } from '@polkadot/util-crypto';
16 *
17 * naclSeal([...], [...], [...], [...]); // => [...]
18 * ```
19 */
20export function naclSeal(message, senderBoxSecret, receiverBoxPublic, nonce = randomAsU8a(24)) {
21 return {
22 nonce,
23 sealed: nacl.box(message, nonce, receiverBoxPublic, senderBoxSecret)
24 };
25}
\No newline at end of file