Crates.io | redseat-crypto |
lib.rs | redseat-crypto |
version | 0.1.0 |
source | src |
created_at | 2024-04-07 22:03:51.161175 |
updated_at | 2024-04-07 22:03:51.161175 |
description | Common description for plugin creation |
homepage | https://github.com/neckaros/redseat-crypto/ |
repository | https://github.com/neckaros/redseat-crypto/ |
max_upload_size | |
id | 1199569 |
size | 31,820 |
All in one stream encoding
No need to understand crypto: Wrap your Reader / Writer width one of AesReader / AesWriter and read/write!
All you need to create a Reader is
const SALT: &str = "e5709660b22ab0803630cb963f703b83";
let salt = URL_SAFE.decode(SALT).unwrap();
let key = derive_key("test password", &salt);
All you need to create a writer is
random_iv(None)
functionRedSeat Crypto is used by the RedSeat app (for encrypted backup and encrypted libraries) and add header that you can customize inside the crypted file:
pub struct CryptoInfo{
pub thumb: Option<Vec<u8>>,
pub mime: Option<String>,
pub thumb_mime: Option<String>,
pub info: Option<String>,
}
All field are optional but it allows you to store in the crypted file thumb data and any info in String format (for example serialized JSON).
Therefore just by reading the header you can extract the thumb or info without reading the full file:
u32::from_be_bytes()
)u32::from_be_bytes()
)String::from_utf8()
)String::from_utf8()
)For exemple if T (thumb size) is 240: Thumb will be at byptes [313 to 553]
Next versions will provide easy functionality to retreive Thumb and Info
Exemple usage with information:
fn encrypt_with_infos() -> Result<()> {
let infos = CryptoInfo {
thumb: Some(random_iv(Some(145))),
mime: Some("video/quicktime-mov".to_owned()),
thumb_mime: Some("image/jpeg".to_owned()),
info: Some("{JSONEXEMPLE====}".to_owned()),
};
let test_path = PathBuf::from_str("test_data/test_infos").unwrap();
if test_path.exists() {
remove_file(&test_path)?;
}
let salt = URL_SAFE.decode(SALT).unwrap();
let key = derive_key("test password", &salt);
let iv = random_iv(None);
{
let writer = File::create(&test_path)?;
let encryptor = AesSafe256Encryptor::new(&key);
let mut aes_writer = AesWriter::new_with_infos(iv, infos.clone(), writer, encryptor)?;
aes_writer.encrypt_write("test encrypted".as_bytes(), true)?;
}
{
let reader = File::open(&test_path)?;
let decryptor = AesSafe256Decryptor::new(&key);
let (dec_infos ,mut aes_reader) = AesReader::new_with_infos(reader, decryptor)?;
let mut decrypted = String::new();
assert_eq!(infos, dec_infos, "infos different than initial infos");
aes_reader.read_to_string(&mut decrypted)?;
assert_eq!(decrypted, "test encrypted", "Decrypted different than encprypted");
}
Ok(())
}
See the tests for more examples
Encryption used: AES256 with CBC Block Mode