use orion::aead as crypto;
pub fn enrypt_string(string: String, password: String) -> Result<Vec<u8>, orion::errors::UnknownCryptoError>{
println!("{:?}", password.as_bytes());
let passphrase = crypto::SecretKey::from_slice(password.as_bytes()).expect("Failed to encrypt password");
println!("{:?} {:?}", passphrase, string.as_bytes());
let ciphertext = crypto::seal(&passphrase, &string.as_bytes()).expect("Failed to encrypt text");
Ok(ciphertext)
}Panics at |
Answered by
brycx
Jun 4, 2021
Replies: 2 comments 11 replies
|
I changed the secretkey method to default() and it works now, but I want a user inputted password |
0 replies
|
Hi! If you take a peek at the errors documentation, you'll see that the first one says that the In practice, please note that using user-passwords directly to encrypt data is not secure. You can use Don't hesitate to let us know if there's anything else! |
11 replies
Answer selected by
Jytesh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
If you take a peek at the errors documentation, you'll see that the first one says that the
secret_keymust be 32 bytes. The password you use here is however only 14 bytes. You can use the.len()function on aSecretKeyinstance to check if it is upheld.default()randomly generates 32 bytes, which is why it works.In practice, please note that using user-passwords directly to encrypt data is not secure. You can use
orion::kdfto "stretch" the password before using it for encryption. There is also an example in that module for this exact use-case.Don't hesitate to let us know if there's anything else!