Crates.io | sha_256 |
lib.rs | sha_256 |
version | 1.0.1 |
source | src |
created_at | 2024-10-25 10:33:59.78671 |
updated_at | 2024-10-26 14:42:14.378613 |
description | Optimised sha-256 |
homepage | https://github.com/goastler/sha_256 |
repository | https://github.com/goastler/sha_256 |
max_upload_size | |
id | 1422323 |
size | 199,100 |
A fast implementation of sha-256 in rust.
Partially unrolled loops enhance efficiency by optimizing CPU cache utilization.
Bypasses Rust's safety checks to eliminate array index safety validations.
Utilizes only stack memory, avoiding dynamic memory allocation (malloc
).
Reduces memory footprint through array reuse across multiple SHA-256 stages.
Prevents memory reallocation, allowing subsequent SHA-256 calls to reuse existing memory.
Optimized memory layout increases CPU cache hit rates.
Avoids unnecessary byte array conversions (e.g., u8a to u32a).
Written entirely in Rust, with no embedded assembly or specific CPU instructions.
No external dependencies.
Does not require the standard library (std
).
In your project, run:
cargo add sha_256
Import the library
use sha_256::Sha256;
Create an instance of the sha256 struct.
let mut sha256: Sha256 = Sha256::new();
Create your message in bytes.
let bytes = &[0u8, 1u8, 2u8];
Run sha256 to create a digest/hash.
let hash: [u8; 32] = sha256.digest(bytes);
The general idea is "bytes in, bytes out". This is the most efficient input and output type to minimise conversions.
You will need to convert your input into bytes, e.g. string to bytes. See example project.
If you want the hash as a hex string you will need to convert it from bytes to hex afterwards. See example project.
How fast is this library? Up to 25% faster than the sha256
and sha
. They contain use of Intel's SHA-NI
cpu instructions (via a feature flag), whereas this library uses pure rust.
However, the above figures were obtained through some rough benchmarks on only my hardware. More thorough benchmarks are required, YMMV!
// TODO further benchmarks