Cryptographic-API-Services / cas-lib

Apache License 2.0
1 stars 0 forks source link

Create Example Documentation #7

Open WingZer0o opened 1 day ago

WingZer0o commented 1 day ago

@rakurame96 I am making this ticket as we discussed through email. I will provide some examples of demonstration code in this thread. I think it will be good to have examples for the specific usage case of each cryptographic algorithm from a file.

So for instance I would read a file from disk and get the bytes and perform the necessary operation.

Hashers would compare two hashes of different files and show a true / false example. I can some code examples in this thread for a hash and aes_gcm.

WingZer0o commented 4 hours ago

@rakurame96 I made an example for AES 256 encrypting and decrypting a docx and writing it disk after each round. This way the user can see its encrypted but when the bytes decrypted its the same file that is written to disk as the original.

You can test this by doing a cargo init then doing cargo add cas-lib.

use std::{fs::{File}, io::Write, path::Path};

use cas_lib::symmetric::{aes::CASAES256, cas_symmetric_encryption::CASAESEncryption};

fn main() {
    let path = Path::new("MikeMulchrone_Resume2024.docx");
    let file_bytes: Vec<u8> = std::fs::read(path).unwrap();
    let aes_nonce = <CASAES256 as CASAESEncryption>::generate_nonce();
    let aes_key = <CASAES256 as CASAESEncryption>::generate_key();
    let encrypted_bytes = <CASAES256 as CASAESEncryption>::encrypt_plaintext(aes_key.clone(), aes_nonce.clone(), file_bytes);
    let mut file =  File::create("encrypted.docx").unwrap();
    file.write_all(&encrypted_bytes);

    let path = Path::new("encrypted.docx");
    let file_bytes: Vec<u8> = std::fs::read(path).unwrap();
    let decrypted_bytes = <CASAES256 as CASAESEncryption>::decrypt_ciphertext(aes_key, aes_nonce, file_bytes);
    let mut file =  File::create("decrypted.docx").unwrap();
    file.write_all(&decrypted_bytes);
}