forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2870b9
commit 40c4df0
Showing
3 changed files
with
30 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! Hashing service | ||
use sha1::{Digest, Sha1}; | ||
|
||
// Calculate the sha1 hash of a string | ||
#[must_use] | ||
pub fn sha1(data: &str) -> String { | ||
// Create a Sha1 object | ||
let mut hasher = Sha1::new(); | ||
|
||
// Write input message | ||
hasher.update(data.as_bytes()); | ||
|
||
// Read hash digest and consume hasher | ||
let result = hasher.finalize(); | ||
|
||
// Convert the hash (a byte array) to a string of hex characters | ||
hex::encode(result) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::services::hasher::sha1; | ||
|
||
#[test] | ||
fn it_should_hash_an_string() { | ||
assert_eq!(sha1("hello world"), "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); | ||
} | ||
} |
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