annotate core/tools/hash.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function h = hash(inp,meth)
Daniel@0 2 % HASH - Convert an input variable into a message digest using any of
Daniel@0 3 % several common hash algorithms
Daniel@0 4 %
Daniel@0 5 % USAGE: h = hash(inp,'meth')
Daniel@0 6 %
Daniel@0 7 % inp = input variable, of any of the following classes:
Daniel@0 8 % char, uint8, logical, double, single, int8, uint8,
Daniel@0 9 % int16, uint16, int32, uint32, int64, uint64
Daniel@0 10 % h = hash digest output, in hexadecimal notation
Daniel@0 11 % meth = hash algorithm, which is one of the following:
Daniel@0 12 % MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512
Daniel@0 13 %
Daniel@0 14 % NOTES: (1) If the input is a string or uint8 variable, it is hashed
Daniel@0 15 % as usual for a byte stream. Other classes are converted into
Daniel@0 16 % their byte-stream values. In other words, the hash of the
Daniel@0 17 % following will be identical:
Daniel@0 18 % 'abc'
Daniel@0 19 % uint8('abc')
Daniel@0 20 % char([97 98 99])
Daniel@0 21 % The hash of the follwing will be different from the above,
Daniel@0 22 % because class "double" uses eight byte elements:
Daniel@0 23 % double('abc')
Daniel@0 24 % [97 98 99]
Daniel@0 25 % You can avoid this issue by making sure that your inputs
Daniel@0 26 % are strings or uint8 arrays.
Daniel@0 27 % (2) The name of the hash algorithm may be specified in lowercase
Daniel@0 28 % and/or without the hyphen, if desired. For example,
Daniel@0 29 % h=hash('my text to hash','sha256');
Daniel@0 30 % (3) Carefully tested, but no warranty. Use at your own risk.
Daniel@0 31 % (4) Michael Kleder, Nov 2005
Daniel@0 32 %
Daniel@0 33 % EXAMPLE:
Daniel@0 34 %
Daniel@0 35 % algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'};
Daniel@0 36 % for n=1:6
Daniel@0 37 % h=hash('my sample text',algs{n});
Daniel@0 38 % disp([algs{n} ' (' num2str(length(h)*4) ' bits):'])
Daniel@0 39 % disp(h)
Daniel@0 40 % end
Daniel@0 41
Daniel@0 42 inp=inp(:);
Daniel@0 43 % convert strings and logicals into uint8 format
Daniel@0 44 if ischar(inp) || islogical(inp)
Daniel@0 45 inp=uint8(inp);
Daniel@0 46 else % convert everything else into uint8 format without loss of data
Daniel@0 47 inp=typecast(inp,'uint8');
Daniel@0 48 end
Daniel@0 49
Daniel@0 50 % verify hash method, with some syntactical forgiveness:
Daniel@0 51 meth=upper(meth);
Daniel@0 52 switch meth
Daniel@0 53 case 'SHA1'
Daniel@0 54 meth='SHA-1';
Daniel@0 55 case 'SHA256'
Daniel@0 56 meth='SHA-256';
Daniel@0 57 case 'SHA384'
Daniel@0 58 meth='SHA-384';
Daniel@0 59 case 'SHA512'
Daniel@0 60 meth='SHA-512';
Daniel@0 61 otherwise
Daniel@0 62 end
Daniel@0 63 algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'};
Daniel@0 64 if isempty(strmatch(meth,algs,'exact'))
Daniel@0 65 error(['Hash algorithm must be ' ...
Daniel@0 66 'MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512']);
Daniel@0 67 end
Daniel@0 68
Daniel@0 69 % create hash
Daniel@0 70 x=java.security.MessageDigest.getInstance(meth);
Daniel@0 71 x.update(inp);
Daniel@0 72 h=typecast(x.digest,'uint8');
Daniel@0 73 h=dec2hex(h)';
Daniel@0 74 if(size(h,1))==1 % remote possibility: all hash bytes < 128, so pad:
Daniel@0 75 h=[repmat('0',[1 size(h,2)]);h];
Daniel@0 76 end
Daniel@0 77 h=lower(h(:)');
Daniel@0 78 clear x
Daniel@0 79 return