crypto-hash icon indicating copy to clipboard operation
crypto-hash copied to clipboard

Calling hasher.write_all() after hasher.finish() causes panic on Windows

Open Marcondiro opened this issue 2 years ago • 0 comments

Hello, on Windows a call to write_all after finish causes panic.

Example:

use crypto_hash::{Algorithm, Hasher};
use std::io::Write;

fn main() {
    let mut hasher = Hasher::new(Algorithm::MD5);

    let message1 = b"Hello";
    hasher.write_all(message1).unwrap();
    let _hash1 = hasher.finish();

    let message2 = b"world!";
    hasher.write_all(message2).unwrap();
    let _hash2 = hasher.finish();
}

This is caused by the misusage of the Windows CryptoAPI. As stated here in the Microsoft documentation:

  • The hash value can be retrieved by using CryptGetHashParam
  • [...]

After one of the functions from this list has been called, CryptHashData and CryptHashSessionKey cannot be called.

Marcondiro avatar Dec 09 '22 23:12 Marcondiro