Skip to content

Commit

Permalink
feat(objectarium): implement SHA-224 hash algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 9, 2023
1 parent 36e5e05 commit 3f8b938
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions contracts/okp4-objectarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ pub mod query {
impl From<state::HashAlgorithm> for crypto::HashAlgorithm {
fn from(algorithm: state::HashAlgorithm) -> Self {
match algorithm {
state::HashAlgorithm::Sha224 => crypto::HashAlgorithm::Sha224,
state::HashAlgorithm::Sha256 => crypto::HashAlgorithm::Sha256,
state::HashAlgorithm::Sha384 => crypto::HashAlgorithm::Sha384,
state::HashAlgorithm::Sha512 => crypto::HashAlgorithm::Sha512,
Expand Down
36 changes: 25 additions & 11 deletions contracts/okp4-objectarium/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use sha2::{Digest, Sha256, Sha512, Sha384};
use sha2;
use sha2::Digest;

/// HashAlgorithm is the type of the hash algorithm.
pub enum HashAlgorithm {
/// Represents the SHA-224 algorithm.
Sha224,
/// Represents the SHA-256 algorithm.
Sha256,
/// Represents the SHA-384 algorithm.
Expand All @@ -10,31 +13,42 @@ pub enum HashAlgorithm {
Sha512,
}

impl HashAlgorithm {
/// hash returns the hash of the given data using the given algorithm.
pub fn hash_fn(&self) -> HashFn {
match self {
HashAlgorithm::Sha224 => sha224_hash,
HashAlgorithm::Sha256 => sha256_hash,
HashAlgorithm::Sha384 => sha384_hash,
HashAlgorithm::Sha512 => sha512_hash,
}
}
}

/// HashFn is the type of the function used to hash data.
pub type HashFn = fn(&Vec<u8>) -> String;

/// hash returns the hash of the given data using the given algorithm.
/// If the algorithm is not supported, an error is returned.
pub fn hash(algorithm: &HashAlgorithm, data: &Vec<u8>) -> String {
let hash_fn = match algorithm {
HashAlgorithm::Sha256 => sha256_hash,
HashAlgorithm::Sha384 => sha384_hash,
HashAlgorithm::Sha512 => sha512_hash,
};
hash_fn(data)
algorithm.hash_fn()(data)
}

/// sha224_hash returns the SHA-224 hash of the given data.
fn sha224_hash(data: &Vec<u8>) -> String {
base16ct::lower::encode_string(&sha2::Sha224::digest(data))
}

/// sha256_hash returns the SHA-256 hash of the given data.
fn sha256_hash(data: &Vec<u8>) -> String {
base16ct::lower::encode_string(&Sha256::digest(data))
base16ct::lower::encode_string(&sha2::Sha256::digest(data))
}

/// sha384_hash returns the SHA-384 hash of the given data.
fn sha384_hash(data: &Vec<u8>) -> String {
base16ct::lower::encode_string(&Sha384::digest(data))
base16ct::lower::encode_string(&sha2::Sha384::digest(data))
}

/// sha512_hash returns the SHA-512 hash of the given data.
fn sha512_hash(data: &Vec<u8>) -> String {
base16ct::lower::encode_string(&Sha512::digest(data))
base16ct::lower::encode_string(&sha2::Sha512::digest(data))
}
2 changes: 2 additions & 0 deletions contracts/okp4-objectarium/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct BucketResponse {
#[cw_serde]
#[derive(Copy)]
pub enum HashAlgorithm {
/// Represents the SHA-224 algorithm.
Sha224,
/// Represents the SHA-256 algorithm.
Sha256,
/// Represents the SHA-384 algorithm.
Expand Down
4 changes: 4 additions & 0 deletions contracts/okp4-objectarium/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ impl Bucket {
/// supported for hashing the content of objects.
#[derive(Serialize, Copy, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub enum HashAlgorithm {
/// Represents the SHA-224 algorithm.
Sha224,
/// Represents the SHA-256 algorithm.
Sha256,
/// Represents the SHA-384 algorithm.
Expand All @@ -81,6 +83,7 @@ impl Default for HashAlgorithm {
impl From<msg::HashAlgorithm> for HashAlgorithm {
fn from(algorithm: msg::HashAlgorithm) -> Self {
match algorithm {
msg::HashAlgorithm::Sha224 => HashAlgorithm::Sha224,
msg::HashAlgorithm::Sha256 => HashAlgorithm::Sha256,
msg::HashAlgorithm::Sha384 => HashAlgorithm::Sha384,
msg::HashAlgorithm::Sha512 => HashAlgorithm::Sha512,
Expand All @@ -91,6 +94,7 @@ impl From<msg::HashAlgorithm> for HashAlgorithm {
impl From<HashAlgorithm> for msg::HashAlgorithm {
fn from(algorithm: HashAlgorithm) -> Self {
match algorithm {
HashAlgorithm::Sha224 => msg::HashAlgorithm::Sha224,
HashAlgorithm::Sha256 => msg::HashAlgorithm::Sha256,
HashAlgorithm::Sha384 => msg::HashAlgorithm::Sha384,
HashAlgorithm::Sha512 => msg::HashAlgorithm::Sha512,
Expand Down

0 comments on commit 3f8b938

Please sign in to comment.