Search code examples
copyrangeddigest

std.algorithm.copy and std.digest


When I use std.algorithm.copy on std.digest objects, I get different results compared when I am using put byte by byte. Why?

import std.stdio;
import std.digest.digest;
import std.digest.md;
import std.algorithm;

void main() {
    string s = "Hello!\n";
    auto d1 = makeDigest!MD5;
    auto d2 = makeDigest!MD5;
    foreach (ubyte b; s) {
        d1.put(b);
    }
    s.copy(d2);
    writeln(digest!MD5(s).toHexString);
    writeln(d1.finish().toHexString);
    writeln(d2.finish().toHexString);
}

Output:

E134CED312B3511D88943D57CCD70C83
E134CED312B3511D88943D57CCD70C83
D41D8CD98F00B204E9800998ECF8427E

Solution

  • d2 is passed by value to copy. The data gets copied inside the function, but then when it returns, the d2 variable on the outside is unmodified!

    I kinda think this might be a bug: the current behavior doesn't make a great deal of sense to me. When you are copying it, it makes sense to do it by reference. The unittests only test arrays which are half-reference (they are pointers) and it works for them.