hashes: Add const hash engine constructors

Add `const` constructors to all hash engines. Call through to
`Self::new` in `default` impls on `HashEngine`.
This commit is contained in:
Tobin C. Harding 2024-07-09 13:01:13 +10:00
parent b392510ec1
commit a7422a779c
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
6 changed files with 48 additions and 24 deletions

View File

@ -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];

View File

@ -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];

View File

@ -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;

View File

@ -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 {

View File

@ -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,

View File

@ -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 {