hashgtkvalamd5sum

Vala: Free GChecksum after operation to get a correct MD5 hash


I'm learning Vala and I'm trying to build a Gtk+ GUI which outputs an MD5 hash of a file. Currently I'm using valadoc.org to code my application and I'm using GLib.Checksum to calculate the checksum. GLib.Checksum handle MD5, SHA1, SHA256 and SHA512 (Found in the documentation). So I used the code in the documentation in order to calculate the hash of an input file's path. I found that Vala didn't free the GChecksum automatically and I don't how to do it manually.

Thanks for the help.

This is my Vala code:

private string HashSum( string path, string hash){
    Checksum checksum = new Checksum(ChecksumType.MD5);
    FileStream stream = FileStream.open(path, "rb");
    //var stream = File.new_for_path(path).read();

   uint8 fbuf[100];
   size_t size;

   while ((size = stream.read(fbuf)) > 0){
      checksum.update(fbuf, size);
    }
   unowned string digest = checksum.get_string();
   stdout.printf("%s: %s\n", path, digest);
   
   return @"$hash hash: $digest";
}

Solution

  • I guess the problem was with my Valac version. I upgraded it to the last one i found in my Ubuntu repositories which is: Vala 0.30.0 . And the output now is correct.