Skip to content

Instantly share code, notes, and snippets.

@jaysonsantos
Last active April 29, 2024 15:16

Revisions

  1. jaysonsantos revised this gist Mar 27, 2017. 1 changed file with 22 additions and 23 deletions.
    45 changes: 22 additions & 23 deletions main.rs
    Original file line number Diff line number Diff line change
    @@ -28,34 +28,33 @@ fn main() {
    let mut in_out = content.clone();

    // The input/output variable need some space for a suffix
    println!("Tag len {}", CHACHA20_POLY1305.tag_len());
    for _ in 0..CHACHA20_POLY1305.tag_len() {
    in_out.push(0);
    }

    // Opening key used to decrypt data
    let opening_key = OpeningKey::new(&CHACHA20_POLY1305, &key).unwrap();
    println!("Tag len {}", CHACHA20_POLY1305.tag_len());
    for _ in 0..CHACHA20_POLY1305.tag_len() {
    in_out.push(0);
    }

    // Sealing key used to encrypt data
    let sealing_key = SealingKey::new(&CHACHA20_POLY1305, &key).unwrap();
    // Opening key used to decrypt data
    let opening_key = OpeningKey::new(&CHACHA20_POLY1305, &key).unwrap();

    // Random data must be used only once per encryption
    let mut nonce = vec![0; 12];
    // Sealing key used to encrypt data
    let sealing_key = SealingKey::new(&CHACHA20_POLY1305, &key).unwrap();

    // Fill nonce with random data
    let rand = SystemRandom::new();
    rand.fill(&mut nonce).unwrap();
    // Random data must be used only once per encryption
    let mut nonce = vec![0; 12];

    // Encrypt data into in_out variable
    let output_size = seal_in_place(&sealing_key, &nonce, &additional_data, &mut in_out,
    CHACHA20_POLY1305.tag_len()).unwrap();
    // Fill nonce with random data
    let rand = SystemRandom::new();
    rand.fill(&mut nonce).unwrap();

    println!("Encrypted data's size {}", output_size);
    // Encrypt data into in_out variable
    let output_size = seal_in_place(&sealing_key, &nonce, &additional_data, &mut in_out,
    CHACHA20_POLY1305.tag_len()).unwrap();

    let decrypted_data = open_in_place(&opening_key, &nonce, &additional_data,
    0, &mut in_out).unwrap();
    println!("Encrypted data's size {}", output_size);

    println!("{:?}", String::from_utf8(decrypted_data.to_vec()).unwrap());
    assert_eq!(content, decrypted_data);
    let decrypted_data = open_in_place(&opening_key, &nonce, &additional_data,
    0, &mut in_out).unwrap();

    }
    println!("{:?}", String::from_utf8(decrypted_data.to_vec()).unwrap());
    assert_eq!(content, decrypted_data);
    }
  2. jaysonsantos revised this gist Mar 27, 2017. 1 changed file with 33 additions and 34 deletions.
    67 changes: 33 additions & 34 deletions main.rs
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,20 @@
    extern crate ring;

    use ring::aead::*;
    use ring::pbkdf2::*;
    use ring::rand::SystemRandom;

    fn main() {
    // The password will be used to generate a key
    let password = b"nice password";

    // Usually the salt has some random data and something that relates to the user
    // like an username
    let salt = [0, 1, 2, 3, 4, 5, 6, 7];

    // Keys are sent as &[T] and must have 32 bytes
    let mut key = b"nice password?".to_vec();
    for i in 0..32 - key.len() {
    key.push(0);
    }
    let mut key = [0; 32];
    derive(&HMAC_SHA256, 100, &salt, &password[..], &mut key);

    // Your private data
    let content = b"content to encrypt".to_vec();
    @@ -22,41 +28,34 @@ fn main() {
    let mut in_out = content.clone();

    // The input/output variable need some space for a suffix
    println!("Tag len {}", CHACHA20_POLY1305.tag_len());
    for _ in 0..CHACHA20_POLY1305.tag_len() {
    in_out.push(0);
    }
    println!("Tag len {}", CHACHA20_POLY1305.tag_len());
    for _ in 0..CHACHA20_POLY1305.tag_len() {
    in_out.push(0);
    }

    // Opening key used to decrypt data
    let opening_key = OpeningKey::new(&CHACHA20_POLY1305, &key).unwrap();

    // Opening key used to decrypt data
    let opening_key = OpeningKey::new(&CHACHA20_POLY1305, &key).unwrap();
    // Sealing key used to encrypt data
    let sealing_key = SealingKey::new(&CHACHA20_POLY1305, &key).unwrap();

    // Sealing key used to encrypt data
    let sealing_key = SealingKey::new(&CHACHA20_POLY1305, &key).unwrap();
    // Random data must be used only once per encryption
    let mut nonce = vec![0; 12];

    // Random data must be used only once per encryption
    let mut nonce = vec![0; 12];
    // Fill nonce with random data
    let rand = SystemRandom::new();
    rand.fill(&mut nonce).unwrap();

    // Fill nonce with random data
    let rand = SystemRandom::new();
    rand.fill(&mut nonce).unwrap();
    // Encrypt data into in_out variable
    let output_size = seal_in_place(&sealing_key, &nonce, &additional_data, &mut in_out,
    CHACHA20_POLY1305.tag_len()).unwrap();

    // Encrypt data into in_out variable
    let output_size = seal_in_place(&sealing_key,
    &nonce,
    &additional_data,
    &mut in_out,
    CHACHA20_POLY1305.tag_len())
    .unwrap();
    println!("Encrypted data's size {}", output_size);

    println!("Encrypted data's size {}", output_size);
    let decrypted_data = open_in_place(&opening_key, &nonce, &additional_data,
    0, &mut in_out).unwrap();

    rand.fill(&mut nonce).unwrap();
    let decrypted_data = open_in_place(&opening_key,
    &nonce,
    &additional_data,
    CHACHA20_POLY1305.tag_len(),
    &mut in_out)
    .unwrap();
    println!("{:?}", String::from_utf8(decrypted_data.to_vec()).unwrap());
    assert_eq!(content, decrypted_data);

    println!("{}", String::from_utf8(decrypted_data.to_vec()).unwrap());
    }
    }
  3. jaysonsantos created this gist Mar 27, 2017.
    62 changes: 62 additions & 0 deletions main.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    extern crate ring;

    use ring::aead::*;
    use ring::rand::SystemRandom;

    fn main() {
    // Keys are sent as &[T] and must have 32 bytes
    let mut key = b"nice password?".to_vec();
    for i in 0..32 - key.len() {
    key.push(0);
    }

    // Your private data
    let content = b"content to encrypt".to_vec();
    println!("Content to encrypt's size {}", content.len());

    // Additional data that you would like to send and it would not be encrypted but it would
    // be signed
    let additional_data: [u8; 0] = [];

    // Ring uses the same input variable as output
    let mut in_out = content.clone();

    // The input/output variable need some space for a suffix
    println!("Tag len {}", CHACHA20_POLY1305.tag_len());
    for _ in 0..CHACHA20_POLY1305.tag_len() {
    in_out.push(0);
    }

    // Opening key used to decrypt data
    let opening_key = OpeningKey::new(&CHACHA20_POLY1305, &key).unwrap();

    // Sealing key used to encrypt data
    let sealing_key = SealingKey::new(&CHACHA20_POLY1305, &key).unwrap();

    // Random data must be used only once per encryption
    let mut nonce = vec![0; 12];

    // Fill nonce with random data
    let rand = SystemRandom::new();
    rand.fill(&mut nonce).unwrap();

    // Encrypt data into in_out variable
    let output_size = seal_in_place(&sealing_key,
    &nonce,
    &additional_data,
    &mut in_out,
    CHACHA20_POLY1305.tag_len())
    .unwrap();

    println!("Encrypted data's size {}", output_size);

    rand.fill(&mut nonce).unwrap();
    let decrypted_data = open_in_place(&opening_key,
    &nonce,
    &additional_data,
    CHACHA20_POLY1305.tag_len(),
    &mut in_out)
    .unwrap();

    println!("{}", String::from_utf8(decrypted_data.to_vec()).unwrap());
    }