forked from NeuroJSON/jsonlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdatahash.m
51 lines (46 loc) · 1.48 KB
/
jdatahash.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function key = jdatahash(data, algorithm, varargin)
%
% key = jdatahash(data)
% or
% key = jdatahash(data, algorithm)
% key = jdatahash(data, algorithm, 'param1', value1, ...)
%
% computing the hash key for a string or a numeric array (data elements
% are serialized in the row-major order first)
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% data: a string or a numeric array
% algorithm: a string denoting the data hashing algorithm (case
% insensitive); default is 'sha-256'; supported options include
%
% for both MATLAB/Octave: 'sha-256' (default), 'sha-1' ,'sha-384', 'sha-512', 'md2', 'md5'
% Octave-only: 'md4'
%
% examples:
% sha256key = jdatahash('neurojson')
% md5key = jdatahash('reusable data', 'md5')
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%
if (nargin < 2)
algorithm = 'sha-256';
end
if (ischar(data))
data = uint8(data);
end
opt = varargin2struct(varargin{:});
if (jsonopt('rowmajor', 1, opt))
data = permute(data, ndims(data):-1:1);
end
if (isoctavemesh && exist('hash'))
algorithm(algorithm == '-') = [];
key = hash(algorithm, char(typecast(data(:).', 'uint8')));
else
md = java.security.MessageDigest.getInstance(algorithm);
key = sprintf('%2.2x', typecast(md.digest(typecast(data(:), 'uint8')), 'uint8')');
end