js-multiformats
js-multiformats copied to clipboard
feat: incremental-hasher
Proposal for the #260
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.
What about digest()
-> multihash and rawDigest()
-> digest without multihash header?
I'd really like this to land! I find myself wanting to do this more and more...
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);
}