[][src]Crate enc_file

Enc_File

Encrypt / decrypt files or calculate hash from the command line. Warning: This crate hasn't been audited or reviewed in any sense. I created it to easily encrypt und decrypt non-important files which won't cause harm if known by third parties. Don't use for anything important, use VeraCrypt or similar instead.

Breaking change in Version 0.3: Changed input of some functions. To encrypt/decrypt and hash use e.g. "encrypt_chacha(readfile(example.file).unwrap(), key).unwrap()". Using a keymap to work with several keys conveniently. You can import your old keys, using "Add key" -> "manually".

Breaking change in Version 0.2: Using XChaCha20Poly1305 as default encryption/decryption. AES is still available using encrypt_aes or decrypt_aes to maintain backwards compability.

Uses XChaCha20Poly1305 (https://docs.rs/chacha20poly1305) or AES-GCM-SIV (https://docs.rs/aes-gcm-siv) for encryption, bincode (https://docs.rs/bincode) for encoding and BLAKE3 (https://docs.rs/blake3) or SHA256 / SHA512 (https://docs.rs/sha2) for hashing.

Encrypted files are (and have to be) stored as .crpt.

Can be used as library and a binary target. Install via cargo install enc_file

Panics at errors making safe execution impossible.

Examples

use enc_file::{encrypt_chacha, decrypt_chacha, read_file};

//Plaintext to encrypt
let text = b"This a test";
//Provide key. Key will normally be chosen from keymap and provided to the encrypt_chacha() function
let key: &str = "an example very very secret key.";
//Convert text to Vec<u8>
let text_vec = text.to_vec();

//Encrypt text
//Ciphertext stores the len() of encrypted content, the nonce and the actual ciphertext using bincode
let ciphertext = encrypt_chacha(text_vec, key).unwrap(); //encrypt vec<u8>, returns result(Vec<u8>)
//let ciphertext = encrypt_chacha(read_file(example.file).unwrap(), key).unwrap(); //read a file as Vec<u8> and then encrypt
//Check that plaintext != ciphertext
assert_ne!(&ciphertext, &text);

//Decrypt ciphertext to plaintext
let plaintext = decrypt_chacha(ciphertext, key).unwrap();
//Check that text == plaintext
assert_eq!(format!("{:?}", text), format!("{:?}", plaintext));
use enc_file::{get_blake3_hash};

let test = b"Calculating the BLAKE3 Hash of this text";
let test_vec = test.to_vec(); //Convert text to Vec<u8>
let hash1 = get_blake3_hash(test_vec.clone()).unwrap();
let hash2 = get_blake3_hash(test_vec).unwrap();
assert_eq!(hash1, hash2); //Make sure hash1 == hash2
let test2 = b"Calculating the BLAKE3 Hash of this text."; //"." added at the end
let test2_vec = test2.to_vec();
let hash3 = get_blake3_hash(test2_vec).unwrap();
assert_ne!(hash1, hash3); //check that the added "." changes the hash and hash1 != hash3

See https://github.com/LazyEmpiricist/enc_file

Functions

add_key

Adds key to keymap. Taking a keymap "keymap_plaintext" and user provided password.

choose_hashing_function

Allows user to choose desired hashing function. Returns result.

create_new_keyfile

Creates a new keyfile. User can choose to create a random key or manually enter 32-long char-utf8 password in a keyfile. Key has to be valid utf8. Resturns result (password, keyfile and bool (true if new keyfile way created)).

decrypt_aes

Decrypts ciphertext (Vec) with a key (&str) using AES256 GCM SIV. Panics with wrong key. Returns result (cleartext as Vec).

decrypt_chacha

Decrypts ciphertext (Vec) with a key (&str) using XChaCha20Poly1305 (24-byte nonce as compared to 12-byte in ChaCha20Poly1305). Panics with wrong key. Returns result (cleartext as Vec).

decrypt_file

Decrypts file. Taking a keymap "keymap_plaintext" and the choosen encryption "enc" ("chacha" for ChaCha20Poly1305 or "aes" for AES256-GCM-SIV). Returns result.

encrypt_aes

Examples

encrypt_chacha

Encrypts cleartext (Vec) with a key (&str) using XChaCha20Poly1305 (24-byte nonce as compared to 12-byte in ChaCha20Poly1305). Returns result (ciphertext as Vec).

encrypt_file

Encrypts file. Taking a keymap "keymap_plaintext" and the choosen encryption "enc" ("chacha" for ChaCha20Poly1305 or "aes" for AES256-GCM-SIV). Returns result.

encrypt_hashmap

Encrypt a given hashmap with a given password using ChaCha20Poly1305. Returns result (Vec)

get_blake3_hash

Get BLAKE3 Hash from data. File needs to be read as Vac (e.g. use enc_file::read_file()). Returns result.

get_input_string

Reads userinput from stdin and returns it as String. Returns result.

get_sha256_hash

Get SHA256 Hash from data. File needs to be read as Vac (e.g. use enc_file::read_file()). Returns result.

get_sha512_hash

Get SHA512 Hash from data. File needs to be read as Vac (e.g. use enc_file::read_file()). Returns result.

read_file

Reads file from same folder as Vec. Returns result.

read_keyfile

Read keyfile to keymap. Asks for userpassword. Returns result (password, keymap and bool(false as no new keymap was created))

remove_key

Removes choosen key from keymap. Taking a keymap "keymap_plaintext" and user provided password.

save_file

Saves file to same folder. Returns result