Skip to content

Commit

Permalink
feat(objectarium): implement MD5 hash algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 10, 2023
1 parent fdd1a01 commit be4bb16
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 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::MD5 => crypto::HashAlgorithm::MD5,
state::HashAlgorithm::Sha224 => crypto::HashAlgorithm::Sha224,
state::HashAlgorithm::Sha256 => crypto::HashAlgorithm::Sha256,
state::HashAlgorithm::Sha384 => crypto::HashAlgorithm::Sha384,
Expand Down
9 changes: 9 additions & 0 deletions contracts/okp4-objectarium/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use md5;
use sha2;
use sha2::Digest;

/// HashAlgorithm is the type of the hash algorithm.
pub enum HashAlgorithm {
/// Represents the MD5 algorithm.
MD5,
/// Represents the SHA-224 algorithm.
Sha224,
/// Represents the SHA-256 algorithm.
Expand All @@ -17,6 +20,7 @@ impl HashAlgorithm {
/// hash returns the hash of the given data using the given algorithm.
pub fn hash_fn(&self) -> HashFn {
match self {
HashAlgorithm::MD5 => md5_hash,
HashAlgorithm::Sha224 => sha224_hash,
HashAlgorithm::Sha256 => sha256_hash,
HashAlgorithm::Sha384 => sha384_hash,
Expand All @@ -33,6 +37,11 @@ pub fn hash(algorithm: &HashAlgorithm, data: &Vec<u8>) -> String {
algorithm.hash_fn()(data)
}

/// md5_hash returns the MD5 hash of the given data.
pub fn md5_hash(data: &Vec<u8>) -> String {
base16ct::lower::encode_string(&md5::Md5::digest(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))
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 @@ -133,6 +133,8 @@ pub struct BucketResponse {
#[cw_serde]
#[derive(Copy)]
pub enum HashAlgorithm {
/// Represents the MD5 algorithm.
MD5,
/// Represents the SHA-224 algorithm.
Sha224,
/// Represents the SHA-256 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 MD5 algorithm.
MD5,
/// Represents the SHA-224 algorithm.
Sha224,
/// Represents the SHA-256 algorithm.
Expand All @@ -83,6 +85,7 @@ impl Default for HashAlgorithm {
impl From<msg::HashAlgorithm> for HashAlgorithm {
fn from(algorithm: msg::HashAlgorithm) -> Self {
match algorithm {
msg::HashAlgorithm::MD5 => HashAlgorithm::MD5,
msg::HashAlgorithm::Sha224 => HashAlgorithm::Sha224,
msg::HashAlgorithm::Sha256 => HashAlgorithm::Sha256,
msg::HashAlgorithm::Sha384 => HashAlgorithm::Sha384,
Expand All @@ -94,6 +97,7 @@ impl From<msg::HashAlgorithm> for HashAlgorithm {
impl From<HashAlgorithm> for msg::HashAlgorithm {
fn from(algorithm: HashAlgorithm) -> Self {
match algorithm {
HashAlgorithm::MD5 => msg::HashAlgorithm::MD5,
HashAlgorithm::Sha224 => msg::HashAlgorithm::Sha224,
HashAlgorithm::Sha256 => msg::HashAlgorithm::Sha256,
HashAlgorithm::Sha384 => msg::HashAlgorithm::Sha384,
Expand Down

0 comments on commit be4bb16

Please sign in to comment.