Stream mixins?

Kris foo at bar.com
Sun Nov 11 11:10:06 PST 2007


"Janice Caron" <caron800 at googlemail.com>
[snip]
> What I was actually planning to do was implement a hash function (e.g.
> SHA-256) as an output stream. You write your bytes to the stream, then
> call a function to get the hash of those bytes. By definition this
> would /have/ to be an output-only stream. That's the whole POINT of a
> hash function - it's /one way/!

Indeed. You perhaps don't care about this, Janice, but others may since they 
won't have to write it themselves:

Tango has a broad suite of digest classes, including MD2, MD4, MD5, SHA0, 
SHA01, SHA1, SHA256, and Tiger (contributed by NG regulars). You can use 
those independently, or combine with a DigestStream to slurp the digest as 
it flow by. For example:

# auto stream = new DigestOutput (new GrowBuffer, new Sha256);
# stream.write (somedata)
# stream.write (moredata)
# ....
# auto digest = stream.digest.binaryDigest;


And similarly for input, if you need that also:

# auto stream = new DigestInput (new FileInput("myfile"), new Sha256);
# stream.read (somedata)
# stream.read (moredata)
# ....
# auto digest = stream.digest.binaryDigest;



> But I don't see why, in order to do that, I should have to implement:
>
>    abstract void write(char[] s);
>    abstract void write(const(wchar)[] s);


Yeah, I agree.





More information about the Digitalmars-d mailing list