|
19 | 19 | //! thing, it exposes hexadecimal serialization and deserialization, since these
|
20 | 20 | //! are needed to display hashes anway.
|
21 | 21 | //!
|
| 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 | +//! ``` |
22 | 79 |
|
23 | 80 | // Coding conventions
|
24 | 81 | #![deny(non_upper_case_globals)]
|
|
0 commit comments