Skip to content

Commit 6b1d40f

Browse files
committed
feat: default code table can be changed at compile time
It is now possible to change the contents of the default code table with enabling/disabling various features. If you e.g. only need SHA2 hashes, you can specify that when you use the `multihash` dependency: multihash = { version = …, default-features = false, features = ["std", "multihash-impl", "sha2"] } BREAKING CHANGE: Only secure hashers are enabled by default
1 parent 9810f18 commit 6b1d40f

File tree

4 files changed

+58
-19
lines changed

4 files changed

+58
-19
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ jobs:
5858
- name: Run cargo-tarpaulin
5959
uses: actions-rs/[email protected]
6060
with:
61-
version: '0.11.0'
62-
args: '-- --test-threads 1'
61+
args: '--all-features -- --test-threads 1'
6362

6463
- name: Upload to codecov.io
6564
uses: codecov/[email protected]

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ documentation = "https://docs.rs/multihash/"
1414
edition = "2018"
1515

1616
[features]
17-
default = ["std", "all", "derive", "multihash-impl"]
17+
default = ["std", "derive", "multihash-impl", "secure-hashes"]
1818
std = ["unsigned-varint/std", "multihash-derive/std"]
19-
multihash-impl = ["derive", "all"]
19+
multihash-impl = ["derive"]
2020
derive = ["multihash-derive"]
2121
arb = ["quickcheck", "rand"]
22-
all = ["blake2b", "blake2s", "blake3", "sha1", "sha2", "sha3", "strobe"]
22+
secure-hashes = ["blake2b", "blake2s", "blake3", "sha2", "sha3"]
2323
scale-codec = ["parity-scale-codec"]
2424
serde-codec = ["serde", "generic-array/serde"]
2525

2626
blake2b = ["blake2b_simd"]
2727
blake2s = ["blake2s_simd"]
28+
identity = []
2829
sha1 = ["digest", "sha-1"]
2930
sha2 = ["digest", "sha-2"]
3031
sha3 = ["digest", "sha-3"]

src/lib.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,51 @@
33
//! Feature Flags
44
//! -------------
55
//!
6-
//! Multihash has lots of [feature flags], by default all features, except for `arb`, `scale-codec`
7-
//! and `serde-codec` are enabled.
6+
//! Multihash has lots of [feature flags], by default a table with cryptographically secure hashers
7+
//! is created.
88
//!
9-
//! Some of the features are about specific hash functions, these are:
9+
//! Some of the features are about specific hash functions, these are ("default" marks the hashers
10+
//! that are enabled by default):
1011
//!
11-
//! - `blake2b`: Enable Blake2b hashers
12-
//! - `blake2s`: Enable Blake2s hashers
13-
//! - `sha1`: Enable SHA-1 hashers
14-
//! - `sha2`: Enable SHA-2 hashers
15-
//! - `sha3`: Enable SHA-3 hashers
12+
//! - `blake2b`: (default) Enable Blake2b hashers
13+
//! - `blake2s`: (default) Enable Blake2s hashers
14+
//! - `identity`: Enable the Identity hashers (using it is discouraged as it's not a hash function
15+
//! in the sense that it produces a fixed sized output independent of the input size)
16+
//! - `sha1`: Enable SHA-1 hasher
17+
//! - `sha2`: (default) Enable SHA-2 hashers
18+
//! - `sha3`: (default) Enable SHA-3 hashers
1619
//! - `strobe`: Enable Strobe hashers
1720
//!
18-
//! In order to enable all hashers, you can set the `all` feature flag.
21+
//! In order to enable all cryptographically secure hashers, you can set the `secure-hashes`
22+
//! feature flag (enabled by default).
1923
//!
2024
//! The library has support for `no_std`, if you disable the `std` feature flag.
2125
//!
22-
//! The `multihash-impl` feature flag enables a default Multihash implementation that contains some
23-
//! of the bundled hashers. If you want a different set of hash algorithms or add one which isn't
24-
//! supported by default, you will disable that feature. Intead enable the `derive` feature in
25-
//! order to be able to use the [`Multihash` derive], together with the features for the hashers
26-
//! you need.
26+
//! The `multihash-impl` feature flag (enabled by default) enables a default Multihash
27+
//! implementation that contains some of the bundled hashers. If you want a different set of hash
28+
//! algorithms you can change this with enabled the corresponding features.
29+
//!
30+
//! For example if you only need SHA2 hasher, you could set the features in the `multihash`
31+
//! dependency like this:
32+
//!
33+
//! ```toml
34+
//! multihash = { version = …, default-features = false, features = ["std", "multihash-impl", "sha2"] }
35+
//! ```
36+
//!
37+
//! If you want to customize your code table even more, for example you want only one specific hash
38+
//! digest size and not whole family, you would only enable the `derive` feature (enabled by
39+
//! default), which enables the [`Multihash` derive], together with the hashers you want.
2740
//!
2841
//! The `arb` feature flag enables the quickcheck arbitrary implementation for property based
2942
//! testing.
3043
//!
44+
//! For serializing the multihash there is support for [Serde] via the `serde-codec` feature and
45+
//! the [SCALE Codec] via the `scale-codec` feature.
46+
//!
3147
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
3248
//! [`Multihash` derive]: crate::derive
49+
//! [Serde]: https://serde.rs
50+
//! [SCALE Codec]: https://github.com/paritytech/parity-scale-codec
3351
3452
#![deny(missing_docs)]
3553
#![cfg_attr(not(feature = "std"), no_std)]

src/multihash_impl.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,71 @@ use multihash_derive::Multihash;
1010
#[mh(alloc_size = crate::U64)]
1111
pub enum Code {
1212
/// SHA-256 (32-byte hash size)
13+
#[cfg(feature = "sha2")]
1314
#[mh(code = 0x12, hasher = crate::Sha2_256, digest = crate::Sha2Digest<crate::U32>)]
1415
Sha2_256,
1516
/// SHA-512 (64-byte hash size)
17+
#[cfg(feature = "sha2")]
1618
#[mh(code = 0x13, hasher = crate::Sha2_512, digest = crate::Sha2Digest<crate::U64>)]
1719
Sha2_512,
1820
/// SHA3-224 (28-byte hash size)
21+
#[cfg(feature = "sha3")]
1922
#[mh(code = 0x17, hasher = crate::Sha3_224, digest = crate::Sha3Digest<crate::U28>)]
2023
Sha3_224,
2124
/// SHA3-256 (32-byte hash size)
25+
#[cfg(feature = "sha3")]
2226
#[mh(code = 0x16, hasher = crate::Sha3_256, digest = crate::Sha3Digest<crate::U32>)]
2327
Sha3_256,
2428
/// SHA3-384 (48-byte hash size)
29+
#[cfg(feature = "sha3")]
2530
#[mh(code = 0x15, hasher = crate::Sha3_384, digest = crate::Sha3Digest<crate::U48>)]
2631
Sha3_384,
2732
/// SHA3-512 (64-byte hash size)
33+
#[cfg(feature = "sha3")]
2834
#[mh(code = 0x14, hasher = crate::Sha3_512, digest = crate::Sha3Digest<crate::U64>)]
2935
Sha3_512,
3036
/// Keccak-224 (28-byte hash size)
37+
#[cfg(feature = "sha3")]
3138
#[mh(code = 0x1a, hasher = crate::Keccak224, digest = crate::KeccakDigest<crate::U28>)]
3239
Keccak224,
3340
/// Keccak-256 (32-byte hash size)
41+
#[cfg(feature = "sha3")]
3442
#[mh(code = 0x1b, hasher = crate::Keccak256, digest = crate::KeccakDigest<crate::U32>)]
3543
Keccak256,
3644
/// Keccak-384 (48-byte hash size)
45+
#[cfg(feature = "sha3")]
3746
#[mh(code = 0x1c, hasher = crate::Keccak384, digest = crate::KeccakDigest<crate::U48>)]
3847
Keccak384,
3948
/// Keccak-512 (64-byte hash size)
49+
#[cfg(feature = "sha3")]
4050
#[mh(code = 0x1d, hasher = crate::Keccak512, digest = crate::KeccakDigest<crate::U64>)]
4151
Keccak512,
4252
/// BLAKE2b-256 (32-byte hash size)
53+
#[cfg(feature = "blake2b")]
4354
#[mh(code = 0xb220, hasher = crate::Blake2b256, digest = crate::Blake2bDigest<crate::U32>)]
4455
Blake2b256,
4556
/// BLAKE2b-512 (64-byte hash size)
57+
#[cfg(feature = "blake2b")]
4658
#[mh(code = 0xb240, hasher = crate::Blake2b512, digest = crate::Blake2bDigest<crate::U64>)]
4759
Blake2b512,
4860
/// BLAKE2s-128 (16-byte hash size)
61+
#[cfg(feature = "blake2s")]
4962
#[mh(code = 0xb250, hasher = crate::Blake2s128, digest = crate::Blake2sDigest<crate::U16>)]
5063
Blake2s128,
5164
/// BLAKE2s-256 (32-byte hash size)
65+
#[cfg(feature = "blake2s")]
5266
#[mh(code = 0xb260, hasher = crate::Blake2s256, digest = crate::Blake2sDigest<crate::U32>)]
5367
Blake2s256,
5468
/// BLAKE3-256 (32-byte hash size)
69+
#[cfg(feature = "blake3")]
5570
#[mh(code = 0x1e, hasher = crate::Blake3_256, digest = crate::Blake3Digest<crate::U32>)]
5671
Blake3_256,
72+
73+
// The following hashes are not cryptographically secure hashes and are not enabled by default
74+
/// Identity hash (max. 64 bytes)
75+
#[cfg(feature = "identity")]
76+
#[mh(code = 0x00, hasher = crate::IdentityHasher::<crate::U64>, digest = crate::IdentityDigest<crate::U64>)]
77+
Identity,
5778
}
5879

5980
#[cfg(test)]

0 commit comments

Comments
 (0)