Skip to content

Commit

Permalink
Setup lib.rs
Browse files Browse the repository at this point in the history
Prepared the project to be usable with both CLI and GUI by exposing common API functions in lib.rs
  • Loading branch information
duckysmacky committed Aug 11, 2024
1 parent 0871529 commit bf3aba2
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 215 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ name = "lockbox"
version = "0.1.0"
edition = "2021"

[lib]
path = "src/lib.rs"

[[bin]]
name = "lockbox"
path = "src/cli/main.rs"

[dependencies]
bincode = "1.3.3"
chacha20 = "0.9.1"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# LockBox

A fast and secure data encryption program, which focuses on speed, safety and user-friendliness
A data encryption program, which focuses on speed, safety and user-friendliness

*The project is still very work-in-progress and the mentioned features could very well change during development*

## About

Lockbox aims to be cross-platform lightweight solution for file encryption
Lockbox aims to be cross-platform a lightweight solution for file encryption

Lockbox is mainly using the **ChaCha20** algorithm with **Poly1305** universal hash function to perform encryption
operations. It proved to be much more safe and fast than the most popular **AES** algorithm

The encrypted files are stored in a custom `.box` file format. This enables the ability to embed additional safety
utilities into the encrypted data, such as checksums, to check file's integrity. It also sets lockbox apart from its
utilities into the encrypted data, such as checksums, to check the file's integrity. It also sets lockbox apart from its
competitors

The general idea behind this project is to standardise file encryption by making a universal, cross-platform and
Expand Down
42 changes: 22 additions & 20 deletions src/commands/mod.rs → src/cli/commands.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
mod r#box;
mod unbox;
mod path;
mod key;

use std::path::PathBuf;
use std::collections::VecDeque;
use std::{
collections::VecDeque, path::PathBuf,
io
};
use clap::ArgMatches;
use crate::{log_error, log_success};
use crate::cli::path;
use crate::{decrypt, DecryptionOptions, delete_key, DeleteKeyOptions, encrypt, EncryptionOptions, KeyOptions, log_error, log_success, new_key, NewKeyOptions};

pub fn r#box(args: &ArgMatches) -> u32 {
pub fn r#box(args: &ArgMatches) -> io::Result<u32> {
let mut total_files: u32 = 0;
let mut file_paths: Vec<PathBuf> = Vec::new();

Expand All @@ -18,13 +16,13 @@ pub fn r#box(args: &ArgMatches) -> u32 {
};
path::parse_paths(&mut file_paths, options);

let mut options = r#box::EncryptionOptions {
let mut options = EncryptionOptions {
keep_name: args.get_flag("keep-name"),
output_paths: get_path_deque(args, "output-path")
};

for path in file_paths {
match r#box::encrypt(path.as_path(), &mut options) {
match encrypt(path.as_path(), &mut options) {
Ok(_) => {
log_success!("Successfully encrypted {:?}", path.file_name().unwrap().to_os_string());
total_files += 1;
Expand All @@ -33,10 +31,10 @@ pub fn r#box(args: &ArgMatches) -> u32 {
}
}

total_files
Ok(total_files)
}

pub fn unbox(args: &ArgMatches) -> u32 {
pub fn unbox(args: &ArgMatches) -> io::Result<u32> {
let mut total_files: u32 = 0;
let mut file_paths: Vec<PathBuf> = Vec::new();

Expand All @@ -46,12 +44,12 @@ pub fn unbox(args: &ArgMatches) -> u32 {
};
path::parse_paths(&mut file_paths, options);

let mut options = unbox::DecryptionOptions {
let mut options = DecryptionOptions {
output_paths: get_path_deque(args, "output-path")
};

for path in file_paths {
match unbox::decrypt(path.as_path(), &mut options) {
match decrypt(path.as_path(), &mut options) {
Ok(_) => {
log_success!("Successfully decrypted {:?}", path.file_name().unwrap().to_os_string());
total_files += 1;
Expand All @@ -60,22 +58,26 @@ pub fn unbox(args: &ArgMatches) -> u32 {
}
}

total_files
Ok(total_files)
}

pub fn key(args: &ArgMatches) {
/* NEW */
if let Some(_args) = args.subcommand_matches("new") {
let options = key::NewOptions {};
match key::new_key(&options) {
let options = NewKeyOptions {
key_options: KeyOptions {}
};
match new_key(&options) {
Ok(_) => log_success!("Successfully generated a new encryption key"),
Err(err) => log_error!("An error has occurred while trying to generate a new key: {}", err)
}
}
/* DELETE */
if let Some(_args) = args.subcommand_matches("delete") {
let options = key::DeleteOptions {};
match key::delete_key(&options) {
let options = DeleteKeyOptions {
key_options: KeyOptions {}
};
match delete_key(&options) {
Ok(_) => log_success!("Successfully deleted encryption key"),
Err(err) => log_error!("An error has occurred while trying to delete a key: {}", err)
}
Expand Down
15 changes: 7 additions & 8 deletions src/logger.rs → src/cli/logger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
sync::{Arc, Mutex},
string::String,
cmp::PartialEq,
string::String, cmp::PartialEq,
fmt
};

Expand Down Expand Up @@ -85,7 +84,7 @@ pub fn configure_logger(args: &ArgMatches) {
macro_rules! log_info {
($($arg:tt)*) => {
{
use crate::logger::*;
use crate::cli::logger::*;
let logger = LOGGER.lock().unwrap();
logger.log(LogType::INFO, format_args!($($arg)*));
}
Expand All @@ -96,7 +95,7 @@ macro_rules! log_info {
macro_rules! log_warn {
($($arg:tt)*) => {
{
use crate::logger::*;
use crate::cli::logger::*;
let logger = LOGGER.lock().unwrap();
logger.log(LogType::WARNING, format_args!($($arg)*));
}
Expand All @@ -107,7 +106,7 @@ macro_rules! log_warn {
macro_rules! log_success {
($($arg:tt)*) => {
{
use crate::logger::*;
use crate::cli::logger::*;
let logger = LOGGER.lock().unwrap();
logger.log(LogType::SUCCESS, format_args!($($arg)*));
}
Expand All @@ -118,7 +117,7 @@ macro_rules! log_success {
macro_rules! log_error {
($($arg:tt)*) => {
{
use crate::logger::*;
use crate::cli::logger::*;
let logger = LOGGER.lock().unwrap();
logger.log(LogType::ERROR, format_args!($($arg)*));
}
Expand All @@ -129,7 +128,7 @@ macro_rules! log_error {
macro_rules! log_debug {
($($arg:tt)*) => {
{
use crate::logger::*;
use crate::cli::logger::*;
let logger = LOGGER.lock().unwrap();
logger.log(LogType::DEBUG, format_args!($($arg)*));
}
Expand All @@ -140,7 +139,7 @@ macro_rules! log_debug {
macro_rules! log_fatal {
($($arg:tt)*) => {
{
use crate::logger::*;
use crate::cli::logger::*;
let logger = LOGGER.lock().unwrap();
logger.log(LogType::FATAL, format_args!($($arg)*));
panic!()
Expand Down
34 changes: 34 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{io, time::Instant};
use lockbox::{cli, log_fatal, log_success};

fn main() -> io::Result<()> {
let start_time = Instant::now();
let args = cli::get_command().get_matches();

cli::logger::configure_logger(&args);

/* BOX */
if let Some(args) = args.subcommand_matches("box") {
match cli::commands::r#box(args) {
Ok(file_count) => log_success!("Total files encrypted: {}", file_count),
Err(err) => log_fatal!("Encryption failed: {}", err)
}
}

/* UNBOX */
if let Some(args) = args.subcommand_matches("unbox") {
match cli::commands::unbox(args) {
Ok(file_count) => log_success!("Total files decrypted: {}", file_count),
Err(err) => log_fatal!("Decryption failed: {}", err)
}
}

/* KEY */
if let Some(args) = args.subcommand_matches("key") {
cli::commands::key(args)
}

let duration = start_time.elapsed();
log_success!("Time taken: {:.2?}", duration);
Ok(())
}
41 changes: 5 additions & 36 deletions src/main.rs → src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
mod commands;
mod encryption;
mod logger;
mod storage;
pub mod commands;
pub mod logger;
pub mod path;

use std::io::{self};
use std::time::Instant;
use clap::{command, Arg, ArgAction, Command};
use clap::{Arg, ArgAction, Command, command};

fn main() -> io::Result<()> {
let start_time = Instant::now();
let args = get_command().get_matches();

logger::configure_logger(&args);

/* BOX */
if let Some(args) = args.subcommand_matches("box") {
let file_count = commands::r#box(args);
log_success!("Total files encrypted: {}", file_count);
}

/* UNBOX */
if let Some(args) = args.subcommand_matches("unbox") {
let file_count = commands::unbox(args);
log_success!("Total files decrypted: {}", file_count);
}

/* KEY */
if let Some(args) = args.subcommand_matches("key") {
commands::key(args)
}

let duration = start_time.elapsed();
log_success!("Time taken: {:.2?}", duration);
Ok(())
}

fn get_command() -> Command {
pub fn get_command() -> Command {
command!()
/* BASE COMMAND */
.arg(Arg::new("debug")
Expand Down
File renamed without changes.
58 changes: 0 additions & 58 deletions src/commands/box.rs

This file was deleted.

23 changes: 0 additions & 23 deletions src/commands/key.rs

This file was deleted.

Loading

0 comments on commit bf3aba2

Please sign in to comment.