generated from okp4/template-rust
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(objectarium): implement SHA-512 hash algorithm
- Loading branch information
Showing
4 changed files
with
93 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,32 @@ | ||
use sha2::{Digest, Sha256}; | ||
use sha2::{Digest, Sha256, Sha512}; | ||
|
||
pub fn sha256_hash(data: &Vec<u8>) -> String { | ||
/// HashAlgorithm is the type of the hash algorithm. | ||
pub enum HashAlgorithm { | ||
/// Represents the SHA-256 algorithm. | ||
Sha256, | ||
/// Represents the SHA-512 algorithm. | ||
Sha512, | ||
} | ||
|
||
/// 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::Sha512 => sha512_hash, | ||
}; | ||
hash_fn(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)) | ||
} | ||
|
||
/// 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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters