diff options
Diffstat (limited to 'src/cryptoblob.rs')
-rw-r--r-- | src/cryptoblob.rs | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/cryptoblob.rs b/src/cryptoblob.rs index 6e51dbb..ad05521 100644 --- a/src/cryptoblob.rs +++ b/src/cryptoblob.rs @@ -5,17 +5,22 @@ use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; use zstd::stream::{decode_all as zstd_decode, encode_all as zstd_encode}; -use sodiumoxide::crypto::secretbox::xsalsa20poly1305::{self, gen_nonce, Nonce, NONCEBYTES}; +use sodiumoxide::crypto::secretbox::xsalsa20poly1305 as secretbox; +use sodiumoxide::crypto::box_ as publicbox; + pub use sodiumoxide::crypto::secretbox::xsalsa20poly1305::{gen_key, Key, KEYBYTES}; +pub use sodiumoxide::crypto::box_::{gen_keypair, PublicKey, SecretKey, PUBLICKEYBYTES, SECRETKEYBYTES}; pub fn open(cryptoblob: &[u8], key: &Key) -> Result<Vec<u8>> { + use secretbox::{NONCEBYTES, Nonce}; + if cryptoblob.len() < NONCEBYTES { return Err(anyhow!("Cyphertext too short")); } // Decrypt -> get Zstd data let nonce = Nonce::from_slice(&cryptoblob[..NONCEBYTES]).unwrap(); - let zstdblob = xsalsa20poly1305::open(&cryptoblob[NONCEBYTES..], &nonce, key) + let zstdblob = secretbox::open(&cryptoblob[NONCEBYTES..], &nonce, key) .map_err(|_| anyhow!("Could not decrypt blob"))?; // Decompress zstd data @@ -26,13 +31,15 @@ pub fn open(cryptoblob: &[u8], key: &Key) -> Result<Vec<u8>> { } pub fn seal(plainblob: &[u8], key: &Key) -> Result<Vec<u8>> { + use secretbox::{NONCEBYTES, gen_nonce}; + // Compress data using zstd let mut reader = &plainblob[..]; let zstdblob = zstd_encode(&mut reader, 0)?; // Encrypt let nonce = gen_nonce(); - let cryptoblob = xsalsa20poly1305::seal(&zstdblob, &nonce, key); + let cryptoblob = secretbox::seal(&zstdblob, &nonce, key); let mut res = Vec::with_capacity(NONCEBYTES + cryptoblob.len()); res.extend(nonce.as_ref()); |