Skip to content

Commit cb3bcd0

Browse files
committed
chore: Remove *with_type
1 parent deb852b commit cb3bcd0

26 files changed

+126
-614
lines changed

crates/abcrypt/examples/encrypt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn main() -> anyhow::Result<()> {
124124
.interact()
125125
.context("could not read passphrase")?;
126126
let params = Params::new(opt.memory_cost, opt.time_cost, opt.parallelism, None)?;
127-
let ciphertext = abcrypt::encrypt_with_version(
127+
let ciphertext = abcrypt::encrypt_with_context(
128128
plaintext,
129129
passphrase,
130130
opt.argon2_type.into(),

crates/abcrypt/src/encrypt.rs

Lines changed: 22 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ pub struct Encryptor<'m> {
2323
impl<'m> Encryptor<'m> {
2424
/// Creates a new `Encryptor`.
2525
///
26-
/// This uses the [recommended Argon2 parameters] created by
27-
/// [`Params::default`].
26+
/// This uses the recommended Argon2 parameters according to the [OWASP
27+
/// Password Storage Cheat Sheet] created by [`Params::default`]. This also
28+
/// uses the Argon2 type created by [`Algorithm::default`] and the Argon2
29+
/// version created by [`Version::default`].
2830
///
2931
/// # Errors
3032
///
@@ -41,7 +43,7 @@ impl<'m> Encryptor<'m> {
4143
/// let cipher = Encryptor::new(data, passphrase).unwrap();
4244
/// ```
4345
///
44-
/// [recommended Argon2 parameters]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
46+
/// [OWASP Password Storage Cheat Sheet]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
4547
#[cfg(feature = "alloc")]
4648
#[inline]
4749
pub fn new(plaintext: &'m impl AsRef<[u8]>, passphrase: impl AsRef<[u8]>) -> Result<Self> {
@@ -50,7 +52,8 @@ impl<'m> Encryptor<'m> {
5052

5153
/// Creates a new `Encryptor` with the specified [`Params`].
5254
///
53-
/// This uses the Argon2 type created by [`Algorithm::default`].
55+
/// This uses the Argon2 type created by [`Algorithm::default`] and the
56+
/// Argon2 version created by [`Version::default`].
5457
///
5558
/// # Errors
5659
///
@@ -73,43 +76,10 @@ impl<'m> Encryptor<'m> {
7376
passphrase: impl AsRef<[u8]>,
7477
params: Params,
7578
) -> Result<Self> {
76-
Self::with_type(plaintext, passphrase, Algorithm::default(), params)
77-
}
78-
79-
/// Creates a new `Encryptor` with the specified [`Algorithm`] and
80-
/// [`Params`].
81-
///
82-
/// This uses the Argon2 version created by [`Version::default`].
83-
///
84-
/// # Errors
85-
///
86-
/// Returns [`Err`] if the Argon2 context is invalid.
87-
///
88-
/// # Examples
89-
///
90-
/// ```
91-
/// # use abcrypt::{
92-
/// # argon2::{Algorithm, Params},
93-
/// # Encryptor,
94-
/// # };
95-
/// #
96-
/// let data = b"Hello, world!\n";
97-
/// let passphrase = "passphrase";
98-
///
99-
/// let params = Params::new(32, 3, 4, None).unwrap();
100-
/// let cipher = Encryptor::with_type(data, passphrase, Algorithm::Argon2d, params).unwrap();
101-
/// ```
102-
#[inline]
103-
pub fn with_type(
104-
plaintext: &'m impl AsRef<[u8]>,
105-
passphrase: impl AsRef<[u8]>,
106-
argon2_type: Algorithm,
107-
params: Params,
108-
) -> Result<Self> {
109-
Self::with_version(
79+
Self::with_context(
11080
plaintext,
11181
passphrase,
112-
argon2_type,
82+
Algorithm::default(),
11383
Version::default(),
11484
params,
11585
)
@@ -135,10 +105,10 @@ impl<'m> Encryptor<'m> {
135105
///
136106
/// let params = Params::new(32, 3, 4, None).unwrap();
137107
/// let cipher =
138-
/// Encryptor::with_version(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
108+
/// Encryptor::with_context(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
139109
/// .unwrap();
140110
/// ```
141-
pub fn with_version(
111+
pub fn with_context(
142112
plaintext: &'m impl AsRef<[u8]>,
143113
passphrase: impl AsRef<[u8]>,
144114
argon2_type: Algorithm,
@@ -283,8 +253,10 @@ impl<'m> Encryptor<'m> {
283253

284254
/// Encrypts `plaintext` and into a newly allocated [`Vec`](alloc::vec::Vec).
285255
///
286-
/// This uses the [recommended Argon2 parameters] created by
287-
/// [`Params::default`].
256+
/// This uses the recommended Argon2 parameters according to the [OWASP Password
257+
/// Storage Cheat Sheet] created by [`Params::default`]. This also uses the
258+
/// Argon2 type created by [`Algorithm::default`] and the Argon2 version created
259+
/// by [`Version::default`].
288260
///
289261
/// This is a convenience function for using [`Encryptor::new`] and
290262
/// [`Encryptor::encrypt_to_vec`].
@@ -303,7 +275,7 @@ impl<'m> Encryptor<'m> {
303275
/// # assert_ne!(ciphertext, data);
304276
/// ```
305277
///
306-
/// [recommended Argon2 parameters]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
278+
/// [OWASP Password Storage Cheat Sheet]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
307279
#[cfg(feature = "alloc")]
308280
#[inline]
309281
pub fn encrypt(
@@ -317,7 +289,8 @@ pub fn encrypt(
317289
/// Encrypts `plaintext` with the specified [`Params`] and into a newly
318290
/// allocated [`Vec`](alloc::vec::Vec).
319291
///
320-
/// This uses the Argon2 type created by [`Algorithm::default`].
292+
/// This uses the Argon2 type created by [`Algorithm::default`] and the Argon2
293+
/// version created by [`Version::default`].
321294
///
322295
/// This is a convenience function for using [`Encryptor::with_params`] and
323296
/// [`Encryptor::encrypt_to_vec`].
@@ -348,48 +321,11 @@ pub fn encrypt_with_params(
348321
Encryptor::with_params(&plaintext, passphrase, params).map(|c| c.encrypt_to_vec())
349322
}
350323

351-
#[allow(clippy::module_name_repetitions)]
352-
/// Encrypts `plaintext` with the specified [`Algorithm`] and [`Params`] and
353-
/// into a newly allocated [`Vec`](alloc::vec::Vec).
354-
///
355-
/// This uses the Argon2 version created by [`Version::default`].
356-
///
357-
/// This is a convenience function for using [`Encryptor::with_type`] and
358-
/// [`Encryptor::encrypt_to_vec`].
359-
///
360-
/// # Errors
361-
///
362-
/// Returns [`Err`] if the Argon2 context is invalid.
363-
///
364-
/// # Examples
365-
///
366-
/// ```
367-
/// # use abcrypt::argon2::{Algorithm, Params};
368-
/// #
369-
/// let data = b"Hello, world!\n";
370-
/// let passphrase = "passphrase";
371-
///
372-
/// let params = Params::new(32, 3, 4, None).unwrap();
373-
/// let ciphertext =
374-
/// abcrypt::encrypt_with_type(data, passphrase, Algorithm::Argon2d, params).unwrap();
375-
/// # assert_ne!(ciphertext, data);
376-
/// ```
377-
#[cfg(feature = "alloc")]
378-
#[inline]
379-
pub fn encrypt_with_type(
380-
plaintext: impl AsRef<[u8]>,
381-
passphrase: impl AsRef<[u8]>,
382-
argon2_type: Algorithm,
383-
params: Params,
384-
) -> Result<alloc::vec::Vec<u8>> {
385-
Encryptor::with_type(&plaintext, passphrase, argon2_type, params).map(|c| c.encrypt_to_vec())
386-
}
387-
388324
#[allow(clippy::module_name_repetitions)]
389325
/// Encrypts `plaintext` with the specified [`Algorithm`], [`Version`] and
390326
/// [`Params`] and into a newly allocated [`Vec`](alloc::vec::Vec).
391327
///
392-
/// This is a convenience function for using [`Encryptor::with_version`] and
328+
/// This is a convenience function for using [`Encryptor::with_context`] and
393329
/// [`Encryptor::encrypt_to_vec`].
394330
///
395331
/// # Errors
@@ -406,19 +342,19 @@ pub fn encrypt_with_type(
406342
///
407343
/// let params = Params::new(32, 3, 4, None).unwrap();
408344
/// let ciphertext =
409-
/// abcrypt::encrypt_with_version(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
345+
/// abcrypt::encrypt_with_context(data, passphrase, Algorithm::Argon2i, Version::V0x10, params)
410346
/// .unwrap();
411347
/// # assert_ne!(ciphertext, data);
412348
/// ```
413349
#[cfg(feature = "alloc")]
414350
#[inline]
415-
pub fn encrypt_with_version(
351+
pub fn encrypt_with_context(
416352
plaintext: impl AsRef<[u8]>,
417353
passphrase: impl AsRef<[u8]>,
418354
argon2_type: Algorithm,
419355
argon2_version: Version,
420356
params: Params,
421357
) -> Result<alloc::vec::Vec<u8>> {
422-
Encryptor::with_version(&plaintext, passphrase, argon2_type, argon2_version, params)
358+
Encryptor::with_context(&plaintext, passphrase, argon2_type, argon2_version, params)
423359
.map(|c| c.encrypt_to_vec())
424360
}

crates/abcrypt/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub use crate::{
129129
#[cfg(feature = "alloc")]
130130
pub use crate::{
131131
decrypt::decrypt,
132-
encrypt::{encrypt, encrypt_with_params, encrypt_with_type, encrypt_with_version},
132+
encrypt::{encrypt, encrypt_with_context, encrypt_with_params},
133133
};
134134

135135
#[cfg(not(feature = "alloc"))]

crates/abcrypt/tests/encrypt.rs

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,8 @@ fn success_with_params() {
5858
}
5959

6060
#[test]
61-
fn success_with_type() {
62-
let cipher = Encryptor::with_type(
63-
&TEST_DATA,
64-
PASSPHRASE,
65-
Algorithm::Argon2d,
66-
Params::new(32, 3, 4, None).unwrap(),
67-
)
68-
.unwrap();
69-
let mut buf = [u8::default(); TEST_DATA.len() + HEADER_SIZE + TAG_SIZE];
70-
cipher.encrypt(&mut buf);
71-
assert_ne!(buf, TEST_DATA);
72-
73-
let argon2 = Argon2::new(buf).unwrap();
74-
assert_eq!(argon2.variant(), Algorithm::Argon2d);
75-
assert_eq!(argon2.version(), Version::V0x13);
76-
77-
let params = abcrypt::Params::new(buf).unwrap();
78-
assert_eq!(params.memory_cost(), 32);
79-
assert_eq!(params.time_cost(), 3);
80-
assert_eq!(params.parallelism(), 4);
81-
82-
let cipher = Decryptor::new(&buf, PASSPHRASE).unwrap();
83-
let mut buf = [u8::default(); TEST_DATA.len()];
84-
cipher.decrypt(&mut buf).unwrap();
85-
assert_eq!(buf, TEST_DATA);
86-
}
87-
88-
#[test]
89-
fn success_with_version() {
90-
let cipher = Encryptor::with_version(
61+
fn success_with_context() {
62+
let cipher = Encryptor::with_context(
9163
&TEST_DATA,
9264
PASSPHRASE,
9365
Algorithm::Argon2i,
@@ -177,10 +149,11 @@ fn version() {
177149
#[test]
178150
fn argon2_type() {
179151
{
180-
let cipher = Encryptor::with_type(
152+
let cipher = Encryptor::with_context(
181153
&TEST_DATA,
182154
PASSPHRASE,
183155
Algorithm::Argon2d,
156+
Version::default(),
184157
Params::new(32, 3, 4, None).unwrap(),
185158
)
186159
.unwrap();
@@ -192,10 +165,11 @@ fn argon2_type() {
192165
assert_eq!(argon2.variant(), Algorithm::Argon2d);
193166
}
194167
{
195-
let cipher = Encryptor::with_type(
168+
let cipher = Encryptor::with_context(
196169
&TEST_DATA,
197170
PASSPHRASE,
198171
Algorithm::Argon2i,
172+
Version::default(),
199173
Params::new(32, 3, 4, None).unwrap(),
200174
)
201175
.unwrap();
@@ -207,10 +181,11 @@ fn argon2_type() {
207181
assert_eq!(argon2.variant(), Algorithm::Argon2i);
208182
}
209183
{
210-
let cipher = Encryptor::with_type(
184+
let cipher = Encryptor::with_context(
211185
&TEST_DATA,
212186
PASSPHRASE,
213187
Algorithm::Argon2id,
188+
Version::default(),
214189
Params::new(32, 3, 4, None).unwrap(),
215190
)
216191
.unwrap();
@@ -226,7 +201,7 @@ fn argon2_type() {
226201
#[test]
227202
fn argon2_version() {
228203
{
229-
let cipher = Encryptor::with_version(
204+
let cipher = Encryptor::with_context(
230205
&TEST_DATA,
231206
PASSPHRASE,
232207
Algorithm::default(),
@@ -242,7 +217,7 @@ fn argon2_version() {
242217
assert_eq!(argon2.version(), Version::V0x10);
243218
}
244219
{
245-
let cipher = Encryptor::with_version(
220+
let cipher = Encryptor::with_context(
246221
&TEST_DATA,
247222
PASSPHRASE,
248223
Algorithm::default(),
@@ -375,36 +350,8 @@ fn success_convenience_function_with_params() {
375350

376351
#[cfg(feature = "alloc")]
377352
#[test]
378-
fn success_convenience_function_with_type() {
379-
let ciphertext = abcrypt::encrypt_with_type(
380-
TEST_DATA,
381-
PASSPHRASE,
382-
Algorithm::Argon2d,
383-
Params::new(32, 3, 4, None).unwrap(),
384-
)
385-
.unwrap();
386-
assert_ne!(ciphertext, TEST_DATA);
387-
assert_eq!(ciphertext.len(), TEST_DATA.len() + HEADER_SIZE + TAG_SIZE);
388-
389-
let argon2 = Argon2::new(&ciphertext).unwrap();
390-
assert_eq!(argon2.variant(), Algorithm::Argon2d);
391-
assert_eq!(argon2.version(), Version::V0x13);
392-
393-
let params = abcrypt::Params::new(&ciphertext).unwrap();
394-
assert_eq!(params.memory_cost(), 32);
395-
assert_eq!(params.time_cost(), 3);
396-
assert_eq!(params.parallelism(), 4);
397-
398-
let cipher = Decryptor::new(&ciphertext, PASSPHRASE).unwrap();
399-
let mut buf = [u8::default(); TEST_DATA.len()];
400-
cipher.decrypt(&mut buf).unwrap();
401-
assert_eq!(buf, TEST_DATA);
402-
}
403-
404-
#[cfg(feature = "alloc")]
405-
#[test]
406-
fn success_convenience_function_with_version() {
407-
let ciphertext = abcrypt::encrypt_with_version(
353+
fn success_convenience_function_with_context() {
354+
let ciphertext = abcrypt::encrypt_with_context(
408355
TEST_DATA,
409356
PASSPHRASE,
410357
Algorithm::Argon2i,

crates/capi/examples/encrypt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int main(int argc, char *argv[]) {
8181

8282
std::vector<std::uint8_t> ciphertext(
8383
plaintext.size() + (ABCRYPT_HEADER_SIZE + ABCRYPT_TAG_SIZE));
84-
auto error_code = abcrypt_encrypt_with_version(
84+
auto error_code = abcrypt_encrypt_with_context(
8585
plaintext.data(), plaintext.size(),
8686
reinterpret_cast<uint8_t *>(passphrase.data()), passphrase.size(),
8787
ciphertext.data(), ciphertext.size(), argon2_type, argon2_version,

0 commit comments

Comments
 (0)