Skip to content

Commit

Permalink
Merge pull request #21 from baoyachi/support_char_type
Browse files Browse the repository at this point in the history
support char type
  • Loading branch information
baoyachi authored Jan 3, 2024
2 parents dfa209c + 5671f6c commit 70471a8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@
use sha256::digest;

fn main() {
let input = String::from("hello");
let val = digest(input);
assert_eq!(val,"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");

//sha256 digest &str
let input = "hello";
let val = digest(input);
assert_eq!(val, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");

let input = "hello".to_string();
assert_eq!(val,"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");

//sha256 digest &mut &str
let mut input = "hello";
let val = digest(&mut input);
assert_eq!(val,"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");

//sha256 digest char
let mut input = "π";
let val = digest(input);
assert_eq!(val, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
assert_eq!(val,"2617fcb92baa83a96341de050f07a3186657090881eae6b833f66a035600f35a");


let input = b"hello";
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
//! let val = digest(input);
//! assert_eq!(val,"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
//!
//! //sha256 digest &mut &str
//! let mut input = "hello";
//! let val = digest(&mut input);
//! assert_eq!(val,"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
//!
//! //sha256 digest char
//! let mut input = "π";
//! let val = digest(input);
//! assert_eq!(val,"2617fcb92baa83a96341de050f07a3186657090881eae6b833f66a035600f35a");
//!
//! //sha256 digest bytes
//! let input = b"hello";
//! let val = digest(input);
Expand Down Expand Up @@ -156,6 +166,18 @@ impl Sha256Digest for &str {
}
}

impl Sha256Digest for char {
fn digest(self) -> String {
__digest__(self.encode_utf8(&mut [0; 4]).as_bytes())
}
}

impl Sha256Digest for &mut &str {
fn digest(self) -> String {
__digest__(self.as_bytes())
}
}

impl Sha256Digest for &String {
fn digest(self) -> String {
__digest__(self.as_bytes())
Expand Down

0 comments on commit 70471a8

Please sign in to comment.