Checksum MD5-question

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Checksum MD5-question

Norbert Vischer-2
Hello All,

I want to use checksums in a macro to verify whether two files are the same. I would like to use this for any type of files, not only images.
So far, I managed to write a small macro which calculates the MD5 checksum both on MacOS and Windows.
However, I still have some questions:

a) My macro  works only if the path name contains no spaces, has anyone an advice? The tricks I know did not work.

b) Is there a macro command like "chooseFile", so I can obtain the full path of a file without opening it? In the macro below, I first had to open and then close a raw image to get the full path (which works for any type).

c) I wonder if there is enough demand for a macro command getChecksum(path);


My tests show that if I save an image on Windows or Mac gives different checksums, but once being saved it shows the same checksum on either machine,  and of course independent of the file name. For example, when I save the sample image "Blobs" as tiff I get these results:

Saved on Mac:
============
Result=  MD5(/Users/norbert/Desktop/blobs.tif)= 49e42c00ca2ddf1910b5499bbbf9db8e

Saved on Windows:
================
Result=  MD5 hash of file C:\Users\nor\Desktop\blobs.tif:
0c 6f b4 1d 54 9a 48 86 01 4b cd 48 5f b8 60 ba
CertUtil: -hashfile command completed successfully.

below is the macro,
best regards, Norbert

--------------

macro "Show MD5 Checksum"{
        run("Raw...", " image=8-bit width=100 height=10");
        path = getDirectory("image") + getTitle;
        close;
        print(path);
        if(indexOf(path, " ") >=0)
                print("--- Path contains spaces ---");
        print("Path= ", path);
        if(startsWith(getInfo("os.name"), "Windows"))
                cmd = "certutil -hashfile " +  path + " MD5";
        else
                cmd = "openssl md5 " +  path;
        result = exec(cmd);
        print("Result= ", result);
        selectWindow("Log");
}
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Checksum MD5-question

Curtis Rueden
Hi Norbert,

SciJava Common has some convenience methods for checksums. Here is an
example Groovy script:

  import org.scijava.util.DigestUtils
  string = "Hello world\n"
  bytes = DigestUtils.bytes(string)
  md5 = DigestUtils.md5(bytes)
  println(DigestUtils.hex(md5))

In the Script Editor, this script prints:
f0ef7081e1539ac00ef5b761b4fb01b3

Which matches what you get from md5sum on the command line:

  $ echo "Hello world" | md5sum
  f0ef7081e1539ac00ef5b761b4fb01b3  -

There is a method DigestUtils.bestHex(String) to do it as a one-liner (and
callable from IJ macro), but it uses SHA1 rather than MD5:

  string = "Hello world\n"
  result = call("org.scijava.util.DigestUtils.bestHex", string)
  print(result)

HTH,
Curtis

--
Curtis Rueden
LOCI software architect - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
Have you tried the Image.sc Forum? https://forum.image.sc/



On Wed, Jun 12, 2019 at 2:57 PM Norbert Vischer <[hidden email]>
wrote:

> Hello All,
>
> I want to use checksums in a macro to verify whether two files are the
> same. I would like to use this for any type of files, not only images.
> So far, I managed to write a small macro which calculates the MD5 checksum
> both on MacOS and Windows.
> However, I still have some questions:
>
> a) My macro  works only if the path name contains no spaces, has anyone an
> advice? The tricks I know did not work.
>
> b) Is there a macro command like "chooseFile", so I can obtain the full
> path of a file without opening it? In the macro below, I first had to open
> and then close a raw image to get the full path (which works for any type).
>
> c) I wonder if there is enough demand for a macro command
> getChecksum(path);
>
>
> My tests show that if I save an image on Windows or Mac gives different
> checksums, but once being saved it shows the same checksum on either
> machine,  and of course independent of the file name. For example, when I
> save the sample image "Blobs" as tiff I get these results:
>
> Saved on Mac:
> ============
> Result=  MD5(/Users/norbert/Desktop/blobs.tif)=
> 49e42c00ca2ddf1910b5499bbbf9db8e
>
> Saved on Windows:
> ================
> Result=  MD5 hash of file C:\Users\nor\Desktop\blobs.tif:
> 0c 6f b4 1d 54 9a 48 86 01 4b cd 48 5f b8 60 ba
> CertUtil: -hashfile command completed successfully.
>
> below is the macro,
> best regards, Norbert
>
> --------------
>
> macro "Show MD5 Checksum"{
>         run("Raw...", " image=8-bit width=100 height=10");
>         path = getDirectory("image") + getTitle;
>         close;
>         print(path);
>         if(indexOf(path, " ") >=0)
>                 print("--- Path contains spaces ---");
>         print("Path= ", path);
>         if(startsWith(getInfo("os.name"), "Windows"))
>                 cmd = "certutil -hashfile " +  path + " MD5";
>         else
>                 cmd = "openssl md5 " +  path;
>         result = exec(cmd);
>         print("Result= ", result);
>         selectWindow("Log");
> }
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html