aboutsummaryrefslogtreecommitdiff
path: root/src/cryptoblob.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-05-19 14:33:49 +0200
committerAlex Auvolat <alex@adnab.me>2022-05-19 14:33:49 +0200
commit6be90936a108d971e0cfa3ddaa9c2d54557e30f3 (patch)
treeed0dae1b4ebf3215b808b81d9980376e9b5dee26 /src/cryptoblob.rs
parent1ac56a91981bee4867dfb054bd2199c6111fe1eb (diff)
downloadaerogramme-6be90936a108d971e0cfa3ddaa9c2d54557e30f3.tar.gz
aerogramme-6be90936a108d971e0cfa3ddaa9c2d54557e30f3.zip
Some refactoring
Diffstat (limited to 'src/cryptoblob.rs')
-rw-r--r--src/cryptoblob.rs13
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());