|
@@ -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)
|
|
|
}
|
|
|
}
|