| 1 |
# RustCrypto: SHA-2 |
| 2 |
|
| 3 |
[![crate][crate-image]][crate-link] |
| 4 |
[![Docs][docs-image]][docs-link] |
| 5 |
![Apache2/MIT licensed][license-image] |
| 6 |
![Rust Version][rustc-image] |
| 7 |
[![Project Chat][chat-image]][chat-link] |
| 8 |
[![Build Status][build-image]][build-link] |
| 9 |
|
| 10 |
Pure Rust implementation of the [SHA-2] cryptographic hash algorithms. |
| 11 |
|
| 12 |
There are 6 standard algorithms specified in the SHA-2 standard: |
| 13 |
`Sha224`, `Sha256`, `Sha512_224`, `Sha512_256`, `Sha384`, and `Sha512`. |
| 14 |
|
| 15 |
Algorithmically, there are only 2 core algorithms: SHA-256 and SHA-512. |
| 16 |
All other algorithms are just applications of these with different initial |
| 17 |
hash values, and truncated to different digest bit lengths. The first two |
| 18 |
algorithms in the list are based on SHA-256, while the last four are based |
| 19 |
on SHA-512. |
| 20 |
|
| 21 |
## Examples |
| 22 |
|
| 23 |
### One-shot API |
| 24 |
|
| 25 |
```rust |
| 26 |
use sha2::{Sha256, Digest}; |
| 27 |
use hex_literal::hex; |
| 28 |
|
| 29 |
let hash = Sha256::digest(b"hello world"); |
| 30 |
assert_eq!(hash, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")); |
| 31 |
``` |
| 32 |
|
| 33 |
### Incremental API |
| 34 |
|
| 35 |
```rust |
| 36 |
use sha2::{Sha256, Sha512, Digest}; |
| 37 |
use hex_literal::hex; |
| 38 |
|
| 39 |
let mut hasher = Sha256::new(); |
| 40 |
hasher.update(b"hello "); |
| 41 |
hasher.update(b"world"); |
| 42 |
let hash256 = hasher.finalize(); |
| 43 |
|
| 44 |
assert_eq!(hash256, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")); |
| 45 |
|
| 46 |
let mut hasher = Sha512::new(); |
| 47 |
hasher.update(b"hello world"); |
| 48 |
let hash512 = hasher.finalize(); |
| 49 |
|
| 50 |
assert_eq!(hash512, hex!( |
| 51 |
"309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f" |
| 52 |
"989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f" |
| 53 |
)); |
| 54 |
``` |
| 55 |
|
| 56 |
See the [`digest`] crate docs for additional examples. |
| 57 |
|
| 58 |
## Backends |
| 59 |
|
| 60 |
This crate supports a number of different backends. |
| 61 |
|
| 62 |
SHA-256 and SHA-512 backends: |
| 63 |
- `soft`: portable software implementation |
| 64 |
- `loongarch64-asm`: `asm!`-based implementation for LoongArch64 targets |
| 65 |
- `riscv-zknh`: uses the RISC-V `Zknh` scalar crypto extension. Experimental, |
| 66 |
requires Nightly compiler and to enable `Zknh` and `Zbkb` (or `Zbb`) |
| 67 |
target features at compile time. |
| 68 |
- `wasm32-simd128`: uses the WASM `simd128` extension. |
| 69 |
|
| 70 |
SHA-256 only backends: |
| 71 |
- `aarch64-sha2`: uses the AArch64 `sha2` extension. |
| 72 |
- `x86-sha`: uses the x86 SHA-NI extension. |
| 73 |
|
| 74 |
SHA-512 only backends: |
| 75 |
- `aarch64-sha3`: uses the AArch64 `sha3` extension. |
| 76 |
- `x86-avx2`: uses the x86 AVX2 extension. |
| 77 |
|
| 78 |
By default the following backends are used: |
| 79 |
- `target_arch = "aarch64"`: use `aarch64-sha2` and `aarch64-sha3` when the required |
| 80 |
target features are detected at runtime; otherwise fall back to `soft`. |
| 81 |
- `any(target_arch = "x86", target_arch = "x86_64")`: use `x86-sha` and `x86-avx` when |
| 82 |
the required target features are detected at runtime; otherwise fall back to `soft`. |
| 83 |
- `target_arch = "loongarch64"`: use `loongarch64-asm`. |
| 84 |
- `all(target_arch = "wasm32", target_feature = "simd128")`: use `wasm32-simd128`. |
| 85 |
- All other targets: use `soft`. |
| 86 |
|
| 87 |
You can force backend selection using the following configuration flags: |
| 88 |
- `sha2_backend`: select SHA-256 and SHA-512 backends. Supported values: `soft`, `riscv-zknh`. |
| 89 |
- `sha2_256_backend`: select SHA-256 backend. Supported values: `aarch64-sha2`, `soft`, |
| 90 |
`riscv-zknh`, `x86-sha`. |
| 91 |
- `sha2_512_backend`: select SHA-512 backend. Supported values: `aarch64-sha3`, `soft`, |
| 92 |
`riscv-zknh`, `x86-avx2`. |
| 93 |
|
| 94 |
They can be enabled using either the `RUSTFLAGS` environment variable |
| 95 |
(e.g. `RUSTFLAGS='--cfg sha2_backend="soft"'`), or by modifying your `.cargo/config.toml` file. |
| 96 |
|
| 97 |
Note that `sha2_backend` has higher priority than `sha2_256_backend` and `sha2_512_backend`. |
| 98 |
In other words, using `--cfg sha2_backend="soft" --cfg sha2_256_backend="x86_sha"` will result |
| 99 |
in selection of the software backend for SHA-256. |
| 100 |
|
| 101 |
By default `soft` and `riscv-zknh` backends unroll round loops, which results in a better |
| 102 |
performance at the cost of a bigger resulting binary. You can disable unrolling in the backends |
| 103 |
by using `sha2_backend_soft = "compact"` and `sha2_backend_riscv_zknh = "compact"` configuration |
| 104 |
flags respectively. |
| 105 |
|
| 106 |
## License |
| 107 |
|
| 108 |
The crate is licensed under either of: |
| 109 |
|
| 110 |
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) |
| 111 |
* [MIT license](http://opensource.org/licenses/MIT) |
| 112 |
|
| 113 |
at your option. |
| 114 |
|
| 115 |
### Contribution |
| 116 |
|
| 117 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 118 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 119 |
dual licensed as above, without any additional terms or conditions. |
| 120 |
|
| 121 |
[//]: # (badges) |
| 122 |
|
| 123 |
[crate-image]: https://img.shields.io/crates/v/sha2.svg |
| 124 |
[crate-link]: https://crates.io/crates/sha2 |
| 125 |
[docs-image]: https://docs.rs/sha2/badge.svg |
| 126 |
[docs-link]: https://docs.rs/sha2/ |
| 127 |
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg |
| 128 |
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg |
| 129 |
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg |
| 130 |
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260041-hashes |
| 131 |
[build-image]: https://github.com/RustCrypto/hashes/actions/workflows/sha2.yml/badge.svg?branch=master |
| 132 |
[build-link]: https://github.com/RustCrypto/hashes/actions/workflows/sha2.yml?query=branch:master |
| 133 |
|
| 134 |
[//]: # (general links) |
| 135 |
|
| 136 |
[SHA-2]: https://en.wikipedia.org/wiki/SHA-2 |
| 137 |
[`digest`]: https://docs.rs/digest |