Skip to content
New issue

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

Add BaoHasher #30

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "carbonado"
version = "0.4.2"
edition = "2021"
version = "0.5.0"
license = "MIT"
description = "An apocalypse-resistant data storage format for the truly paranoid."
documentation = "https://docs.rs/carbonado"
Expand All @@ -12,7 +12,6 @@ include = ["src/**/*", "LICENSE", "README.md"]
[dependencies]
bao = "0.12.1"
bech32 = "0.11.0"
blake3 = "1.5.0"
bitmask-enum = "2.1.0"
bytes = "1.4.0"
ecies = { version = "0.2.6", default-features = false, features = [
Expand All @@ -26,7 +25,7 @@ nom = "7.1.3"
nostr = "0.28.1"
nostr-sdk = "0.28.0"
pretty_env_logger = "0.5.0"
secp256k1 = { version = "0.28.0", features = [
secp256k1 = { version = "0.29.0", features = [
"global-context",
"rand-std",
"serde",
Expand All @@ -36,6 +35,7 @@ snap = "1.1.0"
thiserror = "1.0"
zfec-rs = "0.1.0"
once_cell = "1.19.0"
libsecp256k1-core = "0.3.0"

[dev-dependencies]
anyhow = "1.0.71"
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum CarbonadoError {

/// ecies error
#[error(transparent)]
EciesError(#[from] ecies::SecpError),
EciesError(#[from] libsecp256k1_core::Error),

/// bao decode error
#[error(transparent)]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ pub use decoding::verify_slice;
pub use decoding::scrub;

pub use bao;
pub use blake3;
pub use secp256k1;
85 changes: 72 additions & 13 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::sync::Once;
use std::{
fmt,
io::{Cursor, Write},
sync::{Arc, Once, RwLock},
};

use bao::Hash;
use bao::{encode::Encoder, Hash};
use bech32::{decode, encode, Bech32m, Hrp};
use log::trace;

Expand Down Expand Up @@ -64,15 +68,70 @@ pub fn bech32_decode(bech32_str: &str) -> Result<(String, Vec<u8>), CarbonadoErr
Ok((hrp.to_string(), words))
}

pub fn pub_keyed_hash(ss: &str, bytes: &[u8]) -> Result<String, CarbonadoError> {
let key = hex::decode(ss)?;
let key_bytes: [u8; 32] = key[0..32].try_into()?;
let secp = secp256k1::Secp256k1::new();
let secret_key = secp256k1::SecretKey::from_slice(key_bytes.as_slice())?;
let pk = secret_key.public_key(&secp);
let (pk, _parity) = pk.x_only_public_key();
let pub_keyed_hash = blake3::keyed_hash(&pk.serialize(), bytes)
.to_hex()
.to_string();
Ok(pub_keyed_hash)
#[derive(Clone, Debug)]
pub struct BaoHash(pub bao::Hash);

impl BaoHash {
pub fn to_bytes(&self) -> Vec<u8> {
let Self(hash) = self;

hash.as_bytes().to_vec()
}
}

impl fmt::Display for BaoHash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Self(hash) = self;

f.write_str(&hash.to_string())
}
}

impl From<&[u8]> for BaoHash {
fn from(value: &[u8]) -> Self {
let mut hash = [0_u8; 32];
hash.copy_from_slice(&value[0..32]);
Self(bao::Hash::from(hash))
}
}

impl From<bao::Hash> for BaoHash {
fn from(hash: bao::Hash) -> Self {
Self(hash)
}
}

/// A threadsafe bao hasher similar to that provided by blake3
pub struct BaoHasher {
encoder: Arc<RwLock<Encoder<Cursor<Vec<u8>>>>>,
}

impl BaoHasher {
pub fn new() -> Arc<Self> {
let data = Vec::new();
let cursor = Cursor::new(data.clone());
let encoder = Encoder::new(cursor);

let bao_hasher = Self {
encoder: Arc::new(RwLock::new(encoder)),
};

Arc::new(bao_hasher)
}

pub fn update(&self, buf: &[u8]) {
let mut encoder = self.encoder.write().expect("encoder write lock");
encoder.write_all(buf).expect("write to encoder");
}

pub fn finalize(&self) -> BaoHash {
let mut encoder = self.encoder.write().expect("encoder read lock");
BaoHash::from(encoder.finalize().expect("finalize bao hash"))
}

pub fn read_all(&self) -> Vec<u8> {
let encoder = self.encoder.write().expect("encoder read lock");
let data = encoder.clone().into_inner();
data.into_inner().to_vec()
}
}
1 change: 1 addition & 0 deletions tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ fn codec(path: &str) -> Result<()> {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&file_path)?;
file.write_all(&header_bytes)?;
file.write_all(&encoded)?;
Expand Down
1 change: 1 addition & 0 deletions tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn format() -> Result<()> {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(file_path)?;
file.write_all(&header_bytes)?;
file.write_all(&encoded)?;
Expand Down
Loading