<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create MD5 hash for a file in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820967#M99994</link>
    <description>&lt;P&gt;Or you can use Python's hashlib&lt;/P&gt;
&lt;PRE&gt;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}')&lt;/PRE&gt;
&lt;P&gt;Using the JSL Pick File(), I selected the above code saved in a file md5_file.py&lt;/P&gt;
&lt;P&gt;giving the results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/Users/_me_/Scripts/md5_file.py&lt;/P&gt;
&lt;P&gt;MD5 /Users/_me_/Scripts/md5_file.py: d175790247060d7e415516794660216f&lt;/P&gt;</description>
    <pubDate>Thu, 05 Dec 2024 22:14:40 GMT</pubDate>
    <dc:creator>Paul_Nelson</dc:creator>
    <dc:date>2024-12-05T22:14:40Z</dc:date>
    <item>
      <title>Create MD5 hash for a file</title>
      <link>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820716#M99950</link>
      <description>&lt;P&gt;I am wanting to create a utility to create MD5 hashes for files. &amp;nbsp;The purpose is to have the hash to use later to verify the integrity of the file (that it has not been altered). &amp;nbsp; &amp;nbsp;Here is the basic script I came up with? &amp;nbsp;Is this reasonable and appropriate for the task I want to accomplish?&lt;/P&gt;&lt;PRE&gt;/* 
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 = "&amp;lt;directory path&amp;gt;";
// filename
filename = "&amp;lt;filename&amp;gt;";
//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
*/&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Dec 2024 20:19:55 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820716#M99950</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2024-12-04T20:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create MD5 hash for a file</title>
      <link>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820731#M99952</link>
      <description>&lt;P&gt;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).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For JMP tables there is also "internal" &amp;lt;&amp;lt; Checksum() message&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1733344823764.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/70854i128125D81935913A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1733344823764.png" alt="jthi_0-1733344823764.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;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".&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 20:42:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820731#M99952</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-12-04T20:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create MD5 hash for a file</title>
      <link>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820732#M99953</link>
      <description>&lt;P&gt;thanks&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;, that data table message is new in v18, it appears, and it would work for JMP data tables. &amp;nbsp;My need is more general purpose, that would work for any type of file (.JMP, .JMPADDIN, .JMPXXXXX, .DOCX, .XLSX, .TXT, ...)&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 21:18:06 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820732#M99953</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2024-12-04T21:18:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create MD5 hash for a file</title>
      <link>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820870#M99975</link>
      <description>&lt;P&gt;If&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt;&amp;nbsp;kudo'd this, I must be on the right track! &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 14:47:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820870#M99975</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2024-12-05T14:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create MD5 hash for a file</title>
      <link>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820967#M99994</link>
      <description>&lt;P&gt;Or you can use Python's hashlib&lt;/P&gt;
&lt;PRE&gt;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}')&lt;/PRE&gt;
&lt;P&gt;Using the JSL Pick File(), I selected the above code saved in a file md5_file.py&lt;/P&gt;
&lt;P&gt;giving the results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/Users/_me_/Scripts/md5_file.py&lt;/P&gt;
&lt;P&gt;MD5 /Users/_me_/Scripts/md5_file.py: d175790247060d7e415516794660216f&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 22:14:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-MD5-hash-for-a-file/m-p/820967#M99994</guid>
      <dc:creator>Paul_Nelson</dc:creator>
      <dc:date>2024-12-05T22:14:40Z</dc:date>
    </item>
  </channel>
</rss>

