From 18c6b33f17ad8e258b0d89f7da5af0dfee2b24a7 Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 22:02:44 +0200 Subject: [PATCH 1/9] feat: direct writing to file --- core/Cargo.toml | 1 + core/src/crypto.rs | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 2b4f6d5..cc6191b 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -13,6 +13,7 @@ jsonwebtoken = "8.1.1" dotenv = "0.15.0" tokio = "1.21.2" futures = "0.3.25" +async-compat = "0.2.1" # crypto argon2 = { version = "0.4", features = ["std"] } diff --git a/core/src/crypto.rs b/core/src/crypto.rs index bacc3fe..68a5e38 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -1,6 +1,7 @@ use argon2::{ Argon2, PasswordHasher as ArgonPasswordHasher, PasswordVerifier as ArgonPasswordVerifier, }; +use async_compat::CompatExt; use secrecy::ExposeSecret; use futures::io::{AsyncReadExt, AsyncWriteExt}; use rand::Rng; @@ -195,8 +196,7 @@ pub async fn encrypt_file_with_recipients( tokio::io::AsyncReadExt::read_to_end(&mut plain_file, &mut plain_file_contents).await?; - let mut encrypt_buffer = Vec::new(); - let mut encrypt_writer = encryptor.wrap_async_output(&mut encrypt_buffer).await?; + let mut encrypt_writer = encryptor.wrap_async_output(cipher_file.compat_mut()).await?; encrypt_writer.write_all(&plain_file_contents).await?; @@ -204,8 +204,6 @@ pub async fn encrypt_file_with_recipients( encrypt_writer.close().await?; - tokio::io::AsyncWriteExt::write_all(&mut cipher_file, &encrypt_buffer).await?; - return Ok(()); } else { // TODO: Error handling From 1704564b6117c0ef9a382abb4b2ca2ae81b56c2f Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 22:04:51 +0200 Subject: [PATCH 2/9] refactor: use native path type --- core/src/crypto.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/crypto.rs b/core/src/crypto.rs index 68a5e38..af34b08 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -6,6 +6,7 @@ use secrecy::ExposeSecret; use futures::io::{AsyncReadExt, AsyncWriteExt}; use rand::Rng; use std::iter; +use std::path::Path; use std::str::FromStr; /// Foolproof random 8 char string @@ -173,9 +174,9 @@ pub async fn decrypt_password_with_private_key( } // TODO: Massive refactor of encrypt_file_with_recipients required -pub async fn encrypt_file_with_recipients( - plain_file_path: &str, - cipher_file_path: &str, +pub async fn encrypt_file_with_recipients>( + plain_file_path: P, + cipher_file_path: P, recipients: Vec<&str>, ) -> Result<(), age::EncryptError> { let public_keys = recipients From 0d5cf755c84791feb1ab8166059515902cd7decf Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 22:34:55 +0200 Subject: [PATCH 3/9] chore: file writing test --- core/Cargo.toml | 3 ++- core/src/crypto.rs | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index cc6191b..d9cb0c4 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -30,4 +30,5 @@ features = [ ] [dev-dependencies] -tokio = "1.21.2" \ No newline at end of file +tokio = "1.21" +tempfile = "3.3" \ No newline at end of file diff --git a/core/src/crypto.rs b/core/src/crypto.rs index af34b08..31faab4 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -353,4 +353,29 @@ mod tests { assert_eq!(PASSWORD, decrypted_2); } + + #[tokio::test] + async fn test_encrypt_file_with_recipients() { + const PUBLIC_KEY: &str = "age1t220v5c8ye0pjx99kw8nr57y7a5qlw4ke0wchjuxnr2gcvfzt3hq7fufz0"; + const PRIVATE_KEY: &str = + "AGE-SECRET-KEY-1WPDHL2FLJ23T6RK5KCX8KS8DNLX0CGXMNZG0XNUAH4QP5C8ZZ46QGD3STV"; + + const PASSWORD: &str = "test"; + + let mut plain_file = tempfile::NamedTempFile::new().unwrap(); + let mut encrypted_file = tempfile::NamedTempFile::new().unwrap(); + + std::io::Write::write_all(&mut plain_file, PASSWORD.as_bytes()).unwrap(); + + assert_eq!(std::fs::read_to_string(&plain_file).unwrap(), PASSWORD); + + super::encrypt_file_with_recipients(&plain_file, &encrypted_file, vec![PUBLIC_KEY]).await.unwrap(); + + let mut buffer = [0; 21]; + + std::io::Read::read(&mut encrypted_file, &mut buffer).unwrap(); + + assert_eq!(&buffer, b"age-encryption.org/v1"); + } + } From 9e9dbcc038a5f66ed6d7e7c38e19a8fca19f5685 Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 23:02:32 +0200 Subject: [PATCH 4/9] refactor: massive refactoring of age key encryption/decryption functions --- core/src/crypto.rs | 163 ++++++++++++++++++++++++--------------------- 1 file changed, 86 insertions(+), 77 deletions(-) diff --git a/core/src/crypto.rs b/core/src/crypto.rs index 31faab4..ee15b00 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -2,9 +2,9 @@ use argon2::{ Argon2, PasswordHasher as ArgonPasswordHasher, PasswordVerifier as ArgonPasswordVerifier, }; use async_compat::CompatExt; -use secrecy::ExposeSecret; use futures::io::{AsyncReadExt, AsyncWriteExt}; use rand::Rng; +use secrecy::ExposeSecret; use std::iter; use std::path::Path; use std::str::FromStr; @@ -32,7 +32,7 @@ pub fn random_8_char_string() -> String { pub async fn hash_password( password_plain_text: String, -) -> Result> { +) -> Result> { let argon_config = Argon2::default(); let hash = tokio::task::spawn_blocking(move || { @@ -41,7 +41,9 @@ pub async fn hash_password( let salt_str = argon2::password_hash::SaltString::generate(rand::thread_rng()); let salt = salt_str.as_salt(); - return argon_config.hash_password(password, &salt).map(|x| x.serialize().to_string()); + return argon_config + .hash_password(password, &salt) + .map(|x| x.serialize().to_string()); }); let hash_string = hash.await??; @@ -112,71 +114,19 @@ pub async fn decrypt_password( Ok(String::from_utf8(decrypt_buffer)?) } -pub fn create_identity() -> (String, String){ +pub fn create_identity() -> (String, String) { let identity = age::x25519::Identity::generate(); // Public Key & Private Key - (identity.to_public().to_string(), identity.to_string().expose_secret().to_string()) + ( + identity.to_public().to_string(), + identity.to_string().expose_secret().to_string(), + ) } -pub async fn encrypt_password_with_recipients( - password_plain_text: &str, - recipients: Vec<&str>, -) -> Result { - let public_keys = recipients - .into_iter() - .map(|recipient| { - //TODO: No unwrap - Box::new(age::x25519::Recipient::from_str(recipient).unwrap()) as _ - }) - .collect(); - - let encryptor_option = age::Encryptor::with_recipients(public_keys); - - if let Some(encryptor) = encryptor_option { - let mut encrypt_buffer = Vec::new(); - let mut encrypt_writer = encryptor.wrap_async_output(&mut encrypt_buffer).await?; - - encrypt_writer - .write_all(password_plain_text.as_bytes()) - .await?; - - encrypt_writer.flush().await?; - - encrypt_writer.close().await?; - - Ok(base64::encode(encrypt_buffer)) - } else { - // TODO: Error handling - unreachable!("No recipients provided"); - } -} - -pub async fn decrypt_password_with_private_key( - password_encrypted: &str, - key: &str, -) -> Result> { - let encrypted = base64::decode(password_encrypted)?; - - let decryptor = match age::Decryptor::new_async(&encrypted[..]).await? { - age::Decryptor::Recipients(d) => d, - _ => unreachable!(), - }; - - let mut decrypt_buffer = Vec::new(); - let mut decrypt_writer = decryptor.decrypt_async(iter::once( - &age::x25519::Identity::from_str(key)? as &dyn age::Identity, - ))?; - - decrypt_writer.read_to_end(&mut decrypt_buffer).await?; - - Ok(String::from_utf8(decrypt_buffer)?) -} - -// TODO: Massive refactor of encrypt_file_with_recipients required -pub async fn encrypt_file_with_recipients>( - plain_file_path: P, - cipher_file_path: P, +async fn age_encrypt_with_recipients( + input_buffer: &[u8], + output_buffer: &mut W, recipients: Vec<&str>, ) -> Result<(), age::EncryptError> { let public_keys = recipients @@ -190,16 +140,11 @@ pub async fn encrypt_file_with_recipients>( let encryptor_option = age::Encryptor::with_recipients(public_keys); if let Some(encryptor) = encryptor_option { - let mut cipher_file = tokio::fs::File::create(cipher_file_path).await?; - let mut plain_file = tokio::fs::File::open(plain_file_path).await?; + let mut encrypt_writer = encryptor + .wrap_async_output(output_buffer.compat_mut()) + .await?; - let mut plain_file_contents = Vec::new(); - - tokio::io::AsyncReadExt::read_to_end(&mut plain_file, &mut plain_file_contents).await?; - - let mut encrypt_writer = encryptor.wrap_async_output(cipher_file.compat_mut()).await?; - - encrypt_writer.write_all(&plain_file_contents).await?; + encrypt_writer.write_all(input_buffer).await?; encrypt_writer.flush().await?; @@ -212,6 +157,69 @@ pub async fn encrypt_file_with_recipients>( } } +async fn age_decrypt_with_private_key( + input_buffer: R, + output_buffer: &mut Vec, + key: &str, +) -> Result<(), Box> { + let decryptor = match age::Decryptor::new_async(input_buffer.compat()).await? { + age::Decryptor::Recipients(d) => d, + _ => unreachable!(), + }; + + let mut decrypt_writer = decryptor.decrypt_async(iter::once( + &age::x25519::Identity::from_str(key)? as &dyn age::Identity, + ))?; + + decrypt_writer.read_to_end(output_buffer).await?; + + Ok(()) +} + +pub async fn encrypt_password_with_recipients( + password_plain_text: &str, + recipients: Vec<&str>, +) -> Result { + let mut encrypt_buffer = Vec::new(); + + age_encrypt_with_recipients( + password_plain_text.as_bytes(), + &mut encrypt_buffer, + recipients, + ) + .await?; + + Ok(base64::encode(encrypt_buffer)) +} + +pub async fn decrypt_password_with_private_key( + password_encrypted: &str, + key: &str, +) -> Result> { + let encrypted = base64::decode(password_encrypted)?; + + let mut decrypt_buffer = Vec::new(); + + age_decrypt_with_private_key(encrypted.as_slice(), &mut decrypt_buffer, key).await?; + + Ok(String::from_utf8(decrypt_buffer)?) +} + +pub async fn encrypt_file_with_recipients>( + plain_file_path: P, + cipher_file_path: P, + recipients: Vec<&str>, +) -> Result<(), age::EncryptError> { + let mut cipher_file = tokio::fs::File::create(cipher_file_path).await?; + let mut plain_file = tokio::fs::File::open(plain_file_path).await?; + + let mut plain_file_contents = Vec::new(); + + tokio::io::AsyncReadExt::read_to_end(&mut plain_file, &mut plain_file_contents).await?; + + age_encrypt_with_recipients(plain_file_contents.as_slice(), &mut cipher_file, recipients).await +} + #[cfg(test)] mod tests { #[test] @@ -278,7 +286,7 @@ mod tests { assert_eq!(PASSWORD, decrypted); } - + #[test] fn test_create_identity() { let identity = super::create_identity(); @@ -358,7 +366,7 @@ mod tests { async fn test_encrypt_file_with_recipients() { const PUBLIC_KEY: &str = "age1t220v5c8ye0pjx99kw8nr57y7a5qlw4ke0wchjuxnr2gcvfzt3hq7fufz0"; const PRIVATE_KEY: &str = - "AGE-SECRET-KEY-1WPDHL2FLJ23T6RK5KCX8KS8DNLX0CGXMNZG0XNUAH4QP5C8ZZ46QGD3STV"; + "AGE-SECRET-KEY-1WPDHL2FLJ23T6RK5KCX8KS8DNLX0CGXMNZG0XNUAH4QP5C8ZZ46QGD3STV"; const PASSWORD: &str = "test"; @@ -369,13 +377,14 @@ mod tests { assert_eq!(std::fs::read_to_string(&plain_file).unwrap(), PASSWORD); - super::encrypt_file_with_recipients(&plain_file, &encrypted_file, vec![PUBLIC_KEY]).await.unwrap(); - + super::encrypt_file_with_recipients(&plain_file, &encrypted_file, vec![PUBLIC_KEY]) + .await + .unwrap(); + let mut buffer = [0; 21]; std::io::Read::read(&mut encrypted_file, &mut buffer).unwrap(); assert_eq!(&buffer, b"age-encryption.org/v1"); } - } From fbeed2d43274b4213a10f0efc1e7921765c59a0b Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 23:14:10 +0200 Subject: [PATCH 5/9] feat: add file decrypt --- core/src/crypto.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/core/src/crypto.rs b/core/src/crypto.rs index ee15b00..11a76b7 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -220,6 +220,23 @@ pub async fn encrypt_file_with_recipients>( age_encrypt_with_recipients(plain_file_contents.as_slice(), &mut cipher_file, recipients).await } +pub async fn decrypt_file_with_private_key>( + cipher_file_path: P, + plain_file_path: P, + key: &str, +) -> Result<(), Box> { + let cipher_file = tokio::fs::File::open(cipher_file_path).await?; + let mut plain_file = tokio::fs::File::create(plain_file_path).await?; + + let mut plain_file_contents = Vec::new(); + + age_decrypt_with_private_key(cipher_file, &mut plain_file_contents, key).await?; + + tokio::io::AsyncWriteExt::write_all(&mut plain_file, plain_file_contents.as_slice()).await?; + + Ok(()) +} + #[cfg(test)] mod tests { #[test] @@ -363,7 +380,7 @@ mod tests { } #[tokio::test] - async fn test_encrypt_file_with_recipients() { + async fn test_encrypt_decrypt_file_with_recipients() { const PUBLIC_KEY: &str = "age1t220v5c8ye0pjx99kw8nr57y7a5qlw4ke0wchjuxnr2gcvfzt3hq7fufz0"; const PRIVATE_KEY: &str = "AGE-SECRET-KEY-1WPDHL2FLJ23T6RK5KCX8KS8DNLX0CGXMNZG0XNUAH4QP5C8ZZ46QGD3STV"; @@ -386,5 +403,13 @@ mod tests { std::io::Read::read(&mut encrypted_file, &mut buffer).unwrap(); assert_eq!(&buffer, b"age-encryption.org/v1"); + + let decrypted_file = tempfile::NamedTempFile::new().unwrap(); + + super::decrypt_file_with_private_key(&encrypted_file, &decrypted_file, PRIVATE_KEY) + .await + .unwrap(); + + assert_eq!(std::fs::read_to_string(&decrypted_file).unwrap(), "PASSWORD"); } } From 761f769a4865c80af97ecdeee0a9f294094c24f8 Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 23:25:40 +0200 Subject: [PATCH 6/9] feat: decrypt to buffer --- core/src/crypto.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/core/src/crypto.rs b/core/src/crypto.rs index 11a76b7..aba3c8a 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -237,6 +237,19 @@ pub async fn decrypt_file_with_private_key>( Ok(()) } +pub async fn decrypt_file_with_private_key_as_buffer>( + cipher_file_path: P, + key: &str, +) -> Result, Box> { + let cipher_file = tokio::fs::File::open(cipher_file_path).await?; + + let mut plain_file = Vec::new(); + + age_decrypt_with_private_key(cipher_file, &mut plain_file, key).await?; + + Ok(plain_file) +} + #[cfg(test)] mod tests { #[test] @@ -410,6 +423,40 @@ mod tests { .await .unwrap(); - assert_eq!(std::fs::read_to_string(&decrypted_file).unwrap(), "PASSWORD"); + assert_eq!( + std::fs::read_to_string(&decrypted_file).unwrap(), + "PASSWORD" + ); + } + + #[tokio::test] + async fn test_decrypt_file_with_private_key_as_buffer() { + const PUBLIC_KEY: &str = "age1t220v5c8ye0pjx99kw8nr57y7a5qlw4ke0wchjuxnr2gcvfzt3hq7fufz0"; + const PRIVATE_KEY: &str = + "AGE-SECRET-KEY-1WPDHL2FLJ23T6RK5KCX8KS8DNLX0CGXMNZG0XNUAH4QP5C8ZZ46QGD3STV"; + + const PASSWORD: &str = "test"; + + let mut plain_file = tempfile::NamedTempFile::new().unwrap(); + let encrypted_file = tempfile::NamedTempFile::new().unwrap(); + + std::io::Write::write_all(&mut plain_file, PASSWORD.as_bytes()).unwrap(); + + let plain_buffer = std::fs::read(&plain_file).unwrap(); + + assert_eq!(String::from_utf8(plain_buffer.clone()).unwrap(), PASSWORD); + + super::encrypt_file_with_recipients(&plain_file, &encrypted_file, vec![PUBLIC_KEY]) + .await + .unwrap(); + + let decrypted_buffer = + super::decrypt_file_with_private_key_as_buffer(encrypted_file, PRIVATE_KEY) + .await + .unwrap(); + + assert_eq!(plain_buffer.len(), decrypted_buffer.len()); + + assert_eq!(String::from_utf8(decrypted_buffer.clone()).unwrap(), PASSWORD); } } From e55c182d9b9259e9afd0d50ba6caf9b846f570ed Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 23:32:33 +0200 Subject: [PATCH 7/9] fix: fix bug in test --- core/src/crypto.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/core/src/crypto.rs b/core/src/crypto.rs index aba3c8a..9723cd0 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -348,7 +348,6 @@ mod tests { .await .unwrap(); - println!("{}", encrypted); assert!(base64::decode(encrypted).is_ok()); } @@ -423,10 +422,7 @@ mod tests { .await .unwrap(); - assert_eq!( - std::fs::read_to_string(&decrypted_file).unwrap(), - "PASSWORD" - ); + assert_eq!(std::fs::read_to_string(&decrypted_file).unwrap(), PASSWORD); } #[tokio::test] @@ -457,6 +453,9 @@ mod tests { assert_eq!(plain_buffer.len(), decrypted_buffer.len()); - assert_eq!(String::from_utf8(decrypted_buffer.clone()).unwrap(), PASSWORD); + assert_eq!( + String::from_utf8(decrypted_buffer.clone()).unwrap(), + PASSWORD + ); } } From 96be1f4d83734ee7db78d49b16fe51059ab83673 Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 28 Oct 2022 23:49:33 +0200 Subject: [PATCH 8/9] feat: async tempfile for tests --- core/Cargo.toml | 2 +- core/src/crypto.rs | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index d9cb0c4..c7a44e9 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -31,4 +31,4 @@ features = [ [dev-dependencies] tokio = "1.21" -tempfile = "3.3" \ No newline at end of file +async-tempfile = "0.2.0" \ No newline at end of file diff --git a/core/src/crypto.rs b/core/src/crypto.rs index 9723cd0..c887053 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -399,30 +399,30 @@ mod tests { const PASSWORD: &str = "test"; - let mut plain_file = tempfile::NamedTempFile::new().unwrap(); - let mut encrypted_file = tempfile::NamedTempFile::new().unwrap(); + let mut plain_file = async_tempfile::TempFile::new().await.unwrap(); + let mut encrypted_file = async_tempfile::TempFile::new().await.unwrap(); - std::io::Write::write_all(&mut plain_file, PASSWORD.as_bytes()).unwrap(); + tokio::io::AsyncWriteExt::write_all(&mut plain_file, PASSWORD.as_bytes()).await.unwrap(); - assert_eq!(std::fs::read_to_string(&plain_file).unwrap(), PASSWORD); + assert_eq!(tokio::fs::read_to_string(&plain_file.file_path()).await.unwrap(), PASSWORD); - super::encrypt_file_with_recipients(&plain_file, &encrypted_file, vec![PUBLIC_KEY]) + super::encrypt_file_with_recipients(&plain_file.file_path(), &encrypted_file.file_path(), vec![PUBLIC_KEY]) .await .unwrap(); let mut buffer = [0; 21]; - std::io::Read::read(&mut encrypted_file, &mut buffer).unwrap(); + tokio::io::AsyncReadExt::read(&mut encrypted_file, &mut buffer).await.unwrap(); assert_eq!(&buffer, b"age-encryption.org/v1"); - let decrypted_file = tempfile::NamedTempFile::new().unwrap(); + let decrypted_file = async_tempfile::TempFile::new().await.unwrap(); - super::decrypt_file_with_private_key(&encrypted_file, &decrypted_file, PRIVATE_KEY) + super::decrypt_file_with_private_key(&encrypted_file.file_path(), &decrypted_file.file_path(), PRIVATE_KEY) .await .unwrap(); - assert_eq!(std::fs::read_to_string(&decrypted_file).unwrap(), PASSWORD); + assert_eq!(tokio::fs::read_to_string(&decrypted_file.file_path()).await.unwrap(), PASSWORD); } #[tokio::test] @@ -433,21 +433,21 @@ mod tests { const PASSWORD: &str = "test"; - let mut plain_file = tempfile::NamedTempFile::new().unwrap(); - let encrypted_file = tempfile::NamedTempFile::new().unwrap(); + let mut plain_file = async_tempfile::TempFile::new().await.unwrap(); + let encrypted_file = async_tempfile::TempFile::new().await.unwrap(); - std::io::Write::write_all(&mut plain_file, PASSWORD.as_bytes()).unwrap(); + tokio::io::AsyncWriteExt::write_all(&mut plain_file, PASSWORD.as_bytes()).await.unwrap(); - let plain_buffer = std::fs::read(&plain_file).unwrap(); + let plain_buffer = tokio::fs::read(&plain_file.file_path()).await.unwrap(); assert_eq!(String::from_utf8(plain_buffer.clone()).unwrap(), PASSWORD); - super::encrypt_file_with_recipients(&plain_file, &encrypted_file, vec![PUBLIC_KEY]) + super::encrypt_file_with_recipients(&plain_file.file_path(), &encrypted_file.file_path(), vec![PUBLIC_KEY]) .await .unwrap(); let decrypted_buffer = - super::decrypt_file_with_private_key_as_buffer(encrypted_file, PRIVATE_KEY) + super::decrypt_file_with_private_key_as_buffer(encrypted_file.file_path(), PRIVATE_KEY) .await .unwrap(); From 433e45e3153b16785938a43fc275250b0338aa27 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Sat, 29 Oct 2022 11:27:32 +0200 Subject: [PATCH 9/9] feat: sync all in encrypt decrypt test --- core/src/crypto.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/crypto.rs b/core/src/crypto.rs index c887053..d6ded82 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -403,12 +403,14 @@ mod tests { let mut encrypted_file = async_tempfile::TempFile::new().await.unwrap(); tokio::io::AsyncWriteExt::write_all(&mut plain_file, PASSWORD.as_bytes()).await.unwrap(); + encrypted_file.sync_all().await.unwrap(); assert_eq!(tokio::fs::read_to_string(&plain_file.file_path()).await.unwrap(), PASSWORD); super::encrypt_file_with_recipients(&plain_file.file_path(), &encrypted_file.file_path(), vec![PUBLIC_KEY]) .await .unwrap(); + encrypted_file.sync_all().await.unwrap(); let mut buffer = [0; 21]; @@ -421,6 +423,7 @@ mod tests { super::decrypt_file_with_private_key(&encrypted_file.file_path(), &decrypted_file.file_path(), PRIVATE_KEY) .await .unwrap(); + decrypted_file.sync_all().await.unwrap(); assert_eq!(tokio::fs::read_to_string(&decrypted_file.file_path()).await.unwrap(), PASSWORD); }