diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index 4ad076090..34f43a601 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -51,9 +51,10 @@ pub struct HashEngine { length: usize, } -impl Default for HashEngine { - fn default() -> Self { - HashEngine { +impl HashEngine { + /// Creates a new SHA256 hash engine. + pub const fn new() -> Self { + Self { h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0], length: 0, buffer: [0; BLOCK_SIZE], @@ -61,6 +62,10 @@ impl Default for HashEngine { } } +impl Default for HashEngine { + fn default() -> Self { Self::new() } +} + impl crate::HashEngine for HashEngine { type MidState = [u8; 20]; diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index 221666301..0ab25ff78 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -43,9 +43,10 @@ pub struct HashEngine { length: usize, } -impl Default for HashEngine { - fn default() -> Self { - HashEngine { +impl HashEngine { + /// Creates a new SHA1 hash engine. + pub const fn new() -> Self { + Self { h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0], length: 0, buffer: [0; BLOCK_SIZE], @@ -53,6 +54,10 @@ impl Default for HashEngine { } } +impl Default for HashEngine { + fn default() -> Self { Self::new() } +} + impl crate::HashEngine for HashEngine { type MidState = [u8; 20]; diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 6900ae583..0c673e341 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -59,9 +59,10 @@ pub struct HashEngine { length: usize, } -impl Default for HashEngine { - fn default() -> Self { - HashEngine { +impl HashEngine { + /// Creates a new SHA256 hash engine. + pub const fn new() -> Self { + Self { h: [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, @@ -72,6 +73,10 @@ impl Default for HashEngine { } } +impl Default for HashEngine { + fn default() -> Self { Self::new() } +} + impl crate::HashEngine for HashEngine { type MidState = Midstate; diff --git a/hashes/src/sha384.rs b/hashes/src/sha384.rs index ed675ce78..53db4e43f 100644 --- a/hashes/src/sha384.rs +++ b/hashes/src/sha384.rs @@ -23,11 +23,13 @@ fn from_engine(e: HashEngine) -> Hash { #[derive(Clone)] pub struct HashEngine(sha512::HashEngine); +impl HashEngine { + /// Creates a new SHA384 hash engine. + pub const fn new() -> Self { Self(sha512::HashEngine::sha384()) } +} + impl Default for HashEngine { - #[rustfmt::skip] - fn default() -> Self { - HashEngine(sha512::HashEngine::sha384()) - } + fn default() -> Self { Self::new() } } impl crate::HashEngine for HashEngine { diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index fb3ab5433..77997a0e3 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -52,10 +52,11 @@ pub struct HashEngine { buffer: [u8; BLOCK_SIZE], } -impl Default for HashEngine { +impl HashEngine { + /// Creates a new SHA512 hash engine. #[rustfmt::skip] - fn default() -> Self { - HashEngine { + pub const fn new() -> Self { + Self { h: [ 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, @@ -66,10 +67,14 @@ impl Default for HashEngine { } } +impl Default for HashEngine { + fn default() -> Self { Self::new() } +} + impl HashEngine { - /// Constructs a hash engine suitable for use inside the default `sha512_256::HashEngine`. + /// Constructs a hash engine suitable for use constructing a `sha512_256::HashEngine`. #[rustfmt::skip] - pub(crate) fn sha512_256() -> Self { + pub(crate) const fn sha512_256() -> Self { HashEngine { h: [ 0x22312194fc2bf72c, 0x9f555fa3c84c64c2, 0x2393b86b6f53b151, 0x963877195940eabd, @@ -80,9 +85,9 @@ impl HashEngine { } } - /// Constructs a hash engine suitable for use inside the default `sha384::HashEngine`. + /// Constructs a hash engine suitable for constructing `sha384::HashEngine`. #[rustfmt::skip] - pub(crate) fn sha384() -> Self { + pub(crate) const fn sha384() -> Self { HashEngine { h: [ 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939, diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index 73d871621..413152196 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -33,11 +33,13 @@ fn from_engine(e: HashEngine) -> Hash { #[derive(Clone)] pub struct HashEngine(sha512::HashEngine); +impl HashEngine { + /// Creates a new SHA512/256 hash engine. + pub const fn new() -> Self { Self(sha512::HashEngine::sha512_256()) } +} + impl Default for HashEngine { - #[rustfmt::skip] - fn default() -> Self { - HashEngine(sha512::HashEngine::sha512_256()) - } + fn default() -> Self { Self::new() } } impl crate::HashEngine for HashEngine {