return to match/for statements for async iterators (until rust gets better support)

This commit is contained in:
brxken128 2023-01-24 14:04:18 +00:00
parent b929d36c89
commit 87b75cfe62
No known key found for this signature in database
GPG key ID: 8B8D1AA6AE10A8FF
3 changed files with 48 additions and 35 deletions

View file

@ -98,7 +98,7 @@ impl FileHeader {
///
/// You receive an error if the password doesn't match or if there are no keyslots.
#[allow(clippy::needless_pass_by_value)]
pub fn decrypt_master_key(
pub async fn decrypt_master_key(
&self,
password: Protected<Vec<u8>>,
) -> Result<Protected<[u8; KEY_LEN]>> {
@ -106,27 +106,18 @@ impl FileHeader {
return Err(Error::NoKeyslots);
}
self.keyslots
.iter()
.find_map(|v| v.decrypt_master_key(password.clone()).ok())
.map(|v| Protected::new(to_array::<KEY_LEN>(v.into_inner()).unwrap()))
.ok_or(Error::IncorrectPassword)
}
/// This is a helper function to find which keyslot a key belongs to.
///
/// You receive an error if the password doesn't match or if there are no keyslots.
#[allow(clippy::needless_pass_by_value)]
pub fn find_key_index(&self, password: Protected<Vec<u8>>) -> Result<usize> {
if self.keyslots.is_empty() {
return Err(Error::NoKeyslots);
for v in self.keyslots.iter() {
if let Some(key) = v
.decrypt_master_key(password.clone())
.await
.ok()
.map(|v| Protected::new(to_array::<KEY_LEN>(v.into_inner()).unwrap()))
{
return Ok(key);
}
}
self.keyslots
.iter()
.enumerate()
.find_map(|(i, v)| v.decrypt_master_key(password.clone()).ok().map(|_| i))
.ok_or(Error::IncorrectPassword)
Err(Error::IncorrectPassword)
}
/// This is a helper function to serialize and write a header to a file.
@ -144,7 +135,7 @@ impl FileHeader {
///
/// You receive an error if the password doesn't match or if there are no keyslots.
#[allow(clippy::needless_pass_by_value)]
pub fn decrypt_master_key_from_prehashed(
pub async fn decrypt_master_key_from_prehashed(
&self,
hashed_keys: Vec<Protected<[u8; KEY_LEN]>>,
) -> Result<Protected<[u8; KEY_LEN]>> {
@ -152,16 +143,38 @@ impl FileHeader {
return Err(Error::NoKeyslots);
}
hashed_keys
.iter()
.find_map(|v| {
self.keyslots.iter().find_map(|z| {
z.decrypt_master_key_from_prehashed(v.clone())
.ok()
.map(|x| Protected::new(to_array::<KEY_LEN>(x.into_inner()).unwrap()))
})
})
.ok_or(Error::IncorrectPassword)
for hashed_key in hashed_keys {
for v in self.keyslots.iter() {
if let Some(key) = v
.decrypt_master_key_from_prehashed(hashed_key.clone())
.await
.ok()
.map(|v| Protected::new(to_array::<KEY_LEN>(v.into_inner()).unwrap()))
{
return Ok(key);
}
}
}
Err(Error::IncorrectPassword)
}
/// This is a helper function to find which keyslot a key belongs to.
///
/// You receive an error if the password doesn't match or if there are no keyslots.
#[allow(clippy::needless_pass_by_value)]
pub async fn find_key_index(&self, password: Protected<Vec<u8>>) -> Result<usize> {
if self.keyslots.is_empty() {
return Err(Error::NoKeyslots);
}
for (i, v) in self.keyslots.iter().enumerate() {
if let Some(i) = v.decrypt_master_key(password.clone()).await.ok().map(|_| i) {
return Ok(i);
}
}
Err(Error::IncorrectPassword)
}
/// This function should be used for generating AAD before encryption

View file

@ -114,7 +114,7 @@ impl FileHeader {
where
T: serde::de::DeserializeOwned,
{
let master_key = self.decrypt_master_key_from_prehashed(hashed_keys)?;
let master_key = self.decrypt_master_key_from_prehashed(hashed_keys).await?;
match self.metadata.as_ref() {
Some(metadata) => {
@ -143,7 +143,7 @@ impl FileHeader {
where
T: serde::de::DeserializeOwned,
{
let master_key = self.decrypt_master_key(password)?;
let master_key = self.decrypt_master_key(password).await?;
match self.metadata.as_ref() {
Some(metadata) => {

View file

@ -91,7 +91,7 @@ impl FileHeader {
&self,
hashed_keys: Vec<Protected<[u8; KEY_LEN]>>,
) -> Result<Protected<Vec<u8>>> {
let master_key = self.decrypt_master_key_from_prehashed(hashed_keys)?;
let master_key = self.decrypt_master_key_from_prehashed(hashed_keys).await?;
match self.preview_media.as_ref() {
Some(pvm) => {
@ -119,7 +119,7 @@ impl FileHeader {
&self,
password: Protected<Vec<u8>>,
) -> Result<Protected<Vec<u8>>> {
let master_key = self.decrypt_master_key(password)?;
let master_key = self.decrypt_master_key(password).await?;
match self.preview_media.as_ref() {
Some(pvm) => {