Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Example Documentation #7

Open
1 of 12 tasks
WingZer0o opened this issue Nov 21, 2024 · 14 comments
Open
1 of 12 tasks

Create Example Documentation #7

WingZer0o opened this issue Nov 21, 2024 · 14 comments

Comments

@WingZer0o
Copy link
Member

WingZer0o commented Nov 21, 2024

@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.

  • Asymmetric
  • Compression
  • Digital Signature
  • ecdsa
  • hashers
  • hybrid
  • key_exchange
  • message
  • password_hashers
  • signatures
  • sponges
  • symmetric
@WingZer0o WingZer0o changed the title Create Documentation Create Example Documentation Nov 21, 2024
@WingZer0o
Copy link
Member Author

WingZer0o commented Nov 23, 2024

@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);
}

@WingZer0o
Copy link
Member Author

I created a .md file at this location, https://github.com/Cryptographic-API-Services/cas-lib/blob/main/docs/EXAMPLES.md

@rakurame96
Copy link
Contributor

Hello Michael,

Thank you for providing the markdown file. I will begin working on these examples next week and assign this ticket to myself.

@rakurame96
Copy link
Contributor

Hi @WingZer0o,

I just created a PR (#15) with the above example, placed inside tests/ folder. Created binary crate with name symmetric and added the code. Do you have any other ideas, or continue the current approach?

@WingZer0o
Copy link
Member Author

WingZer0o commented Nov 26, 2024 via email

@rakurame96
Copy link
Contributor

Hi,
I managed to update the symmetric test to invoke upon cargo test command. I pushed the changes to the same PR. If it is good, then I will slowly start for other algorithms as well.

@WingZer0o
Copy link
Member Author

WingZer0o commented Nov 29, 2024 via email

WingZer0o added a commit that referenced this issue Dec 4, 2024
@WingZer0o
Copy link
Member Author

@rakurame96 I started working on the hasher test cases here. I still have to create example but this fail and success comparison will be a good addition to the test suite.

https://github.com/Cryptographic-API-Services/cas-lib/blob/main/tests/hashers.rs

@rakurame96
Copy link
Contributor

Thanks for the info. For next tests, which one should I consider?

So far, hashers & symmetric has been in good shape with more scenarios can be added in future.

@WingZer0o
Copy link
Member Author

I thinking wrapping up the threadpool methods for the SHA3 hashes would be best and moving onto the blake2 hash. If you wanna take those up lemme know.

आपकी सहायता के लिए धन्यवाद

@rakurame96
Copy link
Contributor

Sure, but would require some assistance from you in this regard.

@WingZer0o
Copy link
Member Author

Honestly the best way to go about this would be to mimic the existing test cases for the SHA256 and SHA512 test cases that I already created, the threadpool methods just have the characters _threadpool appended on it. Here is an example for SHA256 threadpool.

fn test_sha_256_threadpool_compare_fail() {
        let path = Path::new("tests/test.docx");
        let file_bytes: Vec<u8> = std::fs::read(path).unwrap();
        let hash = <CASSHA as CASHasher>::hash_256_threadpool(file_bytes);

        let path_2 = Path::new("tests/test2.docx");
        let file_bytes_2: Vec<u8> = std::fs::read(path_2).unwrap();
        let hash_2 = <CASSHA as CASHasher>::hash_256_threadpool(file_bytes_2);

        assert_ne!(hash, hash_2);
    }

@rakurame96
Copy link
Contributor

Got it. I have created PR for this.

During build, I have noticed many Clippy warnings. Should we add an appropriate command to suppress them or look for permanent fix? Most of them were related to unused variables warnings.

@WingZer0o
Copy link
Member Author

WingZer0o commented Dec 27, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants