Browse Source

Renamed the enum discriminants from *Type to *Kind.

Matthew Carr 2 years ago
parent
commit
8c379b2990
3 changed files with 29 additions and 29 deletions
  1. 23 23
      crates/node/src/crypto.rs
  2. 5 5
      crates/node/src/main.rs
  3. 1 1
      crates/node/src/test_helpers.rs

+ 23 - 23
crates/node/src/crypto.rs

@@ -70,16 +70,16 @@ pub type Result<T> = std::result::Result<T, Error>;
 /// A cryptographic hash.
 #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable, Clone, EnumDiscriminants)]
 #[strum_discriminants(derive(EnumString, Display))]
-#[strum_discriminants(name(HashType))]
+#[strum_discriminants(name(HashKind))]
 pub enum Hash {
     Sha2_256([u8; 32]),
     #[serde(with = "BigArray")]
     Sha2_512([u8; 64]),
 }
 
-impl Default for HashType {
-    fn default() -> HashType {
-        HashType::Sha2_256
+impl Default for HashKind {
+    fn default() -> HashKind {
+        HashKind::Sha2_256
     }
 }
 
@@ -88,16 +88,16 @@ impl Hash {
     /// representation.
     const HASH_SEP: char = ':';
 
-    fn kind(&self) -> HashType {
-        HashType::from(self)
+    fn kind(&self) -> HashKind {
+        HashKind::from(self)
     }
 }
 
-impl From<HashType> for Hash {
-    fn from(discriminant: HashType) -> Hash {
+impl From<HashKind> for Hash {
+    fn from(discriminant: HashKind) -> Hash {
         match discriminant {
-            HashType::Sha2_512 => Hash::Sha2_512([0u8; 64]),
-            HashType::Sha2_256 => Hash::Sha2_256([0u8; 32])
+            HashKind::Sha2_512 => Hash::Sha2_512([0u8; 64]),
+            HashKind::Sha2_256 => Hash::Sha2_256([0u8; 32])
         }
     }
 }
@@ -129,7 +129,7 @@ impl TryFrom<&str> for Hash {
         };
         let second = split.pop().ok_or(Error::InvalidFormat)?;
         let first = split.pop().ok_or(Error::InvalidFormat)?;
-        let mut hash = Hash::from(HashType::from_str(first)
+        let mut hash = Hash::from(HashKind::from_str(first)
             .map_err(|_| Error::InvalidFormat)?);
         base64_url::decode_to_slice(second, hash.as_mut())
             .map_err(|_| Error::InvalidFormat)?;
@@ -139,7 +139,7 @@ impl TryFrom<&str> for Hash {
 
 impl Display for Hash {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let hash_type: HashType = self.into();
+        let hash_type: HashKind = self.into();
         let hash_data = base64_url::encode(self.as_ref());
         write!(f, "{}{}{}", hash_type, Hash::HASH_SEP, hash_data)
     }
@@ -212,7 +212,7 @@ impl From<RsaPadding> for OpensslPadding {
 }
 
 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone, EnumDiscriminants)]
-#[strum_discriminants(name(SymKeyType))]
+#[strum_discriminants(name(SymKeyKind))]
 pub enum SymKey {
     /// A key for the AES 256 cipher in Cipher Block Chaining mode. Note that this includes the
     /// initialization vector, so that a value of this variant contains all the information needed
@@ -245,12 +245,12 @@ fn rand_array<const LEN: usize>() -> Result<[u8; LEN]> {
 }
 
 impl SymKey {
-    pub fn generate(kind: SymKeyType) -> Result<SymKey> {
+    pub fn generate(kind: SymKeyKind) -> Result<SymKey> {
         match kind {
-            SymKeyType::Aes256Cbc => {
+            SymKeyKind::Aes256Cbc => {
                 Ok(SymKey::Aes256Cbc { key: rand_array()?, iv: rand_array()? })
             },
-            SymKeyType::Aes256Ctr => {
+            SymKeyKind::Aes256Ctr => {
                 Ok(SymKey::Aes256Ctr { key: rand_array()?, iv: rand_array()? })
             },
         }
@@ -264,7 +264,7 @@ pub struct Public;
 pub struct Private;
 
 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone, EnumDiscriminants)]
-#[strum_discriminants(name(AsymKeyType))]
+#[strum_discriminants(name(AsymKeyKind))]
 pub enum AsymKey<T> {
     Rsa {
         der: Vec<u8>,
@@ -282,18 +282,18 @@ impl<T> AsymKey<T> {
 }
 
 impl<T> Owned for AsymKey<T> {
-    fn owner_of_kind(&self, kind: HashType) -> Principal {
+    fn owner_of_kind(&self, kind: HashKind) -> Principal {
         let slice = match self {
             AsymKey::Rsa { der, .. } => der.as_slice(),
         };
         match kind {
-            HashType::Sha2_256 => {
+            HashKind::Sha2_256 => {
                 let mut buf = [0; 32];
                 let bytes = hash(MessageDigest::sha256(), slice).unwrap();
                 buf.copy_from_slice(&*bytes);
                 Principal(Hash::Sha2_256(buf))
             },
-            HashType::Sha2_512 => {
+            HashKind::Sha2_512 => {
                 let mut buf = [0; 64];
                 let bytes = hash(MessageDigest::sha512(), slice).unwrap();
                 buf.copy_from_slice(&*bytes);
@@ -309,9 +309,9 @@ pub struct KeyPair {
 }
 
 impl KeyPair {
-    pub fn generate(kind: AsymKeyType) -> Result<KeyPair> {
+    pub fn generate(kind: AsymKeyKind) -> Result<KeyPair> {
         match kind {
-            AsymKeyType::Rsa => {
+            AsymKeyKind::Rsa => {
                 let key = PKey::from_rsa(Rsa::generate(4096)?)?;
                 let public = key.public_key_to_der().map_err(Error::from)?;
                 let private = key.private_key_to_der().map_err(Error::from)?;
@@ -333,7 +333,7 @@ impl KeyPair {
 }
 
 impl Owned for KeyPair {
-    fn owner_of_kind(&self, kind: HashType) -> Principal {
+    fn owner_of_kind(&self, kind: HashKind) -> Principal {
         self.public.owner_of_kind(kind)
     }
 }

+ 5 - 5
crates/node/src/main.rs

@@ -20,7 +20,7 @@ mod test_helpers;
 mod serde_tests;
 
 mod crypto;
-use crypto::{Hash, HashType, Signature, SymKey, AsymKey, Public, Cryptotext};
+use crypto::{Hash, HashKind, Signature, SymKey, AsymKey, Public, Cryptotext};
 
 /// A Block tagged with its version number.
 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
@@ -138,19 +138,19 @@ impl FragmentRecord {
 pub struct Principal(Hash);
 
 impl Principal {
-    fn kind(&self) -> HashType {
-        HashType::from(&self.0)
+    fn kind(&self) -> HashKind {
+        HashKind::from(&self.0)
     }
 }
 
 /// Trait for types which are owned by a `Principal`.
 pub trait Owned {
     /// Returns the `Principal` that owns `self`, using the given hash algorithm.
-    fn owner_of_kind(&self, kind: HashType) -> Principal;
+    fn owner_of_kind(&self, kind: HashKind) -> Principal;
 
     /// Returns the `Principal` that owns `self`, using the default hash algorithm.
     fn owner(&self) -> Principal {
-        self.owner_of_kind(HashType::default())
+        self.owner_of_kind(HashKind::default())
     }
 }
 

+ 1 - 1
crates/node/src/test_helpers.rs

@@ -579,7 +579,7 @@ impl<'a> NamedSlice<'a> {
 }
 
 fn write_rsa_keys_to_file(path: &str) -> Result<()> {
-    let key = KeyPair::generate(AsymKeyType::Rsa).map_err(|e| Error::Message(e.to_string()))?;
+    let key = KeyPair::generate(AsymKeyKind::Rsa).map_err(|e| Error::Message(e.to_string()))?;
     let slices = [
         NamedSlice::new("PUBLIC", key.public.as_slice()),
         NamedSlice::new("PRIVATE", key.private.as_slice())