From 6aeefb7fbb7dd51fa55239973b59b78d4698c594 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 29 Sep 2020 00:08:56 +0100 Subject: [PATCH] sha256 bytes --- Cargo.toml | 2 +- src/password.rs | 2 +- src/sha256.rs | 30 +++++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f5f2abe..bba728a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptohelpers" -version = "1.1.2" +version = "1.2.0" license= "MIT" description = "Collection of helpers and simplifying functions for cryptography things" authors = ["Avril "] diff --git a/src/password.rs b/src/password.rs index 3fd4023..cf3fb9a 100644 --- a/src/password.rs +++ b/src/password.rs @@ -26,7 +26,7 @@ pub struct Password { /// Represents a salt to be used for password operations #[derive(Clone, PartialEq, Eq, Hash, Debug)] -#[repr(C, packed)] +#[repr(transparent)] pub struct Salt([u8; SALTSIZE]); impl Default for Salt diff --git a/src/sha256.rs b/src/sha256.rs index 91111b9..3ed612c 100644 --- a/src/sha256.rs +++ b/src/sha256.rs @@ -15,7 +15,7 @@ use tokio::{ prelude::*, }; -const SIZE: usize = consts::SHA256_SIZE; +pub const SIZE: usize = consts::SHA256_SIZE; /// Represents a SHA256 hash #[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Debug)] @@ -34,6 +34,18 @@ impl Sha256Hash Self { hash: [0u8; SIZE] } } + /// Create a SHA256 instance from these bytes + #[inline] pub const fn from_bytes(hash: [u8; SIZE]) -> Self + { + Self { hash } + } + + /// Consume this instance into bytes + #[inline] pub const fn into_bytes(self) -> [u8; SIZE] + { + self.hash + } + /// Reads the rest of the stream, and computes SHA256 hash into the current instance. Returning the number of bytes read. #[cfg(feature="async")] pub async fn compute_into(&mut self, from: &mut T) -> io::Result @@ -155,3 +167,19 @@ impl AsMut<[u8]> for Sha256Hash &mut self.hash[..] } } + +impl From<[u8; SIZE]> for Sha256Hash +{ + #[inline] fn from(hash: [u8; SIZE]) -> Self + { + Self { hash } + } +} + +impl From for [u8; SIZE] +{ + #[inline] fn from(from: Sha256Hash) -> Self + { + from.hash + } +}