Crate chf

source ·
Expand description

Rust Cryptographic Hash Functions.

This is a simple, no-dependency library which implements crypotographic hash functions. At the moment this includes:

  • SHA-1
  • SHA-2
    • SHA-256
    • SHA-384
    • SHA-512
    • SHA-512/256
  • RIPEMD-160
  • SipHash-2-4
  • HMAC-x (where x is any of the hash functions above).
  • Domain separation using tagged SHA-256.

§Commonly used operations

Hashing a single byte slice or a string:

use chf::sha256;

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 chf::sha256;

#[cfg(std)]
let mut reader: &[u8] = b"hello"; // in real code, this could be a `File` or `TcpStream`
let mut engine = sha256::Engine::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 chf::sha256;
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::Engine::default();
engine.write_all(part1)?;
engine.write_all(part2)?;
engine.write_all(part3)?;
let hash = sha256::Hash::from_engine(engine);

#[cfg(not(std))]

Re-exports§

  • pub extern crate hex;
  • pub extern crate serde;

Modules§

  • Useful comparison functions.
  • Hash-based Message Authentication Code (HMAC).
  • RIPEMD160 implementation.
  • Macros for serde trait implementations, and supporting code.
  • SHA-1 implementation.
  • SHA-256 implementation.
  • SHA256t implementation (tagged SHA256).
  • SHA-384 implementation.
  • SHA-512 implementation.
  • SHA-512/256 implementation.
  • SipHash 2-4 implementation.

Macros§

  • Implements Serialize and Deserialize for a type $t which represents a newtype over a byte-slice over length $len.

Structs§

  • Attempted to create a hash from an invalid length slice.

Traits§

  • A hashing engine which bytes can be serialized into.