Skip to content

Commit

Permalink
feat(objectarium): implement SHA-384 hash algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 9, 2023
1 parent 550d87f commit 36e5e05
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions contracts/okp4-objectarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ impl From<state::HashAlgorithm> 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,
}
}
Expand Down
10 changes: 9 additions & 1 deletion contracts/okp4-objectarium/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand All @@ -16,6 +18,7 @@ pub type HashFn = fn(&Vec<u8>) -> String;
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)
Expand All @@ -26,6 +29,11 @@ fn sha256_hash(data: &Vec<u8>) -> String {
base16ct::lower::encode_string(&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))
}

/// 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))
Expand Down
2 changes: 2 additions & 0 deletions contracts/okp4-objectarium/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
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 @@ -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,
}
Expand All @@ -80,6 +82,7 @@ impl From<msg::HashAlgorithm> 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,
}
}
Expand All @@ -89,6 +92,7 @@ impl From<HashAlgorithm> 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,
}
}
Expand Down

0 comments on commit 36e5e05

Please sign in to comment.