hashes: Derive traits on the sha512::Hash type

For all the other hash types we use derive, by way of the `hash_type!`
macro, unless I'm missing something we can do the same for
`sha512::Hash`.
This commit is contained in:
Tobin C. Harding 2023-04-09 13:36:38 +10:00
parent e83a2d3422
commit 088fa24be8
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 2 additions and 33 deletions

View File

@ -23,7 +23,7 @@
use core::convert::TryInto; use core::convert::TryInto;
use core::ops::Index; use core::ops::Index;
use core::slice::SliceIndex; use core::slice::SliceIndex;
use core::{cmp, hash, str}; use core::{cmp, str};
use crate::{Error, HashEngine as _}; use crate::{Error, HashEngine as _};
@ -80,6 +80,7 @@ impl crate::HashEngine for HashEngine {
} }
/// Output of the SHA512 hash function. /// Output of the SHA512 hash function.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[repr(transparent)] #[repr(transparent)]
pub struct Hash( pub struct Hash(
@ -96,38 +97,6 @@ impl Hash {
fn internal_engine() -> HashEngine { Default::default() } fn internal_engine() -> HashEngine { Default::default() }
} }
impl Copy for Hash {}
impl Clone for Hash {
fn clone(&self) -> Hash {
let mut ret = [0; 64];
ret.copy_from_slice(&self.0);
Hash(ret)
}
}
impl PartialEq for Hash {
fn eq(&self, other: &Hash) -> bool { self.0[..] == other.0[..] }
}
impl Eq for Hash {}
impl Default for Hash {
fn default() -> Hash { Hash([0; 64]) }
}
impl PartialOrd for Hash {
fn partial_cmp(&self, other: &Hash) -> Option<cmp::Ordering> { self.0.partial_cmp(&other.0) }
}
impl Ord for Hash {
fn cmp(&self, other: &Hash) -> cmp::Ordering { self.0.cmp(&other.0) }
}
impl hash::Hash for Hash {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.0.hash(state) }
}
#[cfg(not(fuzzing))] #[cfg(not(fuzzing))]
pub(crate) fn from_engine(mut e: HashEngine) -> Hash { pub(crate) fn from_engine(mut e: HashEngine) -> Hash {
// pad buffer with a single 1-bit then all 0s, until there are exactly 16 bytes remaining // pad buffer with a single 1-bit then all 0s, until there are exactly 16 bytes remaining