md5 hashing acting strangly?
bearophile via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 16 05:15:33 PDT 2014
Sean Campbell:
> i have the following code
> char[] Etag(string file){
> auto info = DirEntry(file);
> ubyte[16] hash = md5Of(to!string(info.timeLastAccessed.day)~
> to!string(info.timeLastAccessed.month)~
> to!string(~info.timeLastAccessed.year)~
> file);
> char[] hashstring = toHexString(hash);
> writeln(hashstring);
> return hashstring;
> }
> the proper hash string prints out in the console, but when i
> try to use the return value of Etag it becomes an obscure
> string?
As shown in the docs toHexString returns a fixed size array, that
in D are values. With "char[] hashstring = " you are slicing a
stack allocated variable, so Etag returns garbage. So you can
write (note the starting lowercase):
auto etag(string file) {
...
> auto hashstring = toHexString(hash);
> writeln(hashstring);
> return hashstring;
> }
Such kind of bugs are impossible in Rust. Hopefully we'll
eventually remove them from D too.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list