#crypto

nightly no-std sha2-const

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

3 releases

0.1.2 Nov 15, 2021
0.1.1 May 4, 2020
0.1.0 May 4, 2020

#337 in Cryptography

Download history 23/week @ 2022-06-04 4/week @ 2022-06-11 13/week @ 2022-06-18 5/week @ 2022-06-25 21/week @ 2022-07-02 102/week @ 2022-07-09 176/week @ 2022-07-16 60/week @ 2022-07-23 102/week @ 2022-07-30 120/week @ 2022-08-06 356/week @ 2022-08-13 218/week @ 2022-08-20 187/week @ 2022-08-27 627/week @ 2022-09-03 1041/week @ 2022-09-10 167/week @ 2022-09-17

2,094 downloads per month
Used in 3 crates (via openbrush_lang)

MIT/Apache

4.5MB
461 lines

sha2-const

Build status Crate Documentation

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.


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:

# use sha2_const::Sha256;
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