Skip to content

Commit

Permalink
fix broken jdatahash
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Feb 22, 2023
1 parent 69a7d01 commit 29bac9d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions jdatahash.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
% 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)
Expand All @@ -12,14 +13,14 @@
% input:
% data: a string or a numeric array
% algorithm: a string denoting the data hashing algorithm (case
% insensitive); default is 'sha256'; supported options include
% insensitive); default is 'sha-256'; supported options include
%
% for both MATLAB/Octave: 'sha256' (default), 'sha1', 'md5'
% Octave-only: 'md2', 'md4', 'sha224', 'sha384', 'sha512'
% for both MATLAB/Octave: 'sha-256' (default), 'sha-1' ,'sha-384', 'sha-512', 'md2', 'md5'
% Octave-only: 'md4'
%
% examples:
% jdatahash('neurojson')
% key = jdatahash('reusable data', 'md5')
% sha256key = jdatahash('neurojson')
% md5key = jdatahash('reusable data', 'md5')
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
Expand All @@ -37,13 +38,14 @@

opt=varargin2struct(varargin{:});

if(jsonopt(opt))
data = permute(data, ndims(data):-1:1);
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
end

0 comments on commit 29bac9d

Please sign in to comment.