2 releases
0.14.1 | Sep 10, 2024 |
---|---|
0.14.0 | Sep 10, 2024 |
#700 in Algorithms
46 downloads per month
Used in 5 crates
(3 directly)
150KB
3K
SLoC
Rust hashes library.
This is a simple, no-dependency library which implements the hash functions needed by Bitcoin. These are SHA256, SHA256d, and RIPEMD160. As an ancillary thing, it exposes hexadecimal serialization and deserialization, since these are needed to display hashes anway.
Commonly used operations
Hashing a single byte slice or a string:
use satsnet_hashes::sha256;
use satsnet_hashes::Hash;
let bytes = [0u8; 5];
let hash_of_bytes = sha256::Hash::hash(&bytes);
let hash_of_string = sha256::Hash::hash("some string".as_bytes());
Hashing content from a reader:
use satsnet_hashes::sha256;
use satsnet_hashes::Hash;
#[cfg(std)]
let mut reader: &[u8] = b"hello"; // in real code, this could be a `File` or `TcpStream`
let mut engine = sha256::HashEngine::default();
std::io::copy(&mut reader, &mut engine)?;
let hash = sha256::Hash::from_engine(engine);
#[cfg(not(std))]
Hashing content by std::io::Write
on HashEngine:
use satsnet_hashes::sha256;
use satsnet_hashes::Hash;
use std::io::Write;
#[cfg(std)]
let mut part1: &[u8] = b"hello";
let mut part2: &[u8] = b" ";
let mut part3: &[u8] = b"world";
let mut engine = sha256::HashEngine::default();
engine.write_all(part1)?;
engine.write_all(part2)?;
engine.write_all(part3)?;
let hash = sha256::Hash::from_engine(engine);
#[cfg(not(std))]
Dependencies
~145–480KB