Forum Navigation
Digester not working?
Moderators:
kris
Posted: 04/12/09 05:27:13 Modified: 04/12/09 05:28:31I'm not sure if I'm doing something wrong here, or what, but io.stream.Digester isn't working for me (DMD 1.041 / Tango 0.99.8):
// File: main.d module digester; import tango.io.Stdout; import tango.io.device.File; import tango.io.digest.Crc32; import tango.io.digest.Digest; import tango.io.stream.Buffered; import tango.io.stream.Data; import tango.io.stream.Digester; import tango.math.Math; char[] digestOfFile_Digester(Digest digest, char[] path) { auto file = new File(path); scope(exit) file.close(); auto fileBuffer = new BufferedInput(file.input); auto fileDigester = new DigestInput(fileBuffer, digest); auto fileInput = new DataInput(fileDigester); while(true) { auto actualPosition = file.position() - fileBuffer.readable(); auto amountToRead = min(file.bufferSize(), file.length() - actualPosition); if(amountToRead > 0) fileInput.load(amountToRead); if(amountToRead < file.length()) break; } return fileDigester.digest.hexDigest(); } char[] digestOfFile_Manual(Digest digest, char[] path) { auto file = new File(path); scope(exit) file.close(); auto fileBuffer = new BufferedInput(file.input); auto fileInput = new DataInput(fileBuffer); while(true) { auto actualPosition = file.position() - fileBuffer.readable(); uint amountToRead = min(file.bufferSize(), cast(uint)(file.length()-actualPosition)); if(amountToRead > 0) { auto data = fileInput.load(amountToRead); digest.update(data); } if(amountToRead < file.length()) break; } return digest.hexDigest(); } void main(char[][] args) { auto checksum1 = digestOfFile_Digester (new Crc32(), "main.d"); auto checksum2 = digestOfFile_Manual (new Crc32(), "main.d"); Stdout.formatln("Digester: {}", checksum1); Stdout.formatln("Manual: {}", checksum2); }Output for me:
Digester: 00000000 Manual: ec5628cfObviously that "Manual" checksum is going to change with tiny changes in the source, but on the "Digester" one I'm getting "00000000" no matter what.