From 36e5e050c072535f1fa9ee3daaa3497ab2cc0bf5 Mon Sep 17 00:00:00 2001 From: ccamel Date: Sun, 9 Apr 2023 19:36:14 +0200 Subject: [PATCH] feat(objectarium): implement SHA-384 hash algorithm --- contracts/okp4-objectarium/src/contract.rs | 1 + contracts/okp4-objectarium/src/crypto.rs | 10 +++++++++- contracts/okp4-objectarium/src/msg.rs | 2 ++ contracts/okp4-objectarium/src/state.rs | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/contracts/okp4-objectarium/src/contract.rs b/contracts/okp4-objectarium/src/contract.rs index 975f9ca3..0e2aa76f 100644 --- a/contracts/okp4-objectarium/src/contract.rs +++ b/contracts/okp4-objectarium/src/contract.rs @@ -365,6 +365,7 @@ impl From for crypto::HashAlgorithm { fn from(algorithm: state::HashAlgorithm) -> Self { match algorithm { state::HashAlgorithm::Sha256 => crypto::HashAlgorithm::Sha256, + state::HashAlgorithm::Sha384 => crypto::HashAlgorithm::Sha384, state::HashAlgorithm::Sha512 => crypto::HashAlgorithm::Sha512, } } diff --git a/contracts/okp4-objectarium/src/crypto.rs b/contracts/okp4-objectarium/src/crypto.rs index 72ea043f..6d4113e7 100644 --- a/contracts/okp4-objectarium/src/crypto.rs +++ b/contracts/okp4-objectarium/src/crypto.rs @@ -1,9 +1,11 @@ -use sha2::{Digest, Sha256, Sha512}; +use sha2::{Digest, Sha256, Sha512, Sha384}; /// HashAlgorithm is the type of the hash algorithm. pub enum HashAlgorithm { /// Represents the SHA-256 algorithm. Sha256, + /// Represents the SHA-384 algorithm. + Sha384, /// Represents the SHA-512 algorithm. Sha512, } @@ -16,6 +18,7 @@ pub type HashFn = fn(&Vec) -> String; pub fn hash(algorithm: &HashAlgorithm, data: &Vec) -> String { let hash_fn = match algorithm { HashAlgorithm::Sha256 => sha256_hash, + HashAlgorithm::Sha384 => sha384_hash, HashAlgorithm::Sha512 => sha512_hash, }; hash_fn(data) @@ -26,6 +29,11 @@ fn sha256_hash(data: &Vec) -> String { base16ct::lower::encode_string(&Sha256::digest(data)) } +/// sha384_hash returns the SHA-384 hash of the given data. +fn sha384_hash(data: &Vec) -> String { + base16ct::lower::encode_string(&Sha384::digest(data)) +} + /// sha512_hash returns the SHA-512 hash of the given data. fn sha512_hash(data: &Vec) -> String { base16ct::lower::encode_string(&Sha512::digest(data)) diff --git a/contracts/okp4-objectarium/src/msg.rs b/contracts/okp4-objectarium/src/msg.rs index 2ba5a210..f944fb3b 100644 --- a/contracts/okp4-objectarium/src/msg.rs +++ b/contracts/okp4-objectarium/src/msg.rs @@ -135,6 +135,8 @@ pub struct BucketResponse { pub enum HashAlgorithm { /// Represents the SHA-256 algorithm. Sha256, + /// Represents the SHA-384 algorithm. + Sha384, /// Represents the SHA-512 algorithm. Sha512, } diff --git a/contracts/okp4-objectarium/src/state.rs b/contracts/okp4-objectarium/src/state.rs index 0c3b1377..65f09d6d 100644 --- a/contracts/okp4-objectarium/src/state.rs +++ b/contracts/okp4-objectarium/src/state.rs @@ -66,6 +66,8 @@ impl Bucket { pub enum HashAlgorithm { /// Represents the SHA-256 algorithm. Sha256, + /// Represents the SHA-384 algorithm. + Sha384, /// Represents the SHA-512 algorithm. Sha512, } @@ -80,6 +82,7 @@ impl From for HashAlgorithm { fn from(algorithm: msg::HashAlgorithm) -> Self { match algorithm { msg::HashAlgorithm::Sha256 => HashAlgorithm::Sha256, + msg::HashAlgorithm::Sha384 => HashAlgorithm::Sha384, msg::HashAlgorithm::Sha512 => HashAlgorithm::Sha512, } } @@ -89,6 +92,7 @@ impl From for msg::HashAlgorithm { fn from(algorithm: HashAlgorithm) -> Self { match algorithm { HashAlgorithm::Sha256 => msg::HashAlgorithm::Sha256, + HashAlgorithm::Sha384 => msg::HashAlgorithm::Sha384, HashAlgorithm::Sha512 => msg::HashAlgorithm::Sha512, } }