Skip to content

Commit 7660bdf

Browse files
authored
Auto merge of #36557 - sfackler:fix-hashdos-docs, r=alexcrichton
Clean up hasher discussion on HashMap * We never want to make guarantees about protecting against attacks. * "True randomness" is not the right terminology to be using in this context. * There is significantly more nuance to the performance of SipHash than "somewhat slow". r? @steveklabnik Follow up to discussion on #35371
2 parents c717cfa + aaf32aa commit 7660bdf

File tree

1 file changed

+22
-8
lines changed
  • src/libstd/collections/hash

1 file changed

+22
-8
lines changed

src/libstd/collections/hash/map.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,29 @@ fn test_resize_policy() {
196196
//
197197
// FIXME(Gankro, pczarn): review the proof and put it all in a separate README.md
198198

199-
/// A hash map implementation which uses linear probing with Robin
200-
/// Hood bucket stealing.
199+
/// A hash map implementation which uses linear probing with Robin Hood bucket
200+
/// stealing.
201201
///
202-
/// By default, HashMap uses a somewhat slow hashing algorithm which can provide resistance
203-
/// to DoS attacks. Rust makes a best attempt at acquiring random numbers without IO
204-
/// blocking from your system. Because of this HashMap is not guaranteed to provide
205-
/// DoS resistance since the numbers generated might not be truly random. If you do
206-
/// require this behavior you can create your own hashing function using
207-
/// [BuildHasherDefault](../hash/struct.BuildHasherDefault.html).
202+
/// By default, `HashMap` uses a hashing algorithm selected to provide
203+
/// resistance against HashDoS attacks. The algorithm is randomly seeded, and a
204+
/// reasonable best-effort is made to generate this seed from a high quality,
205+
/// secure source of randomness provided by the host without blocking the
206+
/// program. Because of this, the randomness of the seed is dependant on the
207+
/// quality of the system's random number generator at the time it is created.
208+
/// In particular, seeds generated when the system's entropy pool is abnormally
209+
/// low such as during system boot may be of a lower quality.
210+
///
211+
/// The default hashing algorithm is currently SipHash 1-3, though this is
212+
/// subject to change at any point in the future. While its performance is very
213+
/// competitive for medium sized keys, other hashing algorithms will outperform
214+
/// it for small keys such as integers as well as large keys such as long
215+
/// strings, though those algorithms will typically *not* protect against
216+
/// attacks such as HashDoS.
217+
///
218+
/// The hashing algorithm can be replaced on a per-`HashMap` basis using the
219+
/// `HashMap::default`, `HashMap::with_hasher`, and
220+
/// `HashMap::with_capacity_and_hasher` methods. Many alternative algorithms
221+
/// are available on crates.io, such as the `fnv` crate.
208222
///
209223
/// It is required that the keys implement the [`Eq`] and [`Hash`] traits, although
210224
/// this can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`.

0 commit comments

Comments
 (0)