バージョン 1.7.1 (12 KB) 作成者: Jan
MD5 or SHA hash for array, struct, cell or file
ダウンロード: 13.3K
更新 2019/5/19


DATAHASH - Hash for Matlab array, struct, cell or file
Hash = DataHash(Data, Opts, ...)
Data: Array of built-in types (U)INT8/16/32/64, SINGLE, DOUBLE (real or complex)
CHAR, LOGICAL, CELL, STRUCT (scalar or array, nested), function_handle.
Options: List of char vectors:
Hashing method: 'SHA-1', 'SHA-256', 'SHA-384', 'SHA-512', 'MD2', 'MD5'.
Output format: 'hex', 'HEX', 'double', 'uint8', 'base64'
Input type:
'array': The contents, type and size of the input [Data] are
considered for the creation of the hash. Nested CELLs
and STRUCT arrays are parsed recursively. Empty arrays of
different type reply different hashs.
'file': [Data] is treated as file name and the hash is calculated
for the files contents.
'bin': [Data] is a numerical, LOGICAL or CHAR array. Only the
binary contents of the array is considered, such that
e.g. empty arrays of different type reply the same hash.
'ascii': Same as 'bin', but only the 8-bit ASCII part of the 16-bit
Matlab CHARs is considered.
Hash: String or numeric vector.
Default: MD5, hex:
DataHash([]) % 7de5637fd217d0e44e0082f4d79b3e73

SHA-1, Base64:
S.a = uint8([]);
S.b = {{1:10}, struct('q', uint64(415))};
DataHash(S, 'base64', 'SHA-1') % ZMe4eUAp0G9TDrvSW0/Qc0gQ9/A

Comparison with standard hash programs using ASCII strings:
DataHash('abc', 'SHA-256', 'ascii')

Michael Kleder's "Compute Hash" works similar, but does not accept structs, cells or files:

"GetMD5" is 2 to 100 times faster, but it replies only MD5 hashes and a C-compiler is required:

Tested: Matlab 7.7, 7.8, 7.13, 8.6, 9.1, 9.5, Win7&10/64, Java: 1.3, 1.6, 1.7

Bugreports and enhancement requests are welcome. Feel free to ask me about a version for Matlab 6.5.

PS. MD5 and SHA1 hash values are "broken": You can construct a data set, which has a specific hash. But to check the integrity of files or to to recognize a set of variables, both methods are reliable.


Jan (2024). DataHash (, MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R2016b
Windows macOS Linux
Help Center および MATLAB AnswersEnvironment and Settings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
バージョン 公開済み リリース ノート

Bugfix in unit test function: GetMD5 caught correctly now.


Improved handling of strings. Accept inputs as list of char vectors.


Opt struct not case-senisitve. String class accepted as data.

Only the description has been changed. The code was not touched.
No need to use TYPECASTX in modern Matlab versions. Hashes have changed for 'Array' mode to reply the same output as GetMD5. Speed improvements for STRUCT arrays.
Bugfix: Failed for empty files.

Fixed bugs: Strings and empty array for "binary" mode. For "array" mode [] and zeros(1,1,0) had the same hash before.
In the version 30-Mar-2015 the results have been changed, but the help section has not been adjusted. This is fixed now.
Structs arrays are processed much faster now and the checksum differs from earlier versions.

Accept empty input for binary mode now.

Binary mode add to consider only the contents of the data.