Home > manopt > tools > hashmd5.m

hashmd5

PURPOSE ^

Computes the MD5 hash of input data.

SYNOPSIS ^

function h = hashmd5(inp)

DESCRIPTION ^

 Computes the MD5 hash of input data.

 function h = hashmd5(inp)
 
 Returns a string containing the MD5 hash of the input variable. The input
 variable may be of any class that can be typecast to uint8 format, which
 is fairly non-restrictive.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function h = hashmd5(inp)
0002 % Computes the MD5 hash of input data.
0003 %
0004 % function h = hashmd5(inp)
0005 %
0006 % Returns a string containing the MD5 hash of the input variable. The input
0007 % variable may be of any class that can be typecast to uint8 format, which
0008 % is fairly non-restrictive.
0009 
0010 % This file is part of Manopt: www.manopt.org.
0011 % This code is a stripped version of more general hashing code by
0012 % Michael Kleder, Nov 2005.
0013 % Change log:
0014 %
0015 %   Aug. 8, 2013 (NB):
0016 %       Made x a static (persistent) variable, in the hope it will speed
0017 %       it up. Furthermore, the function is now Octave compatible.
0018 
0019     is_octave = exist('OCTAVE_VERSION', 'builtin');
0020         
0021     persistent x;
0022     if isempty(x) && ~is_octave
0023         x = java.security.MessageDigest.getInstance('MD5');
0024     end
0025 
0026     inp=inp(:);
0027     % Convert strings and logicals into uint8 format
0028     if ischar(inp) || islogical(inp)
0029         inp=uint8(inp);
0030     else % Convert everything else into uint8 format without loss of data
0031         inp=typecast(inp,'uint8');
0032     end
0033     
0034     % Create hash
0035     if ~is_octave
0036         x.update(inp);
0037         h = typecast(x.digest, 'uint8');
0038         h = dec2hex(h)';
0039         % Remote possibility: all hash bytes < 128, so pad:
0040         if(size(h,1))==1
0041             h = [repmat('0',[1 size(h,2)]);h];
0042         end
0043         h = lower(h(:)');
0044     else
0045         h = md5sum(char(inp'), true);
0046     end
0047     
0048 end

Generated on Sat 12-Nov-2016 14:11:22 by m2html © 2005