#lcg #random #byte #converter #bits #cipher #chiper

CryptTool

A versatile and efficient cryptography library implemented in Rust

4 releases

0.1.4 Nov 20, 2024
0.1.3 Oct 30, 2024
0.1.2 Oct 29, 2024
0.1.1 Oct 20, 2024

#774 in Cryptography

Download history 150/week @ 2024-10-18 209/week @ 2024-10-25 66/week @ 2024-11-01 13/week @ 2024-11-08 123/week @ 2024-11-15 37/week @ 2024-11-22 5/week @ 2024-11-29 1/week @ 2024-12-06

249 downloads per month
Used in hidden_watermark

MIT license

10KB
155 lines

CryptTool

Source Code: https://github.com/guofei9987/CryptTool

This project is a Tool Box about:

  • Random number generator
  • Cipher
  • Byte Bits Converter

This project is mainly used in https://github.com/guofei9987/hidden_watermark_rs
or other projects.

How to use

use crypt_tool::{system_random, BytesBitsConverter, LCG, XorCipher};
fn example_random() {
    let rand = system_random();
    println!("A random number = {}", rand);

    let seed = b"a seed";
    let mut rnd = LCG::from_seed(seed);
    let rand2 = rnd.generate_u8();
    println!("Another random number from seed {}", rand2);

    let random_str = rnd.generate_random_string(20);
    println!("A random String = {}", random_str);
}
fn example_cipher() {
    let cipher = XorCipher::new("password1".as_bytes());
    let data = vec![0, 255, 128, 64, 32, 16, 8, 4, 2, 1];
    let encoded = cipher.encode(&data);
    let decoded = cipher.decode(&encoded);
    assert_eq!(data, decoded);
}

fn example_data_bin_conversion() {
    let converter = BytesBitsConverter::new();
    let bytes = vec![0, 1, 2, 255];
    let bits = vec![
        0, 0, 0, 0, 0, 0, 0, 0, // 0
        0, 0, 0, 0, 0, 0, 0, 1, // 1
        0, 0, 0, 0, 0, 0, 1, 0, // 2
        1, 1, 1, 1, 1, 1, 1, 1, // 255
    ];
    assert_eq!(converter.bytes_to_bits(&bytes), bits);
    assert_eq!(converter.bits_to_bytes(&bits), bytes);
}

fn main() {
    example_random();
    example_cipher();
    example_data_bin_conversion();
}

No runtime deps