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

Commit 561efa4

Browse files
committed
Merge #172: Add "commonly used operations" examples
c977624 Add "commonly used operations" examples (Dawid Ciężarkiewicz) Pull request description: For a fresh user like me it was was relatively hard to figure out even the basic usage. I hope these simple examples will save some time for other newcomers and help describe what this library is about. ACKs for top commit: tcharding: ACK c977624 apoelstra: ACK c977624 Tree-SHA512: 2189bdf9b974c25f2ba56735bbb979317c3f5787fa5decccbcd2fbf846d98a5c6a7f66940e4e5d15b512e84536a5b1358f03e179b11ad5060cb608be471ffe43
2 parents 19f73ee + c977624 commit 561efa4

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)