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