scryptenc_wasm/
encrypt.rs

1// SPDX-FileCopyrightText: 2022 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! Encrypts to the scrypt encrypted data format.
6
7use scryptenc::scrypt::Params;
8use wasm_bindgen::{JsError, prelude::wasm_bindgen};
9
10/// Encrypts `plaintext` and into a newly allocated `Uint8Array`.
11///
12/// This uses the recommended scrypt parameters according to the OWASP Password
13/// Storage Cheat Sheet.
14#[must_use]
15#[wasm_bindgen]
16pub fn encrypt(plaintext: &[u8], passphrase: &[u8]) -> Vec<u8> {
17    scryptenc::encrypt(plaintext, passphrase)
18}
19
20#[allow(clippy::module_name_repetitions)]
21/// Encrypts `plaintext` with the specified scrypt parameters and into a newly
22/// allocated `Uint8Array`.
23///
24/// # Errors
25///
26/// Returns an error if the scrypt parameters is invalid.
27#[wasm_bindgen(js_name = encryptWithParams)]
28pub fn encrypt_with_params(
29    plaintext: &[u8],
30    passphrase: &[u8],
31    log_n: u8,
32    r: u32,
33    p: u32,
34) -> Result<Vec<u8>, JsError> {
35    let params = Params::new(log_n, r, p, Params::RECOMMENDED_LEN)?;
36    Ok(scryptenc::encrypt_with_params(
37        plaintext, passphrase, params,
38    ))
39}