Skip to content

std::hash::Hash documentation should suggest that the hash data should be prefix-free #89429

Closed
@kpreid

Description

@kpreid

As discussed at Why does str.hash(…) pass an extra byte to the Hasher?: the code of impl Hash for &str specifically passes an extra 0xFF byte to the Hasher, so that values like ("ab", "c") and ("a", "bc") hash differently (added in 6066118).

This is a subtle property of hashing and should probably be mentioned in the documentation for Hashparticularly as the documentation for Hasher already says “you cannot assume, for example, that a write_u32 call is equivalent to four calls of write_u8” which could be sloppily interpreted as an expectation that a good Hasher implementation will handle “quoting” of sequential calls itself.

If an implementor of Hash fails to have this property, it could compromise the hash-DoS resistance that Rust tries to offer by default.

@rustbot labels: +A-docs +T-libs-api +C-enhancement

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions