We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
In this example I want to encrypt a text that is stored on a file (I tried first to have it provided by stdin but I couldn't make it work).
use std::fs::File; use std::io::Read; use libsodium_sys as sodium; use secrets::{SecretBox, SecretVec}; const KEY_LEN : usize = sodium::crypto_secretbox_KEYBYTES as _; const NONCE_LEN : usize = sodium::crypto_secretbox_NONCEBYTES as _; const MAC_LEN : usize = sodium::crypto_secretbox_MACBYTES as _; pub fn get_ciphertext(key_path: &str, nonce_path: &str, text_path: &str) -> std::io::Result<SecretVec::<u8>>{ let mut key = SecretBox::<[u8; KEY_LEN]>::zero(); let mut nonce = [0; NONCE_LEN]; let mut plaintext = SecretVec::zero(14); File::open(key_path)? .read_exact(key.borrow_mut().as_mut())?; File::open(nonce_path)? .read_exact(&mut nonce)?; File::open(text_path)? .read_exact(plaintext.borrow_mut().as_mut())?; Ok(SecretVec::<u8>::new(plaintext.len() - MAC_LEN, |s| { if -1 == unsafe { sodium::crypto_secretbox_easy( s.as_mut_ptr(), plaintext.borrow().as_ptr(), plaintext.len() as _, nonce.as_ptr(), key.borrow().as_ptr(), ) } { panic!("failed"); } })) }
This is like the decrypt example but using the sodium::crypto_secretbox_easy instead.
sodium::crypto_secretbox_easy
It gives the error thread 'main' panicked at 'attempt to subtract with overflow' probably because of the size given by plaintext.len() - MAC_LEN.
thread 'main' panicked at 'attempt to subtract with overflow'
plaintext.len() - MAC_LEN
Have you any idea on how to fix this?
The text was updated successfully, but these errors were encountered:
Nevermind, I found the error. It was a dumb problem due to copying.
The libsodium call should be like this.
Ok(SecretVec::<u8>::new(plaintext.len() + MAC_LEN, |s| { if -1 == unsafe { sodium::crypto_secretbox_easy( s.as_mut_ptr(), plaintext.borrow().as_ptr(), plaintext.len() as _, nonce.as_ptr(), key.borrow().as_ptr(), ) } { panic!("failed"); } }))
The MAC len has to be added to the length.
Sorry, something went wrong.
No branches or pull requests
In this example I want to encrypt a text that is stored on a file (I tried first to have it provided by stdin but I couldn't make it work).
This is like the decrypt example but using the
sodium::crypto_secretbox_easy
instead.It gives the error
thread 'main' panicked at 'attempt to subtract with overflow'
probably because of the size given byplaintext.len() - MAC_LEN
.Have you any idea on how to fix this?
The text was updated successfully, but these errors were encountered: