cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
MathStatChem
Level VI

Create MD5 hash for a file

I am wanting to create a utility to create MD5 hashes for files.  The purpose is to have the hash to use later to verify the integrity of the file (that it has not been altered).    Here is the basic script I came up with?  Is this reasonable and appropriate for the task I want to accomplish?

/* 
JMP JSL script to open data file as BLOB and calculate MD5 Hash
*/

Names Default To Here( 1 );
// substitute in your directory path and filename as strings below
dirpath = "<directory path>";
// filename
filename = "<filename>";
//load file as text file Blob, and calculate the hexadecimal MD5 Hash
Char To Hex( Blob To Char( Blob MD5( Load Text File( dirpath || filename, blob ) ) ) );
/* 
End script
*/
1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Create MD5 hash for a file

I think I did use something similar when I had some checks like that (I have since then just moved such files to version control and use that to make sure correct files are being used).

 

For JMP tables there is also "internal" << Checksum() message

jthi_0-1733344823764.png

I have tested of using this on my "Preview data table" class and it seemed to work fine. Not sure how robust it will be due to the "internal use only".

-Jarmo

View solution in original post

4 REPLIES 4
jthi
Super User

Re: Create MD5 hash for a file

I think I did use something similar when I had some checks like that (I have since then just moved such files to version control and use that to make sure correct files are being used).

 

For JMP tables there is also "internal" << Checksum() message

jthi_0-1733344823764.png

I have tested of using this on my "Preview data table" class and it seemed to work fine. Not sure how robust it will be due to the "internal use only".

-Jarmo
MathStatChem
Level VI

Re: Create MD5 hash for a file

thanks @jthi , that data table message is new in v18, it appears, and it would work for JMP data tables.  My need is more general purpose, that would work for any type of file (.JMP, .JMPADDIN, .JMPXXXXX, .DOCX, .XLSX, .TXT, ...)

MathStatChem
Level VI

Re: Create MD5 hash for a file

If @Craige_Hales kudo'd this, I must be on the right track!  

Re: Create MD5 hash for a file

Or you can use Python's hashlib

import jmp

import sys, os, hashlib
from pathlib import Path

# Function to open a file and compute it's checksum
def file_cksum(a_filename):
    digest = hashlib.md5()
    try:
        f = open(a_filename, 'rb')               # open in binary mode
        while True:
            t = f.read(8192)
            if len(t) == 0: break;               # end of file
            digest.update(t)

    except:
        print('file_cksum: Unexpected error: ', sys.exc_info()[0])
    else:
        f.close()
        
    return digest.hexdigest()
    
    
jmp.run_jsl('''
local_path = Pick File("Chose File to checksum");
// Pick Directory on windows returns a leading / use Convert File Path()
If( Host is("Windows"),
    local_path = Convert File Path( local_path, windows )
);
Python Send(local_path);
''')
print(local_path)

cksum = file_cksum(local_path)
print(f'MD5 {local_path}: {cksum}')

Using the JSL Pick File(), I selected the above code saved in a file md5_file.py

giving the results:

 

/Users/_me_/Scripts/md5_file.py

MD5 /Users/_me_/Scripts/md5_file.py: d175790247060d7e415516794660216f