scryptenc_wasm/
params.rs

1// SPDX-FileCopyrightText: 2023 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! The scrypt parameters.
6
7use wasm_bindgen::{JsError, prelude::wasm_bindgen};
8
9/// The scrypt parameters used for the encrypted data.
10#[derive(Clone, Copy, Debug)]
11#[wasm_bindgen]
12pub struct Params(scryptenc::Params);
13
14#[wasm_bindgen]
15impl Params {
16    /// Creates a new instance of the scrypt parameters from `ciphertext`.
17    ///
18    /// # Errors
19    ///
20    /// Returns an error if any of the following are true:
21    ///
22    /// - `ciphertext` is shorter than 128 bytes.
23    /// - The magic number is invalid.
24    /// - The version number is the unrecognized scrypt version number.
25    /// - The scrypt parameters are invalid.
26    #[wasm_bindgen(constructor)]
27    pub fn new(ciphertext: &[u8]) -> Result<Self, JsError> {
28        scryptenc::Params::new(ciphertext)
29            .map(Self)
30            .map_err(JsError::from)
31    }
32
33    #[allow(clippy::missing_const_for_fn)]
34    /// Gets log<sub>2</sub> of the scrypt parameter `N`.
35    #[must_use]
36    #[wasm_bindgen(js_name = logN, getter)]
37    pub fn log_n(&self) -> u8 {
38        self.0.log_n()
39    }
40
41    #[allow(clippy::missing_const_for_fn)]
42    /// Gets `N` parameter.
43    #[must_use]
44    #[wasm_bindgen(getter)]
45    pub fn n(&self) -> u64 {
46        self.0.n()
47    }
48
49    #[allow(clippy::missing_const_for_fn)]
50    /// Gets `r` parameter.
51    #[must_use]
52    #[wasm_bindgen(getter)]
53    pub fn r(&self) -> u32 {
54        self.0.r()
55    }
56
57    #[allow(clippy::missing_const_for_fn)]
58    /// Gets `p` parameter.
59    #[must_use]
60    #[wasm_bindgen(getter)]
61    pub fn p(&self) -> u32 {
62        self.0.p()
63    }
64}