Home > vbmeg > functions > common > utility > vb_CalcMD5.m

vb_CalcMD5   Linux PC, Linux AMD Opteron, Windows, Windows 32, Windows 64

PURPOSE ^

128 bit MD5 checksum: file, string, byte stream [MEX]

SYNOPSIS ^

function MD5 = CalcMD5(Data, InClass, OutClass)

DESCRIPTION ^

 128 bit MD5 checksum: file, string, byte stream [MEX]
 This function calculates a 128 bit checksum for arrays and files.
 Digest = CalcMD5(Data, [InClass], [OutClass])
 INPUT:
   Data:   Data array or file name. Either numerical or CHAR array.
           Currently only files and arrays with up to 2^32 bytes (2.1GB) are
           accepted.
   InClass: String to declare the type of the 1st input.
           Optional. Default: 'Char'.
           'File': [Data] is a file name as string. The digest is calculated
                   for this file.
           'Char': [Data] is a char array to calculate the digest for. Only the
                   ASCII part of the Matlab CHARs is used, such that the digest
                   is the same as if the array is written to a file as UCHAR,
                   e.g. with FWRITE.
           'Unicode': All bytes of the input [Data] are used to calculate the
                   digest. This is the standard for numerical input.
   OutClass: String, format of the output. Just the first character matters.
           Optional, default: 'hex'.
           'hex': [1 x 32] string as lowercase hexadecimal number.
           'HEX': [1 x 32] string as uppercase hexadecimal number.
           'Dec': [1 x 16] double vector with UINT8 values.
           'Base64': [1 x 22] string, encoded to base 64 (A:Z,a:z,0:9,+,/).

 OUTPUT:
   Digest: A 128 bit number is replied in a format depending on [OutClass].
           The chance, that different data sets have the same MD5 sum is about
           2^128 (> 3.4 * 10^38). Therefore MD5 can be used as "finger-print"
           of a file rather than e.g. CRC32.

 EXAMPLES:
   Three methods to get the MD5 of a file:
   1. Direct file access (recommended):
     MD5 = CalcMD5(which('CalcMD5.m'), 'File')
   2. Import the file to a CHAR array (binary mode for exact line breaks!):
     FID = fopen(which('CalcMD5.m'), 'rb');
     S   = fread(FID, inf, 'uchar=>char');
     fclose(FID);
     MD5 = CalcMD5(S, 'char')
   3. Import file as a byte stream:
     FID = fopen(which('CalcMD5.m'), 'rb');
     S   = fread(FID, inf, 'uint8=>uint8');
     fclose(FID);
     MD5 = CalcMD5(S, 'unicode');  // 'unicode' can be omitted here

   Test string:
     CalcMD5(char(0:511), 'char', 'HEX')
       => F5C8E3C31C044BAE0E65569560B54332
     CalcMD5(char(0:511), 'unicode', 'HEX')
       => 3484769D4F7EBB88BBE942BB924834CD

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function MD5 = CalcMD5(Data, InClass, OutClass)
0002 % 128 bit MD5 checksum: file, string, byte stream [MEX]
0003 % This function calculates a 128 bit checksum for arrays and files.
0004 % Digest = CalcMD5(Data, [InClass], [OutClass])
0005 % INPUT:
0006 %   Data:   Data array or file name. Either numerical or CHAR array.
0007 %           Currently only files and arrays with up to 2^32 bytes (2.1GB) are
0008 %           accepted.
0009 %   InClass: String to declare the type of the 1st input.
0010 %           Optional. Default: 'Char'.
0011 %           'File': [Data] is a file name as string. The digest is calculated
0012 %                   for this file.
0013 %           'Char': [Data] is a char array to calculate the digest for. Only the
0014 %                   ASCII part of the Matlab CHARs is used, such that the digest
0015 %                   is the same as if the array is written to a file as UCHAR,
0016 %                   e.g. with FWRITE.
0017 %           'Unicode': All bytes of the input [Data] are used to calculate the
0018 %                   digest. This is the standard for numerical input.
0019 %   OutClass: String, format of the output. Just the first character matters.
0020 %           Optional, default: 'hex'.
0021 %           'hex': [1 x 32] string as lowercase hexadecimal number.
0022 %           'HEX': [1 x 32] string as uppercase hexadecimal number.
0023 %           'Dec': [1 x 16] double vector with UINT8 values.
0024 %           'Base64': [1 x 22] string, encoded to base 64 (A:Z,a:z,0:9,+,/).
0025 %
0026 % OUTPUT:
0027 %   Digest: A 128 bit number is replied in a format depending on [OutClass].
0028 %           The chance, that different data sets have the same MD5 sum is about
0029 %           2^128 (> 3.4 * 10^38). Therefore MD5 can be used as "finger-print"
0030 %           of a file rather than e.g. CRC32.
0031 %
0032 % EXAMPLES:
0033 %   Three methods to get the MD5 of a file:
0034 %   1. Direct file access (recommended):
0035 %     MD5 = CalcMD5(which('CalcMD5.m'), 'File')
0036 %   2. Import the file to a CHAR array (binary mode for exact line breaks!):
0037 %     FID = fopen(which('CalcMD5.m'), 'rb');
0038 %     S   = fread(FID, inf, 'uchar=>char');
0039 %     fclose(FID);
0040 %     MD5 = CalcMD5(S, 'char')
0041 %   3. Import file as a byte stream:
0042 %     FID = fopen(which('CalcMD5.m'), 'rb');
0043 %     S   = fread(FID, inf, 'uint8=>uint8');
0044 %     fclose(FID);
0045 %     MD5 = CalcMD5(S, 'unicode');  // 'unicode' can be omitted here
0046 %
0047 %   Test string:
0048 %     CalcMD5(char(0:511), 'char', 'HEX')
0049 %       => F5C8E3C31C044BAE0E65569560B54332
0050 %     CalcMD5(char(0:511), 'unicode', 'HEX')
0051 %       => 3484769D4F7EBB88BBE942BB924834CD
0052 
0053 % Tested: Matlab 6.5, 7.7, 7.8, WinXP, [UnitTest]
0054 % Author: Jan Simon, Heidelberg, (C) 2009-2010 J@n-Simon.De
0055 % License: This program is derived from the RSA Data Security, Inc.
0056 %          MD5 Message Digest Algorithm, RFC 1321, R. Rivest, April 1992
0057 %
0058 % See also CalcCRC32.
0059 % Michael Kleder has published a Java call to compute the MD5 (and further
0060 %
0061 % $JRev: R5.00j V:015 Sum:zh2gTrvHwbd7 Date:17-Dec-2009 02:46:53 $
0062 % $File: CalcMD5\CalcMD5.m $
0063 % History:
0064 % 015: 15-Dec-2009 16:53, BUGFIX: UINT32 has 32 bits on 64 bit systems now.
0065 %      Thanks to Sebastiaan Breedveld!
0066 %
0067 % Copyright (c) 2009, Jan Simon
0068 % All rights reserved.
0069 % Redistribution and use in source and binary forms, with or without
0070 % modification, are permitted provided that the following conditions are
0071 % met:
0072 %
0073 %    * Redistributions of source code must retain the above copyright
0074 %      notice, this list of conditions and the following disclaimer.
0075 %    * Redistributions in binary form must reproduce the above copyright
0076 %      notice, this list of conditions and the following disclaimer in
0077 %      the documentation and/or other materials provided with the distribution
0078 %
0079 % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0080 % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0081 % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0082 % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0083 % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0084 % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0085 % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0086 % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0087 % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0088 % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0089 % POSSIBILITY OF SUCH DAMAGE.
0090 %
0091 % If the current Matlab path is the parent folder of this script, the
0092 % MEX function is not found - change the current directory!
0093 %error(['JSim:', mfilename, ':NoMex'], 'Cannot find MEX script.');
0094

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005