Skip to content

Commit

Permalink
move hash_topic function to public utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Apr 1, 2022
1 parent 142a1b4 commit c10af71
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ colmeia-hyperswarm-mdns = { git = "https://github.com/bltavares/colmeia.git", re
libutp-rs = { git = "https://github.com/Frando/libutp-rs.git", branch = "feat/clone2", optional = true }
async-trait = "0.1.53"
async-compat = "0.2.1"
blake2-rfc = "0.2.18"

[dev-dependencies]
env_logger = "0.8.3"
Expand Down
14 changes: 4 additions & 10 deletions examples/hyperchat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use async_std::io::{self, ReadExt, WriteExt};
use async_std::stream::StreamExt;
use async_std::sync::Mutex;
use async_std::task::{self, JoinHandle};
use blake2_rfc::blake2b::Blake2b;
use clap::Parser;
use futures::future::{select, Either};
use std::convert::TryInto;
use std::net::SocketAddr;
use std::sync::Arc;

use hyperswarm::{BootstrapNode, Config, DhtConfig, Hyperswarm, HyperswarmStream, TopicConfig};
use hyperswarm::{
hash_topic, BootstrapNode, Config, DhtConfig, Hyperswarm, HyperswarmStream, TopicConfig,
};

const DISCOVERY_NS_BUF: &[u8] = b"hyperchat:v0";

Expand Down Expand Up @@ -80,7 +80,7 @@ async fn main() -> anyhow::Result<()> {
let handle = swarm.handle();
let config = TopicConfig::announce_and_lookup();
for topic in join_opts.topics {
let hash = hash_topic(topic.as_bytes());
let hash = hash_topic(DISCOVERY_NS_BUF, topic.as_bytes());
handle.configure(hash, config.clone());
eprintln!("join topic \"{}\": {}", topic, hex::encode(hash));
}
Expand Down Expand Up @@ -277,12 +277,6 @@ async fn connection_loop(
}
}

pub fn hash_topic(key: &[u8]) -> [u8; 32] {
let mut hasher = Blake2b::with_key(32, key);
hasher.update(DISCOVERY_NS_BUF);
hasher.finalize().as_bytes().try_into().unwrap()
}

pub fn random_name() -> String {
let bytes = rand::random::<[u8; 4]>();
hex::encode(&bytes)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use discovery::dht::DhtConfig;
pub use discovery::mdns::MdnsConfig;

pub use hyperswarm_dht::{IdBytes, DEFAULT_BOOTSTRAP};
pub use util::hash_topic;

use transport::combined::CombinedStream;
pub use transport::Connection;
Expand Down
8 changes: 8 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use blake2_rfc::blake2b::Blake2b;
use std::convert::TryInto;
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};

Expand All @@ -7,3 +9,9 @@ pub fn to_socket_addr<A: ToSocketAddrs>(addr: A) -> io::Result<SocketAddr> {
"Invalid socket address",
))
}

pub fn hash_topic(namespace: &[u8], topic: &[u8]) -> [u8; 32] {
let mut hasher = Blake2b::with_key(32, topic);
hasher.update(namespace);
hasher.finalize().as_bytes().try_into().unwrap()
}

0 comments on commit c10af71

Please sign in to comment.