The review of std.hash package

Walter Bright newshound2 at digitalmars.com
Wed Aug 8 11:40:10 PDT 2012


On 8/8/2012 5:13 AM, Martin Nowak wrote:
>> It should accept an input range. But using an Output Range confuses me. A hash
>> function is a reduce algorithm - it accepts a sequence of input values, and
>> produces a single value. You should be able to write code like:
>>
>>    ubyte[] data;
>>    ...
>>    auto crc = data.crc32();
>>
>> For example, the hash example given is:
>>
>>    foreach (buffer; file.byChunk(4096 * 1024))
>>        hash.put(buffer);
>>    auto result = hash.finish();
>>
>> Instead it should be something like:
>>
>>    auto result = file.byChunk(4096 * 1025).joiner.hash();
>>
> I think sha1Of/digest!SHA1 should do this.
> It's also important to have a stateful hash implementation that can be updated
> incrementally, e.g. from a callback.

Take a look at the reduce function in 
http://dlang.org/phobos/std_algorithm.html#reduce

It has provision for an initial state that can be the current running total.



More information about the Digitalmars-d mailing list