Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@prog/hash.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 function h = hash(inp,meth) | |
2 % HASH - Convert an input variable into a message digest. | |
3 % | |
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
5 % | |
6 % HASH - Convert an input variable into a message digest using any of | |
7 % several common hash algorithms | |
8 % | |
9 % USAGE: h = hash(inp,'meth') | |
10 % | |
11 % inp = input variable, of any of the following classes: | |
12 % char, uint8, logical, double, single, int8, uint8, | |
13 % int16, uint16, int32, uint32, int64, uint64 | |
14 % h = hash digest output, in hexadecimal notation | |
15 % meth = hash algorithm, which is one of the following: | |
16 % MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512 | |
17 % | |
18 % NOTES: (1) If the input is a string or uint8 variable, it is hashed | |
19 % as usual for a byte stream. Other classes are converted into | |
20 % their byte-stream values. In other words, the hash of the | |
21 % following will be identical: | |
22 % 'abc' | |
23 % uint8('abc') | |
24 % char([97 98 99]) | |
25 % The hash of the follwing will be different from the above, | |
26 % because class "double" uses eight byte elements: | |
27 % double('abc') | |
28 % [97 98 99] | |
29 % You can avoid this issue by making sure that your inputs | |
30 % are strings or uint8 arrays. | |
31 % (2) The name of the hash algorithm may be specified in lowercase | |
32 % and/or without the hyphen, if desired. For example, | |
33 % h=hash('my text to hash','sha256'); | |
34 % (3) Carefully tested, but no warranty. Use at your own risk. | |
35 % (4) Michael Kleder, Nov 2005 | |
36 % (5) This is a direct copy of the code from Michael Kleder. Only | |
37 % the function name was changed to bring it in line with the | |
38 % LTPDA naming convention. (M Hewitson 15-09-07) | |
39 % | |
40 % EXAMPLE: | |
41 % | |
42 % algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; | |
43 % for n=1:6 | |
44 % h=hash('my sample text',algs{n}); | |
45 % disp([algs{n} ' (' num2str(length(h)*4) ' bits):']) | |
46 % disp(h) | |
47 % end | |
48 % | |
49 % NOTE: Original taken from MATHWORKS file exchange. | |
50 % Author: Michael Kleder, Nov 2005 | |
51 % http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8944&objectType=file | |
52 % | |
53 % $Id: hash.m,v 1.1 2008/06/18 13:35:11 hewitson Exp $ | |
54 % | |
55 % | |
56 | |
57 inp=inp(:); | |
58 % convert strings and logicals into uint8 format | |
59 if ischar(inp) || islogical(inp) | |
60 inp=uint8(inp); | |
61 else % convert everything else into uint8 format without loss of data | |
62 inp=typecast(inp,'uint8'); | |
63 end | |
64 | |
65 % verify hash method, with some syntactical forgiveness: | |
66 meth=upper(meth); | |
67 switch meth | |
68 case 'SHA1' | |
69 meth='SHA-1'; | |
70 case 'SHA256' | |
71 meth='SHA-256'; | |
72 case 'SHA384' | |
73 meth='SHA-384'; | |
74 case 'SHA512' | |
75 meth='SHA-512'; | |
76 otherwise | |
77 end | |
78 algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; | |
79 if isempty(strmatch(meth,algs,'exact')) | |
80 error(['Hash algorithm must be ' ... | |
81 'MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512']); | |
82 end | |
83 | |
84 % create hash | |
85 x=java.security.MessageDigest.getInstance(meth); | |
86 x.update(inp); | |
87 h=typecast(x.digest,'uint8'); | |
88 h=dec2hex(h)'; | |
89 if(size(h,1))==1 % remote possibility: all hash bytes < 128, so pad: | |
90 h=[repmat('0',[1 size(h,2)]);h]; | |
91 end | |
92 h=lower(h(:)'); | |
93 clear x | |
94 return |