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