Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit c977624

Browse files
committed
Add "commonly used operations" examples
1 parent 4331244 commit c977624

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/lib.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,63 @@
1919
//! thing, it exposes hexadecimal serialization and deserialization, since these
2020
//! are needed to display hashes anway.
2121
//!
22+
//! ## Commonly used operations
23+
//!
24+
//! Hashing a single byte slice or a string:
25+
//!
26+
//! ```rust
27+
//! use bitcoin_hashes::sha256;
28+
//! use bitcoin_hashes::Hash;
29+
//!
30+
//! let bytes = [0u8; 5];
31+
//! let hash_of_bytes = sha256::Hash::hash(&bytes);
32+
//! let hash_of_string = sha256::Hash::hash("some string".as_bytes());
33+
//! ```
34+
//!
35+
//!
36+
//! Hashing content from a reader:
37+
//!
38+
//! ```rust
39+
//! use bitcoin_hashes::sha256;
40+
//! use bitcoin_hashes::Hash;
41+
//!
42+
//! #[cfg(std)]
43+
//! # fn main() -> std::io::Result<()> {
44+
//! let mut reader: &[u8] = b"hello"; // in real code, this could be a `File` or `TcpStream`
45+
//! let mut engine = sha256::HashEngine::default();
46+
//! std::io::copy(&mut reader, &mut engine)?;
47+
//! let hash = sha256::Hash::from_engine(engine);
48+
//! # Ok(())
49+
//! # }
50+
//!
51+
//! #[cfg(not(std))]
52+
//! # fn main() {}
53+
//! ```
54+
//!
55+
//!
56+
//! Hashing content by [`std::io::Write`] on HashEngine:
57+
//!
58+
//! ```rust
59+
//! use bitcoin_hashes::sha256;
60+
//! use bitcoin_hashes::Hash;
61+
//! use std::io::Write;
62+
//!
63+
//! #[cfg(std)]
64+
//! # fn main() -> std::io::Result<()> {
65+
//! let mut part1: &[u8] = b"hello";
66+
//! let mut part2: &[u8] = b" ";
67+
//! let mut part3: &[u8] = b"world";
68+
//! let mut engine = sha256::HashEngine::default();
69+
//! engine.write_all(part1)?;
70+
//! engine.write_all(part2)?;
71+
//! engine.write_all(part3)?;
72+
//! let hash = sha256::Hash::from_engine(engine);
73+
//! # Ok(())
74+
//! # }
75+
//!
76+
//! #[cfg(not(std))]
77+
//! # fn main() {}
78+
//! ```
2279
2380
// Coding conventions
2481
#![deny(non_upper_case_globals)]

0 commit comments

Comments
 (0)