RustCrypto Hashes

repository·master·Indexed 25 days ago

https://github.com/rustcrypto/hashes

A collection of cryptographic hash functions written in pure Rust. The library provides a wide variety of algorithms, most of which are no_std compatible and implement standard digest traits for interoperability. Supported algorithms include Ascon-Hash256, BashHash256, BelT, BLAKE2 (fixed and variable output), FSB (160, 224, 256, 384, 512), GOST94, Grøstl, JH, Kupyna, MD2, MD4, and MD5.

Tokens
15.2K
Snippets
38
Records
92
Agent score
81%

What's inside rustcrypto-hashes

  1. Use SHA-3 hash algorithms

    master

    This crate provides implementations of the SHA-3 family of cryptographic hash algorithms.

    Supported Standard Algorithms

    The following fixed-size algorithms are available:

    • Sha3_224
    • Sha3_256
    • Sha3_384
    • Sha3_512

    Additional Supported Variants

    • Keccak: Keccak224, Keccak256, Keccak384, Keccak512 (NIST submission without padding changes).
    • CryptoNight variant: Keccak256Full.

    Note: For extendable output functions (XOF) like SHAKE128 and SHAKE256, use the [shake] crate instead.

  2. Use the Skein hash family

    master

    The skein crate provides implementations of the Skein family of cryptographic hash algorithms. There are three standard versions available:

    • Skein256
    • Skein512
    • Skein1024

    Because the output size of Skein hash functions is arbitrary, you must specify the desired output size using an additional type parameter (e.g., Skein512_256 produces a 256-bit hash using the Skein512 algorithm).

  3. Security Warning: SHA-1 is cryptographically broken

    master

    🚨 Warning: Cryptographically Broken! 🚨

    SHA-1 is unsuitable for security-critical applications because it is vulnerable to chosen-prefix collisions.

    This crate implements a collision detection algorithm (similar to the one used by Git) to detect and prevent hash collisions when they occur. Note that this implementation is slower than pure SHA-1 because it performs additional computations and cannot rely on hardware acceleration.

  4. How RustCrypto hashes work with the digest crate

    master
    All algorithms in this repository are implemented using traits from the [digest] crate. This ensures interoperability across the RustCrypto ecosystem. For usage examples, refer to the documentation of the specific hash implementation crate or the [digest] crate itself.
  5. Security Warning: MD5 is cryptographically broken

    master

    ⚠️ MD5 is cryptographically broken and unsuitable for further use.

    This crate is provided strictly for legacy interoperability with protocols and systems that mandate MD5. Collision attacks are practical and trivial, and preimage resistance has been weakened. RFC 6151 advises against using MD5-based constructions in new IETF protocols.

  6. Use RustCrypto hashes in no_std or WebAssembly environments

    master
    All hash crates in this repository are no_std capable. This means they do not require the Rust standard library and can be used for bare-metal or WebAssembly (Wasm) programming. To use them in these environments, you must disable the default crate features.
  7. Use the JH hash function in Rust

    master

    The jh crate provides a pure Rust implementation of the JH cryptographic hash function. It supports four standard versions:

    • JH-224
    • JH-256
    • JH-384
    • JH-512

    To use the hash function, instantiate a hasher using ::new(), feed data into it using .update(), and retrieve the resulting hash with .finalize(). This implementation follows the Digest trait patterns common in the RustCrypto ecosystem.

    use jh::{Digest, Jh256};
    use hex_literal::hex;
    
    let mut hasher = Jh256::new();
    hasher.update(b"hello");
    let hash = hasher.finalize();
    
    assert_eq!(hash, hex!("94fd3f4c564957c6754265676bf8b244c707d3ffb294e18af1f2e4f9b8306089"));
  8. Configure SHA-2 backends via RUSTFLAGS or .cargo/config.toml

    master

    The sha2 crate supports various hardware-accelerated and software backends. You can force a specific backend using configuration flags. These can be set via the RUSTFLAGS environment variable or in your .cargo/config.toml file.

    Priority Note: sha2_backend has higher priority than sha2_256_backend and sha2_512_backend. If you set sha2_backend="soft", it will override any specific SHA-256 or SHA-512 backend selection.

  9. Use the FSB hash algorithms in Rust

    master

    The fsb crate provides a pure Rust implementation of the FSB cryptographic hash algorithms. It supports five standard versions: FSB-160, FSB-224, FSB-256, FSB-384, and FSB-512.

    For fixed-size outputs like FSB-256, the functionality is accessed via the Digest trait. You can initialize a hasher using ::new(), feed data into it using .update(), and retrieve the final hash using .finalize().

    use fsb::{Digest, Fsb256};
    use hex_literal::hex;
    
    let mut hasher = Fsb256::new();
    hasher.update(b"hello");
    let hash = hasher.finalize();
    
    assert_eq!(hash, hex!("0f036dc3761aed2cba9de586a85976eedde6fa8f115c0190763decc02f28edbc"));
  10. Setup experimental RISC-V Zknh backend

    master
    The riscv-zknh backend is experimental and requires a Nightly compiler. To use it, you must enable the Zknh target feature at compile time. For optimal performance, it is recommended to also enable Zbkb (or Zbb) and unaligned-scalar-mem.