Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hash_one calls hash_str #130475

Closed
daedric opened this issue Sep 17, 2024 · 2 comments
Closed

hash_one calls hash_str #130475

daedric opened this issue Sep 17, 2024 · 2 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@daedric
Copy link

daedric commented Sep 17, 2024

Hi folks,

I tried this code:

use std::hash::{BuildHasher, Hasher, RandomState};

pub fn main() {

    let s = RandomState::new();
    
    let mut hasher = s.build_hasher();
    hasher.write(b"toto");
    // hasher.write_str("toto"); // does not compile.
    println!("{}", hasher.finish());

    println!("{}", s.hash_one("toto"));
}

I expected to see this happen: same result or compilation error

Instead, this happened: got different hashes.

Meta

rustc --version --verbose:

rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
LLVM version: 18.1.7

Backtrace

core::hash::Hasher::write_str mod.rs:554
core::hash::impls::<impl core::hash::Hash for str>::hash mod.rs:871

Since write_str is not callable in stable (#96762) and the following code cannot be compiled on stable version:

        hasher.write_str("toto");

Final word

Overall, I'm not sure, I understand why it is implemented this way. In the end, I hash bytes and given the same bytes, I want the same hash, regardless of the type of write I do.
I do understand that some hash algorithms work differently and maybe block algorithms need a different interface, but this behavior was definitely not expected on my side.

I also do not expect stable version to call unstable flagged ones.

@daedric daedric added the C-bug Category: This is a bug. label Sep 17, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 17, 2024
@ShE3py
Copy link
Contributor

ShE3py commented Sep 17, 2024

You're hashing different types;

use std::hash::{BuildHasher, RandomState};

pub fn main() {
    let s = RandomState::new();
    println!("{}", s.hash_one("toto"));  // 10326407776415938863
    println!("{}", s.hash_one(b"toto")); // 2295326717856105980
}

You probably meant to write this:

use std::hash::{BuildHasher, Hash, Hasher, RandomState};

pub fn main() {
    let s = RandomState::new();
    
    let mut hasher = s.build_hasher();
//- hasher.write(b"toto");
    "toto".hash(&mut hasher);
    println!("{}", hasher.finish());    // 3485953824124918165

    println!("{}", s.hash_one("toto")); // 3485953824124918165
}

See #27108, Rustdoc: Hash: Prefix collisions, RandomState's write_str

@workingjubilee
Copy link
Member

workingjubilee commented Sep 17, 2024

@daedric Almost the entire stable API of the standard library is implemented by calling unstable API. Almost every function bottoms out at a hidden internal intrinsic somewhere.

@jieyouxu jieyouxu added T-libs Relevant to the library team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 18, 2024
@daedric daedric closed this as completed Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants