Skip to content

Commit

Permalink
improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Mar 4, 2024
1 parent 43f2c51 commit cfc746d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 7 additions & 5 deletions sha1-checked/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ This implementation will be slower to use than the pure SHA-1 implementation, as

```rust
use hex_literal::hex;
use sha1_checked::{Sha1, Digest};
use sha1_checked::Sha1;

let result = Sha1::digest(b"hello world");
assert_eq!(result, hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
let result = Sha1::try_digest(b"hello world");
assert_eq!(result.hash().as_ref(), hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
assert!(!result.has_collision());
```

### Incremental API
Expand All @@ -40,9 +41,10 @@ use sha1_checked::{Sha1, Digest};

let mut hasher = Sha1::new();
hasher.update(b"hello world");
let hash = hasher.finalize();
let result = hasher.try_finalize();

assert_eq!(hash, hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
assert_eq!(result.hash().as_ref(), hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
assert!(!result.has_collision());
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
18 changes: 18 additions & 0 deletions sha1-checked/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ impl Sha1 {
Builder::default()
}

/// Oneshot hashing, reporting the collision state.
///
/// # Examples
///
/// ```
/// use hex_literal::hex;
/// use sha1_checked::Sha1;
///
/// let result = Sha1::try_digest(b"hello world");
/// assert_eq!(result.hash().as_ref(), hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
/// assert!(!result.has_collision());
/// ```
pub fn try_digest(data: impl AsRef<[u8]>) -> CollisionResult {
let mut hasher = Self::default();
Digest::update(&mut hasher, data);
hasher.try_finalize()
}

/// Try finalization, reporting the collision state.
pub fn try_finalize(mut self) -> CollisionResult {
let mut out = Output::<Self>::default();
Expand Down

0 comments on commit cfc746d

Please sign in to comment.