From 0cbb542ccd3b62e7b099ae01e5d58418c254862c Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Sun, 26 Nov 2017 17:28:15 -0800 Subject: move make_discovery_key() to lib.rs --- src/lib.rs | 18 ++++++++++++++++++ src/protocol.rs | 13 +------------ src/sleep_register.rs | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 694265e..f0059e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,3 +52,21 @@ mod protocol; pub use protocol::*; pub mod network_msgs; pub mod metadata_msgs; + + +use crypto::digest::Digest; +use crypto::blake2b::Blake2b; + +/// Helper to calculate a discovery key from a public key. 'key' should be 32 bytes; the returned +/// array will also be 32 bytes long. +/// +/// dat discovery keys are calculated as a BLAKE2b "keyed hash" (using the passed key) of the string +/// "hypercore" (with no trailing null byte). +pub fn make_discovery_key(key: &[u8]) -> Vec { + let mut discovery_key = [0; 32]; + let mut hash = Blake2b::new_keyed(32, key); + hash.input(&"hypercore".as_bytes()); + hash.result(&mut discovery_key); + discovery_key.to_vec() +} + diff --git a/src/protocol.rs b/src/protocol.rs index 6028bf2..5726efd 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -3,8 +3,6 @@ use std::net::TcpStream; use std::time::Duration; use std::io::{Read, Write}; use std::cmp; -use crypto::digest::Digest; -use crypto::blake2b::Blake2b; use sodiumoxide::crypto::stream::*; use rand::{OsRng, Rng}; use protobuf::Message; @@ -14,6 +12,7 @@ use integer_encoding::{VarIntReader, VarIntWriter}; use errors::*; use network_msgs::*; use metadata_msgs::Index; +use make_discovery_key; #[derive(Debug)] pub enum DatNetMessage { @@ -130,16 +129,6 @@ fn test_bsxii_continued() { assert_eq!(a, c); } -// TODO: move to lib.rs? -pub fn make_discovery_key(key: &[u8]) -> Vec { - // calculate discovery key - let mut discovery_key = [0; 32]; - let mut hash = Blake2b::new_keyed(32, key); - hash.input(&"hypercore".as_bytes()); - hash.result(&mut discovery_key); - discovery_key.to_vec() -} - /// Represents a bi-directional connection to a network peer /// /// Spec says nonce is 32 bytes, by dat implementation (hypercore-protocol) is 24 bytes. diff --git a/src/sleep_register.rs b/src/sleep_register.rs index c169cbb..e0ea5a4 100644 --- a/src/sleep_register.rs +++ b/src/sleep_register.rs @@ -13,7 +13,7 @@ use rand::{OsRng, Rng}; use errors::*; use sleep_file::*; -use protocol::make_discovery_key; +use make_discovery_key; /// Abstract access to Hypercore register pub trait HyperRegister { -- cgit v1.2.3