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

Incorrect hash calculation #89

Open
aochagavia opened this issue Jan 31, 2023 · 0 comments
Open

Incorrect hash calculation #89

aochagavia opened this issue Jan 31, 2023 · 0 comments

Comments

@aochagavia
Copy link

aochagavia commented Jan 31, 2023

There is an error with the current implementation of DigestBuf::write, which causes incorrect hashes to be calculated. The problem is that the call to self.inner.write is not guaranteed to write all the bytes in buf, so the same bytes might be offered to the hasher more than once.

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.hasher.update(buf);
self.inner.write(buf)
}

The problem can be solved by rewriting the function as follows:

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
    let written = self.inner.write(buf)?;
    self.hasher.update(&buf[..written]);
    Ok(written)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant