Crate bitcoin_hashes

Source
Expand description

Rust hashes library.

This library implements the hash functions needed by Bitcoin. As an ancillary thing, it exposes hexadecimal serialization and deserialization, since these are needed to display hashes.

§Examples

Hashing a single byte slice or a string:

use bitcoin_hashes::Sha256;

let bytes = [0u8; 5];
let _hash_of_bytes = Sha256::hash(&bytes);
let _hash_of_string = Sha256::hash("some string".as_bytes());

Hashing content from a reader:

use bitcoin_hashes::Sha256;

let mut reader: &[u8] = b"hello"; // In real code, this could be a `File` or `TcpStream`.
let mut engine = Sha256::engine();
std::io::copy(&mut reader, &mut engine).expect("engine writes don't error");
let _hash = Sha256::from_engine(engine);

Hashing content using std::io::Write on a HashEngine:

use std::io::Write as _;
use bitcoin_hashes::Sha256;

let part1: &[u8] = b"hello";
let part2: &[u8] = b" ";
let part3: &[u8] = b"world";
let mut engine = Sha256::engine();
engine.write_all(part1).expect("engine writes don't error");
engine.write_all(part2).unwrap();
engine.write_all(part3).unwrap();
let _hash = Sha256::from_engine(engine);

Re-exports§

pub extern crate hex;
pub extern crate serde;

Modules§

cmp
Useful comparison functions.
error
Error code for the hashes crate.
hash160
HASH160 (SHA256 then RIPEMD160) implementation.
hkdf
HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
hmac
Hash-based Message Authentication Code (HMAC).
macros
Public macros.
ripemd160
RIPEMD160 implementation.
serde_macrosDeprecated
Macros for serde trait implementations, and supporting code.
sha1
SHA1 implementation.
sha256
SHA256 implementation.
sha384
SHA384 implementation.
sha512
SHA512 implementation.
sha256d
SHA256d implementation (double SHA256).
sha256t
SHA256t implementation (tagged SHA256).
sha512_256
SHA512_256 implementation.
siphash24
SipHash 2-4 implementation.

Macros§

hash_newtype
Creates a new newtype around a Hash type.
impl_debug_only_for_newtype
Implements fmt::Debug using hex for a new type crated with crate::hash_newtype macro.
impl_hex_for_newtypehex
Implements string functions using hex for a new type crated with crate::hash_newtype macro.
impl_serde_for_newtypeserde
Implements Serialize and Deserialize for a new type created with crate::hash_newtype macro.
sha256t_hash_newtypeDeprecated
Macro used to define a newtype tagged hash.
sha256t_tag
Macro used to define a tag.

Structs§

FromSliceError
Attempted to create a hash from an invalid length slice.
Hash160
HASH-160: Alias for the hash160::Hash hash type. Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))
Hkdf
HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
Hmac
A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
HmacEngine
Pair of underlying hash engines, used for the inner and outer hash of HMAC.
Ripemd160
RIPEMD-160: Alias for the ripemd160::Hash hash type. Output of the RIPEMD160 hash function.
Sha1
SHA-1: Alias for the sha1::Hash hash type. Output of the SHA1 hash function.
Sha256
SHA-256: Alias for the sha256::Hash hash type. Output of the SHA256 hash function.
Sha384
SHA-384: Alias for the sha384::Hash hash type. Output of the SHA384 hash function.
Sha512
SHA-512: Alias for the sha512::Hash hash type. Output of the SHA512 hash function.
Sha256d
Double SHA-256: Alias for the sha256d::Hash hash type. Output of the SHA256d hash function.
Sha512_256
SHA-512-256: Alias for the sha512_256::Hash hash type. Output of the SHA512/256 hash function.
Siphash24
SipHash-2-4: Alias for the siphash24::Hash hash type. Output of the SipHash24 hash function.

Traits§

GeneralHash
Trait describing hash digests which can be constructed by hashing arbitrary data.
Hash
Trait which applies to hashes of all types.
HashEngine
A hashing engine which bytes can be serialized into.
IsByteArray
Ensures that a type is an array.

Functions§

debug_hex
Writes bytes as a hex string to the formatter.

Type Aliases§

HkdfSha256
HKDF-HMAC-SHA-256: Type alias for the Hkdf<Sha256> type.
HkdfSha512
HKDF-HMAC-SHA-512: Type alias for the Hkdf<Sha512> type.
HmacSha256
HMAC-SHA-256: Type alias for the Hmac<Sha256> type.
HmacSha512
HMAC-SHA-512: Type alias for the Hmac<Sha512> type.
Sha256t
Tagged SHA-256: Type alias for the sha256t::Hash hash type.