LTPDA_HASH - Convert an input variable into a message digest using any of several common hash algorithms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LTPDA_HASH - Convert an input variable into a message digest using any of several common hash algorithms USAGE: h = hash(inp,'meth') inp = input variable, of any of the following classes: char, uint8, logical, double, single, int8, uint8, int16, uint16, int32, uint32, int64, uint64 h = hash digest output, in hexadecimal notation meth = hash algorithm, which is one of the following: MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512 NOTES: (1) If the input is a string or uint8 variable, it is hashed as usual for a byte stream. Other classes are converted into their byte-stream values. In other words, the hash of the following will be identical: 'abc' uint8('abc') char([97 98 99]) The hash of the follwing will be different from the above, because class "double" uses eight byte elements: double('abc') [97 98 99] You can avoid this issue by making sure that your inputs are strings or uint8 arrays. (2) The name of the hash algorithm may be specified in lowercase and/or without the hyphen, if desired. For example, h=hash('my text to hash','sha256'); (3) Carefully tested, but no warranty. Use at your own risk. (4) Michael Kleder, Nov 2005 (5) This is a direct copy of the code from Michael Kleder. Only the function name was changed to bring it in line with the LTPDA naming convention. (M Hewitson 15-09-07) EXAMPLE: algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; for n=1:6 h=hash('my sample text',algs{n}); disp([algs{n} ' (' num2str(length(h)*4) ' bits):']) disp(h) end $Id: ltpda_hash.html,v 1.10 2008/03/31 10:27:31 hewitson Exp $
0001 function h = ltpda_hash(inp,meth) 0002 % LTPDA_HASH - Convert an input variable into a message digest using any of 0003 % several common hash algorithms 0004 % 0005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0006 % 0007 % LTPDA_HASH - Convert an input variable into a message digest using any of 0008 % several common hash algorithms 0009 % 0010 % USAGE: h = hash(inp,'meth') 0011 % 0012 % inp = input variable, of any of the following classes: 0013 % char, uint8, logical, double, single, int8, uint8, 0014 % int16, uint16, int32, uint32, int64, uint64 0015 % h = hash digest output, in hexadecimal notation 0016 % meth = hash algorithm, which is one of the following: 0017 % MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512 0018 % 0019 % NOTES: (1) If the input is a string or uint8 variable, it is hashed 0020 % as usual for a byte stream. Other classes are converted into 0021 % their byte-stream values. In other words, the hash of the 0022 % following will be identical: 0023 % 'abc' 0024 % uint8('abc') 0025 % char([97 98 99]) 0026 % The hash of the follwing will be different from the above, 0027 % because class "double" uses eight byte elements: 0028 % double('abc') 0029 % [97 98 99] 0030 % You can avoid this issue by making sure that your inputs 0031 % are strings or uint8 arrays. 0032 % (2) The name of the hash algorithm may be specified in lowercase 0033 % and/or without the hyphen, if desired. For example, 0034 % h=hash('my text to hash','sha256'); 0035 % (3) Carefully tested, but no warranty. Use at your own risk. 0036 % (4) Michael Kleder, Nov 2005 0037 % (5) This is a direct copy of the code from Michael Kleder. Only 0038 % the function name was changed to bring it in line with the 0039 % LTPDA naming convention. (M Hewitson 15-09-07) 0040 % 0041 % EXAMPLE: 0042 % 0043 % algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; 0044 % for n=1:6 0045 % h=hash('my sample text',algs{n}); 0046 % disp([algs{n} ' (' num2str(length(h)*4) ' bits):']) 0047 % disp(h) 0048 % end 0049 % 0050 % $Id: ltpda_hash.html,v 1.10 2008/03/31 10:27:31 hewitson Exp $ 0051 % 0052 % 0053 0054 inp=inp(:); 0055 % convert strings and logicals into uint8 format 0056 if ischar(inp) || islogical(inp) 0057 inp=uint8(inp); 0058 else % convert everything else into uint8 format without loss of data 0059 inp=typecast(inp,'uint8'); 0060 end 0061 0062 % verify hash method, with some syntactical forgiveness: 0063 meth=upper(meth); 0064 switch meth 0065 case 'SHA1' 0066 meth='SHA-1'; 0067 case 'SHA256' 0068 meth='SHA-256'; 0069 case 'SHA384' 0070 meth='SHA-384'; 0071 case 'SHA512' 0072 meth='SHA-512'; 0073 otherwise 0074 end 0075 algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; 0076 if isempty(strmatch(meth,algs,'exact')) 0077 error(['Hash algorithm must be ' ... 0078 'MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512']); 0079 end 0080 0081 % create hash 0082 x=java.security.MessageDigest.getInstance(meth); 0083 x.update(inp); 0084 h=typecast(x.digest,'uint8'); 0085 h=dec2hex(h)'; 0086 if(size(h,1))==1 % remote possibility: all hash bytes < 128, so pad: 0087 h=[repmat('0',[1 size(h,2)]);h]; 0088 end 0089 h=lower(h(:)'); 0090 clear x 0091 return