#sha-2 #hash #constant #function #compile-time #const #family

no-std sha2-const-stable

const fn implementation of the SHA-2 family of hash functions. Based on sha2-const, but updated to use only stable rust

1 unstable release

0.1.0 Jan 18, 2024

#803 in Cryptography

Download history 9369/week @ 2024-07-21 11091/week @ 2024-07-28 28921/week @ 2024-08-04 12326/week @ 2024-08-11 10730/week @ 2024-08-18 10628/week @ 2024-08-25 11948/week @ 2024-09-01 12475/week @ 2024-09-08 10356/week @ 2024-09-15 12545/week @ 2024-09-22 10882/week @ 2024-09-29 13754/week @ 2024-10-06 16507/week @ 2024-10-13 18752/week @ 2024-10-20 17009/week @ 2024-10-27 17089/week @ 2024-11-03

69,711 downloads per month
Used in 53 crates (3 directly)

MIT/Apache

4.5MB
473 lines

sha2-const-stable

const fn implementation of the SHA-2 family of hash functions.

This crate allows you to use the SHA-2 hash functions as constant expressions in Rust. It is adapted from sha2-const, which requires nightly. For all other usages, the sha2 crate includes more optimized implementations of these hash functions.


lib.rs:

const fn implementation of the SHA-2 family of hash functions.

This crate allows you to use the SHA-2 hash functions as constant expressions in Rust. For all other usages, the sha2 crate includes more optimized implementations of these hash functions.

Examples

Compute the SHA-256 hash of the Bitcoin genesis block at compile time:

const VERSION: u32 = 1;
const HASH_PREV_BLOCK: [u8; 32] = [0; 32];
const HASH_MERKLE_ROOT: [u8; 32] = [
    0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f,
    0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e,
    0x5e, 0x4a,
];
const TIME: u32 = 1231006505;
const BITS: u32 = 0x1d00ffff;
const NONCE: u32 = 0x7c2bac1d;

const BLOCK_HASH: [u8; 32] = Sha256::new()
    .update(
        &Sha256::new()
            .update(&VERSION.to_le_bytes())
            .update(&HASH_PREV_BLOCK)
            .update(&HASH_MERKLE_ROOT)
            .update(&TIME.to_le_bytes())
            .update(&BITS.to_le_bytes())
            .update(&NONCE.to_le_bytes())
            .finalize(),
    )
    .finalize();

assert_eq!(
    hex::encode(&BLOCK_HASH[..]),
    "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000"
);

No runtime deps