Skip to content

Commit

Permalink
Add revelation-decode binary
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Jun 2, 2024
1 parent ce4eed1 commit b479088
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 4 deletions.
114 changes: 114 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
license = "LGPL-3.0-or-later"
description = "Password storing program"
readme = "README.md"
default-run = "password-storage"

[dependencies]
futures = "0.3"
Expand All @@ -26,6 +27,8 @@ gtk = { package = "gtk4", version = "0.8", features = ["v4_12"] }
awesome-glib = "0.4"
awesome-gtk = "0.4"

clap = { version = "4.5.4", features = ["derive"] }

[build-dependencies]
glib-build-tools = "0.19"

Expand Down
41 changes: 41 additions & 0 deletions src/bin/revelation-decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use clap::Parser;
use password_storage::format::revelation;
use std::error::Error;
use std::fs;
use std::io::{stdin, stdout, BufReader, Write};
use std::path::PathBuf;

#[derive(Parser)]
struct Opts {
/// Input file
input: PathBuf,

/// Input file
output: PathBuf,
}

fn main() -> Result<(), Box<dyn Error>> {
let opts = Opts::parse();

print!("Enter a password for a file {}: ", opts.input.display());
stdout().flush()?;

let mut password = String::new();
stdin().read_line(&mut password)?;
println!("");

let file = fs::OpenOptions::new().read(true).open(&opts.input)?;
let mut buffer = BufReader::new(file);

let data = revelation::decrypt_revelation_file(&mut buffer, &password.trim())?;

fs::write(&opts.output, data.content)?;

println!(
"Output file: {}\nData format: {:?}\nDone",
opts.output.display(),
data.format
);

Ok(())
}
19 changes: 15 additions & 4 deletions src/format/revelation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ impl CryptoContainer {
}
}

enum SerializationFormat {
#[derive(Debug)]
pub enum SerializationFormat {
Xml,
}

Expand All @@ -51,16 +52,26 @@ fn version_impl(version: u8) -> Option<(CryptoContainer, SerializationFormat)> {
}
}

pub fn load_revelation_file(source: &mut dyn Read, password: &str) -> Result<RecordTree> {
pub struct Decrypted {
pub format: SerializationFormat,
pub content: Vec<u8>,
}

pub fn decrypt_revelation_file(source: &mut dyn Read, password: &str) -> Result<Decrypted> {
let header = file_header::FileHeader::read(source)?;
let (container, format) = version_impl(header.data_version).ok_or_else(|| {
format!(
"Unsupported format (rvl {}). Try to use version {} or above.",
header.data_version, header.app_version
)
})?;
let decrypted = container.decrypt(source, password)?;
let tree = format.deserialize(&decrypted)?;
let content = container.decrypt(source, password)?;
Ok(Decrypted { format, content })
}

pub fn load_revelation_file(source: &mut dyn Read, password: &str) -> Result<RecordTree> {
let Decrypted { format, content } = decrypt_revelation_file(source, password)?;
let tree = format.deserialize(&content)?;
Ok(tree)
}

Expand Down

0 comments on commit b479088

Please sign in to comment.