Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use checksum of encrypted file data. #139

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/integration/archive_export_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fn create_archive(
#[tokio::test]
#[serial]
async fn integration_archive_local_provider() -> Result<()> {

// TODO: test creating external file storage
// TODO: and extracting the archived files

Expand Down Expand Up @@ -84,6 +83,7 @@ async fn integration_archive_local_provider() -> Result<()> {
selected: vec![vault.summary().clone()],
passphrase: Some(SecretString::new(passphrase.to_string())),
files_dir: None,
files_dir_builder: None,
};

// Create the archive
Expand Down
5 changes: 1 addition & 4 deletions workspace/core/src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ impl<W: Write + Seek> Writer<W> {
pub fn finish(mut self) -> Result<W> {
let manifest = serde_json::to_vec_pretty(&self.manifest)?;

self.append_file_buffer(
ARCHIVE_MANIFEST,
manifest.as_slice(),
)?;
self.append_file_buffer(ARCHIVE_MANIFEST, manifest.as_slice())?;

Ok(self.builder.finish()?)
}
Expand Down
35 changes: 8 additions & 27 deletions workspace/node/src/client/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! creating and managing local accounts.
use std::{
borrow::Cow,
io::{Cursor, Read, Write},
io::{Cursor, Read},
path::{Path, PathBuf},
sync::Arc,
};
Expand Down Expand Up @@ -291,9 +291,9 @@ impl AccountManager {
}

/// Encrypt a file using AGE passphrase encryption and
/// move to a target directory.
/// write to a target directory.
///
/// The file name is the Sha256 digest of the original file.
/// The file name is the Sha256 digest of the encrypted file.
pub fn encrypt_file<S: AsRef<Path>, T: AsRef<Path>>(
source: S,
target: T,
Expand All @@ -302,37 +302,18 @@ impl AccountManager {
let mut file = std::fs::File::open(source)?;
let encryptor = Encryptor::with_user_passphrase(passphrase);

let mut hasher = Sha256::new();
let mut encrypted = tempfile::NamedTempFile::new()?;

let mut encrypted = Vec::new();
let mut writer = encryptor.wrap_output(&mut encrypted)?;

let chunk_size = 8192;
loop {
let mut chunk = Vec::with_capacity(chunk_size);
let n = std::io::Read::by_ref(&mut file)
.take(chunk_size as u64)
.read_to_end(&mut chunk)?;
if n == 0 {
break;
}

writer.write_all(chunk.as_slice())?;
hasher.update(chunk.as_slice());

if n < chunk_size {
break;
}
}

std::io::copy(&mut file, &mut writer)?;
writer.finish()?;

let mut hasher = Sha256::new();
hasher.update(&encrypted);
let digest = hasher.finalize();
let file_name = hex::encode(digest);
let dest = PathBuf::from(target.as_ref()).join(&file_name);

// Move the temporary file into place
std::fs::rename(encrypted.path(), dest)?;
std::fs::write(dest, encrypted)?;

Ok(digest.to_vec())
}
Expand Down