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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
| function Hash = DataHash(Data, varargin)
if nargin == 0 R = Version_L;
if nargout == 0 disp(R); else Hash = R; end
return; end
[Method, OutFormat, isFile, isBin, Data] = ParseInput(Data, varargin{:});
try Engine = java.security.MessageDigest.getInstance(Method);
catch ME if ~usejava('jvm') Error_L('needJava', 'DataHash needs Java.'); end Error_L('BadInput2', 'Invalid hashing algorithm: [%s]. %s', ... Method, ME.message); end
if isFile [FID, Msg] = fopen(Data, 'r'); if FID < 0 Error_L('BadFile', ['Cannot open file: %s', char(10), '%s'], Data, Msg); end
Chunk = 1e6; Count = Chunk; while Count == Chunk [Data, Count] = fread(FID, Chunk, '*uint8'); if Count ~= 0 Engine.update(Data); end end fclose(FID);
elseif isBin if ~isempty(Data) if isnumeric(Data) if isreal(Data) Engine.update(typecast(Data(:), 'uint8')); else Engine.update(typecast(real(Data(:)), 'uint8')); Engine.update(typecast(imag(Data(:)), 'uint8')); end elseif islogical(Data) Engine.update(typecast(uint8(Data(:)), 'uint8')); elseif ischar(Data) Engine.update(typecast(uint16(Data(:)), 'uint8')); elseif myIsString(Data) if isscalar(Data) Engine.update(typecast(uint16(Data{1}), 'uint8')); else Error_L('BadBinData', 'Bin type requires scalar string.'); end else Error_L('BadBinData', 'Data type not handled: %s', class(Data)); end end else Engine = CoreHash(Data, Engine); end
Hash = typecast(Engine.digest, 'uint8');
switch OutFormat case 'hex' Hash = sprintf('%.2x', double(Hash)); case 'HEX' Hash = sprintf('%.2X', double(Hash)); case 'double' Hash = double(reshape(Hash, 1, [])); case 'uint8' Hash = reshape(Hash, 1, []); case 'short' Hash = fBase64_enc(double(Hash), 0); case 'base64' Hash = fBase64_enc(double(Hash), 1);
otherwise Error_L('BadOutFormat', ... '[Opt.Format] must be: HEX, hex, uint8, double, base64.'); end
end
function Engine = CoreHash(Data, Engine)
Engine.update([uint8(class(Data)), ... typecast(uint64([ndims(Data), size(Data)]), 'uint8')]);
if issparse(Data) [S.Index1, S.Index2, S.Value] = find(Data); Engine = CoreHash(S, Engine); elseif isstruct(Data) F = sort(fieldnames(Data)); for iField = 1:length(F) aField = F{iField}; Engine.update(uint8(aField)); for iS = 1:numel(Data) Engine = CoreHash(Data(iS).(aField), Engine); end end elseif iscell(Data) for iS = 1:numel(Data) Engine = CoreHash(Data{iS}, Engine); end elseif isempty(Data) elseif isnumeric(Data) if isreal(Data) Engine.update(typecast(Data(:), 'uint8')); else Engine.update(typecast(real(Data(:)), 'uint8')); Engine.update(typecast(imag(Data(:)), 'uint8')); end elseif islogical(Data) Engine.update(typecast(uint8(Data(:)), 'uint8')); elseif ischar(Data) Engine.update(typecast(uint16(Data(:)), 'uint8')); elseif myIsString(Data) classUint8 = uint8([117, 105, 110, 116, 49, 54]); for iS = 1:numel(Data) aString = uint16(Data{iS}); Engine.update([classUint8, ... typecast(uint64([ndims(aString), size(aString)]), 'uint8')]); if ~isempty(aString) Engine.update(typecast(uint16(aString), 'uint8')); end end
elseif isa(Data, 'function_handle') Engine = CoreHash(ConvertFuncHandle(Data), Engine); elseif (isobject(Data) || isjava(Data)) && ismethod(class(Data), 'hashCode') Engine = CoreHash(char(Data.hashCode), Engine); else try BasicData = ConvertObject(Data); catch ME error(['JSimon:', mfilename, ':BadDataType'], ... '%s: Cannot create elementary array for type: %s\n %s', ... mfilename, class(Data), ME.message); end
try Engine = CoreHash(BasicData, Engine); catch ME if strcmpi(ME.identifier, 'MATLAB:recursionLimit') ME = MException(['JSimon:', mfilename, ':RecursiveType'], ... '%s: Cannot create hash for recursive data type: %s', ... mfilename, class(Data)); end throw(ME); end end
end
function [Method, OutFormat, isFile, isBin, Data] = ParseInput(Data, varargin)
Method = 'MD5'; OutFormat = 'hex'; isFile = false; isBin = false;
nOpt = nargin - 1; Opt = varargin; if nOpt == 1 && isa(Opt{1}, 'struct') Opt = struct2cell(Opt{1}); nOpt = numel(Opt); end
for iOpt = 1:nOpt aOpt = Opt{iOpt}; if ~ischar(aOpt) Error_L('BadInputType', '[Opt] must be a struct or chars.'); end
switch lower(aOpt) case 'file' isFile = true; case {'bin', 'binary'} if (isnumeric(Data) || ischar(Data) || islogical(Data) || ... myIsString(Data)) == 0 || issparse(Data) Error_L('BadDataType', ['[Bin] input needs data type: ', ... 'numeric, CHAR, LOGICAL, STRING.']); end isBin = true; case 'array' isBin = false; case {'asc', 'ascii'} isBin = true; if ischar(Data) Data = uint8(Data); elseif myIsString(Data) && numel(Data) == 1 Data = uint8(char(Data)); else Error_L('BadDataType', ... 'ASCII method: Data must be a CHAR or scalar STRING.'); end case 'hex' if aOpt(1) == 'H' OutFormat = 'HEX'; else OutFormat = 'hex'; end case {'double', 'uint8', 'short', 'base64'} OutFormat = lower(aOpt); otherwise Method = upper(aOpt); end end
end
function FuncKey = ConvertFuncHandle(FuncH)
FuncKey = functions(FuncH);
if ~isempty(FuncKey.file) d = dir(FuncKey.file); if ~isempty(d) FuncKey.filebytes = d.bytes; FuncKey.filedate = d.datenum; end end
end
function DataBin = ConvertObject(DataObj)
try DataBin = uint8(DataObj);
catch WarnS = warning('off', 'MATLAB:structOnObject'); DataBin = struct(DataObj); warning(WarnS); end
end
function Out = fBase64_enc(In, doPad)
B64 = org.apache.commons.codec.binary.Base64; Out = char(B64.encode(In)).'; if ~doPad Out(Out == '=') = []; end
end
function T = myIsString(S)
persistent hasString if isempty(hasString) matlabVer = [100, 1] * sscanf(version, '%d.', 2); hasString = (matlabVer >= 901); end
T = hasString && isstring(S);
end
function R = Version_L()
R.HashVersion = 4; R.Date = [2018, 5, 19];
R.HashMethod = {}; try Provider = java.security.Security.getProviders; for iProvider = 1:numel(Provider) S = char(Provider(iProvider).getServices); Index = strfind(S, 'MessageDigest.'); for iDigest = 1:length(Index) Digest = strtok(S(Index(iDigest):end)); Digest = strrep(Digest, 'MessageDigest.', ''); R.HashMethod = cat(2, R.HashMethod, {Digest}); end end catch ME fprintf(2, '%s\n', ME.message); R.HashMethod = 'error'; end
end
function Error_L(ID, varargin)
error(['JSimon:', mfilename, ':', ID], ['*** %s: ', varargin{1}], ... mfilename, varargin{2:nargin - 1});
end
|