js-multiformats icon indicating copy to clipboard operation
js-multiformats copied to clipboard

feat: incremental-hasher

Open Gozala opened this issue 1 year ago • 4 comments

Proposal for the #260

Gozala avatar Jul 19 '23 17:07 Gozala

I'm realizing now that interface here is intentionally non-destructive, as in I could compute digest over and over. Unfortunately node crypto APIs are destructive though

The Hash object can not be used again after hash.digest() method has been called. Multiple calls will cause an error to be thrown.

Instead node provides copy method so you could continue writing into the hasher copy.

Given this the case with node, proposed API seems impractical, perhaps instead we could also introduce same constraint and copy() method. Better implementations could return same instance from the copy while ones that wrap node crypto APIs would avoid making a copy just in case.

On the other hand copy on digest maybe negligible overhead, in which case API without copy would be nicer.

Gozala avatar Jul 20 '23 19:07 Gozala

What about digest() -> multihash and rawDigest() -> digest without multihash header?

alanshaw avatar Jul 22 '23 14:07 alanshaw

I'd really like this to land! I find myself wanting to do this more and more...

alanshaw avatar Aug 04 '23 15:08 alanshaw

For what it's worth Rust Multihash hasher has a similar interface to one proposed here

/// Trait implemented by a hash function implementation.
pub trait Hasher {
    /// Consume input and update internal state.
    fn update(&mut self, input: &[u8]);

    /// Returns the final digest.
    fn finalize(&mut self) -> &[u8];

    /// Reset the internal hasher state.
    fn reset(&mut self);
}

Gozala avatar Aug 12 '23 06:08 Gozala