Last active
April 29, 2024 15:16
Revisions
-
jaysonsantos revised this gist
Mar 27, 2017 . 1 changed file with 22 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); // 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); 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); } -
jaysonsantos revised this gist
Mar 27, 2017 . 1 changed file with 33 additions and 34 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = [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); } // 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); 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); } -
jaysonsantos created this gist
Mar 27, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()); }