Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support char type #21

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading