#![deny(missing_docs)]
#![deny(unsafe_code)]
#![warn(clippy::all)]
#![warn(rust_2018_idioms)]
pub mod config;
pub mod encryption;
pub mod env;
pub mod error;
pub mod keys;
pub mod stream;
pub mod symmetric;
pub use config::Config;
pub use encryption::HybridCipher;
pub use error::{FluxError, Result};
pub use symmetric::SymmetricCipher;
use encryption::hybrid::HybridCipher as InternalHybridCipher;
use keys::{KeyPair, PrivateKey, PublicKey};
use std::path::Path;
use stream::{FileStreamCipher, StreamCipher};
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug)]
pub struct Cryptum {
config: Config,
hybrid_cipher: InternalHybridCipher,
stream_cipher: StreamCipher,
file_cipher: FileStreamCipher,
}
impl Cryptum {
pub fn new(config: Config) -> Result<Self> {
config.validate()?;
Ok(Self {
hybrid_cipher: InternalHybridCipher::new(config.clone()),
stream_cipher: StreamCipher::new(config.clone()),
file_cipher: FileStreamCipher::new(config.clone()),
config,
})
}
pub fn with_defaults() -> Result<Self> {
Self::new(Config::default())
}
pub fn builder() -> CryptumBuilder {
CryptumBuilder::new()
}
pub fn config(&self) -> &Config {
&self.config
}
pub fn generate_keypair(&self, key_size: usize) -> Result<KeyPair> {
KeyPair::generate(key_size)
}
pub fn encrypt(&self, public_key: &PublicKey, plaintext: &[u8]) -> Result<Vec<u8>> {
self.hybrid_cipher.encrypt(public_key, plaintext)
}
pub fn decrypt(&self, private_key: &PrivateKey, ciphertext: &[u8]) -> Result<Vec<u8>> {
self.hybrid_cipher.decrypt(private_key, ciphertext)
}
pub fn encrypt_file<P: AsRef<Path>>(
&self,
input_path: P,
output_path: P,
public_key: &PublicKey,
) -> Result<u64> {
self.file_cipher
.encrypt_file(input_path, output_path, public_key, None)
}
pub fn decrypt_file<P: AsRef<Path>>(
&self,
input_path: P,
output_path: P,
private_key: &PrivateKey,
) -> Result<u64> {
self.file_cipher
.decrypt_file(input_path, output_path, private_key, None)
}
pub fn encrypt_file_with_progress<P: AsRef<Path>>(
&self,
input_path: P,
output_path: P,
public_key: &PublicKey,
progress: stream::ProgressCallback,
) -> Result<u64> {
self.file_cipher
.encrypt_file(input_path, output_path, public_key, Some(progress))
}
pub fn decrypt_file_with_progress<P: AsRef<Path>>(
&self,
input_path: P,
output_path: P,
private_key: &PrivateKey,
progress: stream::ProgressCallback,
) -> Result<u64> {
self.file_cipher
.decrypt_file(input_path, output_path, private_key, Some(progress))
}
pub fn hybrid_cipher(&self) -> &InternalHybridCipher {
&self.hybrid_cipher
}
pub fn stream_cipher(&self) -> &StreamCipher {
&self.stream_cipher
}
pub fn file_cipher(&self) -> &FileStreamCipher {
&self.file_cipher
}
pub fn batch_processor(&self) -> stream::BatchProcessor {
stream::BatchProcessor::new(self.config.clone())
}
}
#[derive(Debug, Default)]
pub struct CryptumBuilder {
config_builder: config::ConfigBuilder,
}
impl CryptumBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn cipher_suite(mut self, cipher_suite: config::CipherSuite) -> Self {
self.config_builder = self.config_builder.cipher_suite(cipher_suite);
self
}
pub fn rsa_key_size(mut self, key_size: config::RsaKeySize) -> Self {
self.config_builder = self.config_builder.rsa_key_size(key_size);
self
}
pub fn memory_limit_mb(mut self, limit: usize) -> Self {
self.config_builder = self.config_builder.memory_limit_mb(limit);
self
}
pub fn hardware_acceleration(mut self, enable: bool) -> Self {
self.config_builder = self.config_builder.hardware_acceleration(enable);
self
}
pub fn stream_chunk_size(mut self, size: usize) -> Self {
self.config_builder = self.config_builder.stream_chunk_size(size);
self
}
pub fn secure_memory(mut self, enable: bool) -> Self {
self.config_builder = self.config_builder.secure_memory(enable);
self
}
pub fn build(self) -> Result<Cryptum> {
let config = self.config_builder.build()?;
Cryptum::new(config)
}
}
pub fn cryptum() -> Result<Cryptum> {
Cryptum::with_defaults()
}