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

Add `Digest::as_write()` method

Open bozaro opened this issue 8 years ago • 2 comments

bozaro avatar Jun 22 '16 10:06 bozaro

@apoelstra I think, this change is better then #294 because this change affects all digests (SHA1, SHA256, MD5, HMAC etc).

bozaro avatar Jun 22 '16 10:06 bozaro

Usage example:

use crypto::digest::Digest;
use crypto::md5::Md5;
use std::io;
use std::io::{Error, Read, Write};

pub fn stream_md5<R: Read>(reader: &mut R) -> Result<String, Error> {
    let mut hash = Md5::new();
    try! (io::copy(reader, &mut hash.as_write()));
    Ok(hash.result_str())
}

bozaro avatar Jun 22 '16 10:06 bozaro