Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

[Help] hashing(Digest) problem

Moderators: larsivi kris

Posted: 09/15/09 15:59:34 Modified: 09/15/09 16:09:24

Hello everyone,

I am new to D programming language, and I am having some problems :P

ok here is my problem

I am trying to hash(Digest) a file using Tango's predefined digests. here is the code I used

public void hashTestMethod(char[] filePath){
		Md5 d = new Md5();
		auto f = new FileConduit(filePath);
		ubyte[] buffer = new ubyte[2048*1024];
		int i = f.read(buffer);

		while (i!=f.Eof){
			d.update(buffer);
			i = f.read(buffer);
		}

		txtResult.setText(d.hexDigest());
		f.close();
	}

using this code I get a wrong result, but if I use the followig code I get the right result

public void hashTestMethod(char[] filePath){
		Md5 d = new Md5();
		auto f = new FileConduit(filePath);
		d.update(f.load());
		txtResult.setText(d.hexDigest());
		f.close();
	}

but the previous code loads the whole file to the memory which is not a good Idea

can anyone guide me to a proper way using buffers also if anyone can point me to a threading tutorial in D (how data is shared between threads, concurency management ....)

thank you.

Fileburner.

Author Message

Posted: 09/15/09 22:39:12

Subtle change to update only what was read:

while (i != f.Eof)
      {
      d.update (buffer[0..i]);
      i = f.read (buffer);
      }

See the slice operation within d.update? This takes care of a partial read at the end of the file. You might also look at tango.io.stream.Digest, which does a similar kind of thing

Posted: 09/16/09 15:20:09

thank you very much for your reply, now it works :D I almost forgot about array slicing, the strange thing I noticed is that whenever I change the buffer's size the hash result changes thank you