bitcoin_wire/hash256
A fixed-width hash value used throughout Bitcoin wire data.
Types
A generic 256-bit hash value stored in Bitcoin wire order.
This name describes the value’s width, not the single-SHA-256 algorithm.
Bitcoin serializes these hashes in little-endian byte order, which is the
order this type stores and exposes through to_bytes_le.
pub opaque type Hash256
An error that occurred while constructing a Hash256.
pub type Hash256Error {
InvalidBitCount(actual: Int, expected: Int)
}
Constructors
-
InvalidBitCount(actual: Int, expected: Int)The input did not contain exactly 256 bits.
The fields contain the measured and required bit counts, respectively.
Values
pub fn from_bytes_le(
bytes: BitArray,
) -> Result(Hash256, Hash256Error)
Construct a Hash256 from exactly 32 little-endian bytes.
Returns an error if the supplied value does not contain exactly 256 bits. The bytes are stored verbatim without interpreting the hash’s semantic role.
Examples
from_bytes_le(<<0:size(256)>>)
// -> Ok(Hash256) representing an all-zero hash
from_bytes_le(<<1, 2, 3>>)
// -> Error(InvalidBitCount(actual: 24, expected: 256))
pub fn to_bytes_le(hash: Hash256) -> BitArray
Return the raw little-endian byte representation of a hash.
The returned BitArray is always exactly 32 bytes long and uses the same
byte order found in Bitcoin wire data.
pub fn to_display_hex(hash: Hash256) -> String
Format a hash using Bitcoin’s conventional lowercase display notation.
Bitcoin displays identifiers such as txids and block hashes with the wire-order bytes reversed. The returned string therefore contains exactly 64 lowercase hexadecimal characters.