Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Move `hash` module to `radicle-crypto`
Alexis Sellier committed 3 years ago
commit be789a487fe16a0061aa5bccdcd4465aeed74c08
parent 09a8b46f343f1666933ed0bd086a5f8eb2a7815c
11 files changed +83 -89
modified Cargo.lock
@@ -2224,7 +2224,6 @@ dependencies = [
 "radicle-ssh",
 "serde",
 "serde_json",
-
 "sha2 0.10.6",
 "siphasher",
 "sqlite",
 "tempfile",
modified radicle-crypto/Cargo.toml
@@ -10,13 +10,14 @@ edition = "2021"

[features]
test = ["fastrand", "quickcheck"]
-
ssh = ["base64", "radicle-ssh", "sha2", "ssh-key"]
+
ssh = ["base64", "radicle-ssh", "ssh-key"]

[dependencies]
ed25519-compact = { version = "2.0.2", features = ["pem"] }
cyphernet = { version = "0", optional = true }
multibase = { version = "0.9.1" }
serde = { version = "1", features = ["derive"] }
+
sha2 = { version = "0.10.2" }
sqlite = { version = "0.28.1", optional = true }
thiserror = { version = "1" }
zeroize = { version = "1.5.7" }
@@ -47,10 +48,6 @@ version = "0"
default-features = false
optional = true

-
[dependencies.sha2]
-
version = "0.10.2"
-
optional = true
-

[dependencies.base64]
version = "0.13"
optional = true
added radicle-crypto/src/hash.rs
@@ -0,0 +1,69 @@
+
use std::{convert::TryInto, fmt};
+

+
use serde::{Deserialize, Serialize};
+
use sha2::{
+
    digest::{generic_array::GenericArray, OutputSizeUser},
+
    Digest as _, Sha256,
+
};
+
use thiserror::Error;
+

+
#[derive(Debug, Clone, PartialEq, Eq, Error)]
+
pub enum DecodeError {
+
    #[error("invalid digest length {0}")]
+
    InvalidLength(usize),
+
}
+

+
/// A SHA-256 hash.
+
#[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
+
pub struct Digest([u8; 32]);
+

+
impl Digest {
+
    pub fn new(bytes: impl AsRef<[u8]>) -> Self {
+
        Self::from(Sha256::digest(bytes))
+
    }
+
}
+

+
impl AsRef<[u8; 32]> for Digest {
+
    fn as_ref(&self) -> &[u8; 32] {
+
        &self.0
+
    }
+
}
+

+
impl fmt::Debug for Digest {
+
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+
        write!(f, "Hash({})", self)
+
    }
+
}
+

+
impl fmt::Display for Digest {
+
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+
        for byte in &self.0 {
+
            write!(f, "{:02x}", byte)?;
+
        }
+
        Ok(())
+
    }
+
}
+

+
impl From<[u8; 32]> for Digest {
+
    fn from(bytes: [u8; 32]) -> Self {
+
        Self(bytes)
+
    }
+
}
+

+
impl TryFrom<&[u8]> for Digest {
+
    type Error = DecodeError;
+

+
    fn try_from(bytes: &[u8]) -> Result<Self, DecodeError> {
+
        let bytes: [u8; 32] = bytes
+
            .try_into()
+
            .map_err(|_| DecodeError::InvalidLength(bytes.len()))?;
+

+
        Ok(bytes.into())
+
    }
+
}
+

+
impl From<GenericArray<u8, <Sha256 as OutputSizeUser>::OutputSize>> for Digest {
+
    fn from(array: GenericArray<u8, <Sha256 as OutputSizeUser>::OutputSize>) -> Self {
+
        Self(array.into())
+
    }
+
}
modified radicle-crypto/src/lib.rs
@@ -10,6 +10,7 @@ pub use ed25519::{Error, KeyPair, Seed};
#[cfg(any(test, feature = "test"))]
pub mod test;

+
pub mod hash;
#[cfg(feature = "ssh")]
pub mod ssh;

modified radicle-crypto/src/test/arbitrary.rs
@@ -1,6 +1,6 @@
use quickcheck::Arbitrary;

-
use crate::{test::signer::MockSigner, KeyPair, PublicKey, SecretKey, Seed};
+
use crate::{hash, test::signer::MockSigner, KeyPair, PublicKey, SecretKey, Seed};

impl Arbitrary for MockSigner {
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
@@ -22,6 +22,13 @@ impl Arbitrary for PublicKey {
    }
}

+
impl Arbitrary for hash::Digest {
+
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
+
        let bytes: Vec<u8> = Arbitrary::arbitrary(g);
+
        hash::Digest::new(&bytes)
+
    }
+
}
+

#[derive(Clone, Debug)]
pub struct ByteArray<const N: usize>([u8; N]);

modified radicle-node/src/lib.rs
@@ -13,13 +13,13 @@ pub mod tests;
pub mod wire;

pub use nakamoto_net::{Io, Link, LocalDuration, LocalTime};
-
pub use radicle::{collections, crypto, git, hash, identity, node, profile, rad, storage};
+
pub use radicle::{collections, crypto, git, identity, node, profile, rad, storage};

pub mod prelude {
    pub use crate::clock::Timestamp;
+
    pub use crate::crypto::hash::Digest;
    pub use crate::crypto::{PublicKey, Signature, Signer};
    pub use crate::deserializer::Deserializer;
-
    pub use crate::hash::Digest;
    pub use crate::identity::{Did, Id};
    pub use crate::service::filter::Filter;
    pub use crate::service::message::Address;
modified radicle-node/src/wire.rs
@@ -13,11 +13,11 @@ use nakamoto_net as nakamoto;
use nakamoto_net::{Link, LocalTime};

use crate::address;
+
use crate::crypto::hash::Digest;
use crate::crypto::{PublicKey, Signature, Signer, Unverified};
use crate::deserializer::Deserializer;
use crate::git;
use crate::git::fmt;
-
use crate::hash::Digest;
use crate::identity::Id;
use crate::node;
use crate::service;
modified radicle/Cargo.toml
@@ -23,7 +23,6 @@ multibase = { version = "0.9.1" }
log = { version = "0.4.17", features = ["std"] }
once_cell = { version = "1.13" }
olpc-cjson = { version = "0.1.1" }
-
sha2 = { version = "0.10.2" }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
siphasher = { version = "0.3.10" }
deleted radicle/src/hash.rs
@@ -1,69 +0,0 @@
-
use std::{convert::TryInto, fmt};
-

-
use serde::{Deserialize, Serialize};
-
use sha2::{
-
    digest::{generic_array::GenericArray, OutputSizeUser},
-
    Digest as _, Sha256,
-
};
-
use thiserror::Error;
-

-
#[derive(Debug, Clone, PartialEq, Eq, Error)]
-
pub enum DecodeError {
-
    #[error("invalid digest length {0}")]
-
    InvalidLength(usize),
-
}
-

-
/// A SHA-256 hash.
-
#[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
-
pub struct Digest([u8; 32]);
-

-
impl Digest {
-
    pub fn new(bytes: impl AsRef<[u8]>) -> Self {
-
        Self::from(Sha256::digest(bytes))
-
    }
-
}
-

-
impl AsRef<[u8; 32]> for Digest {
-
    fn as_ref(&self) -> &[u8; 32] {
-
        &self.0
-
    }
-
}
-

-
impl fmt::Debug for Digest {
-
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-
        write!(f, "Hash({})", self)
-
    }
-
}
-

-
impl fmt::Display for Digest {
-
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-
        for byte in &self.0 {
-
            write!(f, "{:02x}", byte)?;
-
        }
-
        Ok(())
-
    }
-
}
-

-
impl From<[u8; 32]> for Digest {
-
    fn from(bytes: [u8; 32]) -> Self {
-
        Self(bytes)
-
    }
-
}
-

-
impl TryFrom<&[u8]> for Digest {
-
    type Error = DecodeError;
-

-
    fn try_from(bytes: &[u8]) -> Result<Self, DecodeError> {
-
        let bytes: [u8; 32] = bytes
-
            .try_into()
-
            .map_err(|_| DecodeError::InvalidLength(bytes.len()))?;
-

-
        Ok(bytes.into())
-
    }
-
}
-

-
impl From<GenericArray<u8, <Sha256 as OutputSizeUser>::OutputSize>> for Digest {
-
    fn from(array: GenericArray<u8, <Sha256 as OutputSizeUser>::OutputSize>) -> Self {
-
        Self(array.into())
-
    }
-
}
modified radicle/src/lib.rs
@@ -8,7 +8,6 @@ pub extern crate radicle_crypto as crypto;
pub mod cob;
pub mod collections;
pub mod git;
-
pub mod hash;
pub mod identity;
pub mod node;
pub mod profile;
modified radicle/src/test/arbitrary.rs
@@ -10,7 +10,6 @@ use quickcheck::Arbitrary;

use crate::collections::HashMap;
use crate::git;
-
use crate::hash;
use crate::identity::{project::Delegate, project::Doc, Did, Id};
use crate::storage;
use crate::storage::refs::{Refs, SignedRefs};
@@ -198,10 +197,3 @@ impl Arbitrary for Id {
        Id::from(oid)
    }
}
-

-
impl Arbitrary for hash::Digest {
-
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
-
        let bytes: Vec<u8> = Arbitrary::arbitrary(g);
-
        hash::Digest::new(&bytes)
-
    }
-
}