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