Skip to content

Commit

Permalink
use blake2
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed Sep 21, 2023
1 parent 4d909b0 commit fb6462a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 52 deletions.
42 changes: 6 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ async-trait = "0.1.53"
url = "2.3.1"
serde = {version = "1", features = ["derive"] }
serde_json = "1.0"
sha2 = {version = "0.10", features = ["asm"] }
# blake2 = {version = "0.10"}
blake2s_simd = "1.0"
blake2 = {version = "0.10"}
aes-gcm = "0.10"
hex = "0.4"
lazy_static = "1.4"
Expand Down
22 changes: 9 additions & 13 deletions src/store/bs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use super::{Error, Result, Store};
use crate::fungi::meta::Block;
use aes_gcm::{
aead::{generic_array::GenericArray, Aead, KeyInit},
aead::{Aead, KeyInit},
Aes256Gcm, Nonce,
};
//use blake2::{Blake2s256, Digest};

//type Hasher = Blake2s256;
use blake2::{Blake2s256, Digest};
type Hasher = Blake2s256;

/// The block store builds on top of a store and adds encryption and compression
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -50,31 +49,28 @@ where
pub async fn set(&self, blob: &[u8]) -> Result<Block> {
// we first calculate the hash of the plain-text data

let key = blake2s_simd::blake2s(blob);

let enc_key = GenericArray::from_slice(key.as_array());
//let key = Hasher::digest(blob);
let key = Hasher::digest(blob);
let mut encoder = snap::raw::Encoder::new();
// data is then compressed
let compressed = encoder.compress_vec(blob)?;

// we then encrypt it using the hash of the plain-text as a key
let cipher = Aes256Gcm::new(enc_key);
let cipher = Aes256Gcm::new(&key);
// the nonce is still driven from the key, a nonce is 12 bytes for aes
// it's done like this so a store can still dedup the data
let nonce = Nonce::from_slice(&key.as_bytes()[..12]);
let nonce = Nonce::from_slice(&key[..12]);

// we encrypt the data
let encrypted = cipher
.encrypt(nonce, compressed.as_slice())
.map_err(|_| Error::EncryptionError)?;

// we hash it again, and use that as the store key
let id = blake2s_simd::blake2s(&encrypted);
let id = Hasher::digest(&encrypted);

let block = Block {
id: *id.as_array(),
key: *key.as_array(),
id: id.into(),
key: key.into(),
};

self.store.set(&block.id, &encrypted).await?;
Expand Down

0 comments on commit fb6462a

Please sign in to comment.