Skip to content

Commit

Permalink
chore: Remove *with_type
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Dec 10, 2024
1 parent deb852b commit cb3bcd0
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 614 deletions.
2 changes: 1 addition & 1 deletion crates/abcrypt/examples/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn main() -> anyhow::Result<()> {
.interact()
.context("could not read passphrase")?;
let params = Params::new(opt.memory_cost, opt.time_cost, opt.parallelism, None)?;
let ciphertext = abcrypt::encrypt_with_version(
let ciphertext = abcrypt::encrypt_with_context(
plaintext,
passphrase,
opt.argon2_type.into(),
Expand Down
108 changes: 22 additions & 86 deletions crates/abcrypt/src/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ pub struct Encryptor<'m> {
impl<'m> Encryptor<'m> {
/// Creates a new `Encryptor`.
///
/// This uses the [recommended Argon2 parameters] created by
/// [`Params::default`].
/// This uses the recommended Argon2 parameters according to the [OWASP
/// Password Storage Cheat Sheet] created by [`Params::default`]. This also
/// uses the Argon2 type created by [`Algorithm::default`] and the Argon2
/// version created by [`Version::default`].
///
/// # Errors
///
Expand All @@ -41,7 +43,7 @@ impl<'m> Encryptor<'m> {
/// let cipher = Encryptor::new(data, passphrase).unwrap();
/// ```
///
/// [recommended Argon2 parameters]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
/// [OWASP Password Storage Cheat Sheet]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
#[cfg(feature = "alloc")]
#[inline]
pub fn new(plaintext: &'m impl AsRef<[u8]>, passphrase: impl AsRef<[u8]>) -> Result<Self> {
Expand All @@ -50,7 +52,8 @@ impl<'m> Encryptor<'m> {

/// Creates a new `Encryptor` with the specified [`Params`].
///
/// This uses the Argon2 type created by [`Algorithm::default`].
/// This uses the Argon2 type created by [`Algorithm::default`] and the
/// Argon2 version created by [`Version::default`].
///
/// # Errors
///
Expand All @@ -73,43 +76,10 @@ impl<'m> Encryptor<'m> {
passphrase: impl AsRef<[u8]>,
params: Params,
) -> Result<Self> {
Self::with_type(plaintext, passphrase, Algorithm::default(), params)
}

/// Creates a new `Encryptor` with the specified [`Algorithm`] and
/// [`Params`].
///
/// This uses the Argon2 version created by [`Version::default`].
///
/// # Errors
///
/// Returns [`Err`] if the Argon2 context is invalid.
///
/// # Examples
///
/// ```
/// # use abcrypt::{
/// # argon2::{Algorithm, Params},
/// # Encryptor,
/// # };
/// #
/// let data = b"Hello, world!\n";
/// let passphrase = "passphrase";
///
/// let params = Params::new(32, 3, 4, None).unwrap();
/// let cipher = Encryptor::with_type(data, passphrase, Algorithm::Argon2d, params).unwrap();
/// ```
#[inline]
pub fn with_type(
plaintext: &'m impl AsRef<[u8]>,
passphrase: impl AsRef<[u8]>,
argon2_type: Algorithm,
params: Params,
) -> Result<Self> {
Self::with_version(
Self::with_context(
plaintext,
passphrase,
argon2_type,
Algorithm::default(),
Version::default(),
params,
)
Expand All @@ -135,10 +105,10 @@ impl<'m> Encryptor<'m> {
///
/// let params = Params::new(32, 3, 4, None).unwrap();
/// let cipher =
/// Encryptor::with_version(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
/// Encryptor::with_context(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
/// .unwrap();
/// ```
pub fn with_version(
pub fn with_context(
plaintext: &'m impl AsRef<[u8]>,
passphrase: impl AsRef<[u8]>,
argon2_type: Algorithm,
Expand Down Expand Up @@ -283,8 +253,10 @@ impl<'m> Encryptor<'m> {

/// Encrypts `plaintext` and into a newly allocated [`Vec`](alloc::vec::Vec).
///
/// This uses the [recommended Argon2 parameters] created by
/// [`Params::default`].
/// This uses the recommended Argon2 parameters according to the [OWASP Password
/// Storage Cheat Sheet] created by [`Params::default`]. This also uses the
/// Argon2 type created by [`Algorithm::default`] and the Argon2 version created
/// by [`Version::default`].
///
/// This is a convenience function for using [`Encryptor::new`] and
/// [`Encryptor::encrypt_to_vec`].
Expand All @@ -303,7 +275,7 @@ impl<'m> Encryptor<'m> {
/// # assert_ne!(ciphertext, data);
/// ```
///
/// [recommended Argon2 parameters]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
/// [OWASP Password Storage Cheat Sheet]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
#[cfg(feature = "alloc")]
#[inline]
pub fn encrypt(
Expand All @@ -317,7 +289,8 @@ pub fn encrypt(
/// Encrypts `plaintext` with the specified [`Params`] and into a newly
/// allocated [`Vec`](alloc::vec::Vec).
///
/// This uses the Argon2 type created by [`Algorithm::default`].
/// This uses the Argon2 type created by [`Algorithm::default`] and the Argon2
/// version created by [`Version::default`].
///
/// This is a convenience function for using [`Encryptor::with_params`] and
/// [`Encryptor::encrypt_to_vec`].
Expand Down Expand Up @@ -348,48 +321,11 @@ pub fn encrypt_with_params(
Encryptor::with_params(&plaintext, passphrase, params).map(|c| c.encrypt_to_vec())
}

#[allow(clippy::module_name_repetitions)]
/// Encrypts `plaintext` with the specified [`Algorithm`] and [`Params`] and
/// into a newly allocated [`Vec`](alloc::vec::Vec).
///
/// This uses the Argon2 version created by [`Version::default`].
///
/// This is a convenience function for using [`Encryptor::with_type`] and
/// [`Encryptor::encrypt_to_vec`].
///
/// # Errors
///
/// Returns [`Err`] if the Argon2 context is invalid.
///
/// # Examples
///
/// ```
/// # use abcrypt::argon2::{Algorithm, Params};
/// #
/// let data = b"Hello, world!\n";
/// let passphrase = "passphrase";
///
/// let params = Params::new(32, 3, 4, None).unwrap();
/// let ciphertext =
/// abcrypt::encrypt_with_type(data, passphrase, Algorithm::Argon2d, params).unwrap();
/// # assert_ne!(ciphertext, data);
/// ```
#[cfg(feature = "alloc")]
#[inline]
pub fn encrypt_with_type(
plaintext: impl AsRef<[u8]>,
passphrase: impl AsRef<[u8]>,
argon2_type: Algorithm,
params: Params,
) -> Result<alloc::vec::Vec<u8>> {
Encryptor::with_type(&plaintext, passphrase, argon2_type, params).map(|c| c.encrypt_to_vec())
}

#[allow(clippy::module_name_repetitions)]
/// Encrypts `plaintext` with the specified [`Algorithm`], [`Version`] and
/// [`Params`] and into a newly allocated [`Vec`](alloc::vec::Vec).
///
/// This is a convenience function for using [`Encryptor::with_version`] and
/// This is a convenience function for using [`Encryptor::with_context`] and
/// [`Encryptor::encrypt_to_vec`].
///
/// # Errors
Expand All @@ -406,19 +342,19 @@ pub fn encrypt_with_type(
///
/// let params = Params::new(32, 3, 4, None).unwrap();
/// let ciphertext =
/// abcrypt::encrypt_with_version(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
/// abcrypt::encrypt_with_context(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
/// .unwrap();
/// # assert_ne!(ciphertext, data);
/// ```
#[cfg(feature = "alloc")]
#[inline]
pub fn encrypt_with_version(
pub fn encrypt_with_context(
plaintext: impl AsRef<[u8]>,
passphrase: impl AsRef<[u8]>,
argon2_type: Algorithm,
argon2_version: Version,
params: Params,
) -> Result<alloc::vec::Vec<u8>> {
Encryptor::with_version(&plaintext, passphrase, argon2_type, argon2_version, params)
Encryptor::with_context(&plaintext, passphrase, argon2_type, argon2_version, params)
.map(|c| c.encrypt_to_vec())
}
2 changes: 1 addition & 1 deletion crates/abcrypt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub use crate::{
#[cfg(feature = "alloc")]
pub use crate::{
decrypt::decrypt,
encrypt::{encrypt, encrypt_with_params, encrypt_with_type, encrypt_with_version},
encrypt::{encrypt, encrypt_with_context, encrypt_with_params},
};

#[cfg(not(feature = "alloc"))]
Expand Down
77 changes: 12 additions & 65 deletions crates/abcrypt/tests/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,8 @@ fn success_with_params() {
}

#[test]
fn success_with_type() {
let cipher = Encryptor::with_type(
&TEST_DATA,
PASSPHRASE,
Algorithm::Argon2d,
Params::new(32, 3, 4, None).unwrap(),
)
.unwrap();
let mut buf = [u8::default(); TEST_DATA.len() + HEADER_SIZE + TAG_SIZE];
cipher.encrypt(&mut buf);
assert_ne!(buf, TEST_DATA);

let argon2 = Argon2::new(buf).unwrap();
assert_eq!(argon2.variant(), Algorithm::Argon2d);
assert_eq!(argon2.version(), Version::V0x13);

let params = abcrypt::Params::new(buf).unwrap();
assert_eq!(params.memory_cost(), 32);
assert_eq!(params.time_cost(), 3);
assert_eq!(params.parallelism(), 4);

let cipher = Decryptor::new(&buf, PASSPHRASE).unwrap();
let mut buf = [u8::default(); TEST_DATA.len()];
cipher.decrypt(&mut buf).unwrap();
assert_eq!(buf, TEST_DATA);
}

#[test]
fn success_with_version() {
let cipher = Encryptor::with_version(
fn success_with_context() {
let cipher = Encryptor::with_context(
&TEST_DATA,
PASSPHRASE,
Algorithm::Argon2i,
Expand Down Expand Up @@ -177,10 +149,11 @@ fn version() {
#[test]
fn argon2_type() {
{
let cipher = Encryptor::with_type(
let cipher = Encryptor::with_context(
&TEST_DATA,
PASSPHRASE,
Algorithm::Argon2d,
Version::default(),
Params::new(32, 3, 4, None).unwrap(),
)
.unwrap();
Expand All @@ -192,10 +165,11 @@ fn argon2_type() {
assert_eq!(argon2.variant(), Algorithm::Argon2d);
}
{
let cipher = Encryptor::with_type(
let cipher = Encryptor::with_context(
&TEST_DATA,
PASSPHRASE,
Algorithm::Argon2i,
Version::default(),
Params::new(32, 3, 4, None).unwrap(),
)
.unwrap();
Expand All @@ -207,10 +181,11 @@ fn argon2_type() {
assert_eq!(argon2.variant(), Algorithm::Argon2i);
}
{
let cipher = Encryptor::with_type(
let cipher = Encryptor::with_context(
&TEST_DATA,
PASSPHRASE,
Algorithm::Argon2id,
Version::default(),
Params::new(32, 3, 4, None).unwrap(),
)
.unwrap();
Expand All @@ -226,7 +201,7 @@ fn argon2_type() {
#[test]
fn argon2_version() {
{
let cipher = Encryptor::with_version(
let cipher = Encryptor::with_context(
&TEST_DATA,
PASSPHRASE,
Algorithm::default(),
Expand All @@ -242,7 +217,7 @@ fn argon2_version() {
assert_eq!(argon2.version(), Version::V0x10);
}
{
let cipher = Encryptor::with_version(
let cipher = Encryptor::with_context(
&TEST_DATA,
PASSPHRASE,
Algorithm::default(),
Expand Down Expand Up @@ -375,36 +350,8 @@ fn success_convenience_function_with_params() {

#[cfg(feature = "alloc")]
#[test]
fn success_convenience_function_with_type() {
let ciphertext = abcrypt::encrypt_with_type(
TEST_DATA,
PASSPHRASE,
Algorithm::Argon2d,
Params::new(32, 3, 4, None).unwrap(),
)
.unwrap();
assert_ne!(ciphertext, TEST_DATA);
assert_eq!(ciphertext.len(), TEST_DATA.len() + HEADER_SIZE + TAG_SIZE);

let argon2 = Argon2::new(&ciphertext).unwrap();
assert_eq!(argon2.variant(), Algorithm::Argon2d);
assert_eq!(argon2.version(), Version::V0x13);

let params = abcrypt::Params::new(&ciphertext).unwrap();
assert_eq!(params.memory_cost(), 32);
assert_eq!(params.time_cost(), 3);
assert_eq!(params.parallelism(), 4);

let cipher = Decryptor::new(&ciphertext, PASSPHRASE).unwrap();
let mut buf = [u8::default(); TEST_DATA.len()];
cipher.decrypt(&mut buf).unwrap();
assert_eq!(buf, TEST_DATA);
}

#[cfg(feature = "alloc")]
#[test]
fn success_convenience_function_with_version() {
let ciphertext = abcrypt::encrypt_with_version(
fn success_convenience_function_with_context() {
let ciphertext = abcrypt::encrypt_with_context(
TEST_DATA,
PASSPHRASE,
Algorithm::Argon2i,
Expand Down
2 changes: 1 addition & 1 deletion crates/capi/examples/encrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) {

std::vector<std::uint8_t> ciphertext(
plaintext.size() + (ABCRYPT_HEADER_SIZE + ABCRYPT_TAG_SIZE));
auto error_code = abcrypt_encrypt_with_version(
auto error_code = abcrypt_encrypt_with_context(
plaintext.data(), plaintext.size(),
reinterpret_cast<uint8_t *>(passphrase.data()), passphrase.size(),
ciphertext.data(), ciphertext.size(), argon2_type, argon2_version,
Expand Down
Loading

0 comments on commit cb3bcd0

Please sign in to comment.