Home > io > getMD5Hash.m

getMD5Hash

PURPOSE ^

getMD5Hash

SYNOPSIS ^

function md5Hash=getMD5Hash(inputFile,binEnd)

DESCRIPTION ^

 getMD5Hash
   Calculates MD5 hash for a file

   Input:
   inputFile       string with the path to file for which MD5 hash should
                   be calculated
   binEnd          string that shows the operating system running in the
                   client's computer. Use ".exe" for Windows, ".mac" for
                   macOS or leave it blank for Linux (""). (optional, by
                   default the function automatically detects the client's
                   operating system)

   Output:
   md5Hash         string containing an MD5 hash for inputFile
   
 Usage: md5Hash=getMD5Hash(inputFile,binEnd)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function md5Hash=getMD5Hash(inputFile,binEnd)
0002 % getMD5Hash
0003 %   Calculates MD5 hash for a file
0004 %
0005 %   Input:
0006 %   inputFile       string with the path to file for which MD5 hash should
0007 %                   be calculated
0008 %   binEnd          string that shows the operating system running in the
0009 %                   client's computer. Use ".exe" for Windows, ".mac" for
0010 %                   macOS or leave it blank for Linux (""). (optional, by
0011 %                   default the function automatically detects the client's
0012 %                   operating system)
0013 %
0014 %   Output:
0015 %   md5Hash         string containing an MD5 hash for inputFile
0016 %
0017 % Usage: md5Hash=getMD5Hash(inputFile,binEnd)
0018 inputFile=char(inputFile);
0019 
0020 if nargin<2
0021     if isunix
0022         if ismac
0023             binEnd='.mac';
0024         else
0025             binEnd='';
0026         end
0027     elseif ispc
0028         binEnd='.exe';
0029     else
0030         error('Unknown OS, exiting.')
0031     end
0032 else
0033     binEnd=char(binEnd);
0034 end
0035 
0036 %Check if binEnd is valid
0037 if ~any(strcmp(binEnd,{'.mac','','.exe'}))
0038    error('Unknown OS, exiting.')
0039 end
0040 
0041 %Check file existence
0042 inputFile=checkFileExistence(inputFile);
0043 
0044 %Get string containing an MD5 hash
0045 switch binEnd
0046     case '.mac'
0047         [~, md5HashMessage]=system(['md5 "' inputFile '"']);
0048     case ''
0049         [~, md5HashMessage]=system(['md5sum "' inputFile '"']);
0050     case '.exe'
0051         [~, md5HashMessage]=system(['certutil -hashfile "' inputFile '" MD5"']);
0052 end
0053 
0054 %Extract MD5 hash from a string
0055 md5Hash = char(regexp(md5HashMessage,'[a-f0-9]{32}','match'));
0056 end

Generated by m2html © 2005