wolffd@0: function h = hash(inp,meth) wolffd@0: % HASH - Convert an input variable into a message digest using any of wolffd@0: % several common hash algorithms wolffd@0: % wolffd@0: % USAGE: h = hash(inp,'meth') wolffd@0: % wolffd@0: % inp = input variable, of any of the following classes: wolffd@0: % char, uint8, logical, double, single, int8, uint8, wolffd@0: % int16, uint16, int32, uint32, int64, uint64 wolffd@0: % h = hash digest output, in hexadecimal notation wolffd@0: % meth = hash algorithm, which is one of the following: wolffd@0: % MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512 wolffd@0: % wolffd@0: % NOTES: (1) If the input is a string or uint8 variable, it is hashed wolffd@0: % as usual for a byte stream. Other classes are converted into wolffd@0: % their byte-stream values. In other words, the hash of the wolffd@0: % following will be identical: wolffd@0: % 'abc' wolffd@0: % uint8('abc') wolffd@0: % char([97 98 99]) wolffd@0: % The hash of the follwing will be different from the above, wolffd@0: % because class "double" uses eight byte elements: wolffd@0: % double('abc') wolffd@0: % [97 98 99] wolffd@0: % You can avoid this issue by making sure that your inputs wolffd@0: % are strings or uint8 arrays. wolffd@0: % (2) The name of the hash algorithm may be specified in lowercase wolffd@0: % and/or without the hyphen, if desired. For example, wolffd@0: % h=hash('my text to hash','sha256'); wolffd@0: % (3) Carefully tested, but no warranty. Use at your own risk. wolffd@0: % (4) Michael Kleder, Nov 2005 wolffd@0: % wolffd@0: % EXAMPLE: wolffd@0: % wolffd@0: % algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; wolffd@0: % for n=1:6 wolffd@0: % h=hash('my sample text',algs{n}); wolffd@0: % disp([algs{n} ' (' num2str(length(h)*4) ' bits):']) wolffd@0: % disp(h) wolffd@0: % end wolffd@0: wolffd@0: inp=inp(:); wolffd@0: % convert strings and logicals into uint8 format wolffd@0: if ischar(inp) || islogical(inp) wolffd@0: inp=uint8(inp); wolffd@0: else % convert everything else into uint8 format without loss of data wolffd@0: inp=typecast(inp,'uint8'); wolffd@0: end wolffd@0: wolffd@0: % verify hash method, with some syntactical forgiveness: wolffd@0: meth=upper(meth); wolffd@0: switch meth wolffd@0: case 'SHA1' wolffd@0: meth='SHA-1'; wolffd@0: case 'SHA256' wolffd@0: meth='SHA-256'; wolffd@0: case 'SHA384' wolffd@0: meth='SHA-384'; wolffd@0: case 'SHA512' wolffd@0: meth='SHA-512'; wolffd@0: otherwise wolffd@0: end wolffd@0: algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; wolffd@0: if isempty(strmatch(meth,algs,'exact')) wolffd@0: error(['Hash algorithm must be ' ... wolffd@0: 'MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512']); wolffd@0: end wolffd@0: wolffd@0: % create hash wolffd@0: x=java.security.MessageDigest.getInstance(meth); wolffd@0: x.update(inp); wolffd@0: h=typecast(x.digest,'uint8'); wolffd@0: h=dec2hex(h)'; wolffd@0: if(size(h,1))==1 % remote possibility: all hash bytes < 128, so pad: wolffd@0: h=[repmat('0',[1 size(h,2)]);h]; wolffd@0: end wolffd@0: h=lower(h(:)'); wolffd@0: clear x wolffd@0: return