在matlab中将字符串转换为哈希值
Convert a string into hash value in matlab
如何在 MATLAB 中使用 SHA/MD5 哈希将消息转换为哈希值?有内置函数或固定代码吗?
matlab 中没有计算哈希值的函数。但是,您可以直接从 matlab 调用 Java(任何 OS)或 .Net(仅 Windows)函数,其中任何一个都可以实现您想要的。
请注意,您尚未指定字符串的编码。如果考虑 ASCII、UTF8、UTF16 等格式的字符串,哈希值会有所不同
另请注意,matlab 没有 160 位或 256 位整数,因此哈希显然不能是单个整数。
无论如何,使用 .Net:
SHA256
string = 'some string';
sha256hasher = System.Security.Cryptography.SHA256Managed;
sha256 = uint8(sha256hasher.ComputeHash(uint8(string)));
dec2hex(sha256)
SHA1
sha1hasher = System.Security.Cryptography.SHA1Managed;
sha1= uint8(sha1hasher.ComputeHash(uint8(string)));
dec2hex(sha1)
基于Java的解决方案可以在下面找到link
https://www.mathworks.com/matlabcentral/answers/45323-how-to-calculate-hash-sum-of-a-string-using-java
MATLAB 的 .NET 类 似乎比 JAVA 散列法更新。
但是,这些 类 没有可用的 much/any public 文档。玩了一会儿之后,我找到了一种方法,可以根据需要指定几种哈希算法中的一种。
"System.Security.Cryptography.HashAlgorithm" 构造函数接受哈希算法名称(字符串)。根据您传入的字符串名称,它 returns 不同的散列器 类 (.SHA256Managed 只是一种类型)。请参阅下面的示例以获取完整的字符串输入 ==> 哈希字符串输出生成。
% Available options are 'SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5'
algorithm = 'SHA1';
% SHA1 category
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA1'); % DEFAULT
% SHA2 category
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA256');
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA384');
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA512');
% SHA3 category: Does not appear to be supported
% MD5 category
hasher = System.Security.Cryptography.HashAlgorithm.Create('MD5');
% GENERATING THE HASH:
str = 'Now is the time for all good men to come to the aid of their country';
hash_byte = hasher.ComputeHash( uint8(str) ); % System.Byte class
hash_uint8 = uint8( hash_byte ); % Array of uint8
hash_hex = dec2hex(hash_uint8); % Array of 2-char hex codes
% Generate the hex codes as 1 long series of characters
hashStr = str([]);
nBytes = length(hash_hex);
for k=1:nBytes
hashStr(end+1:end+2) = hash_hex(k,:);
end
fprintf(1, '\n\tThe %s hash is: "%s" [%d bytes]\n\n', algorithm, hashStr, nBytes);
% SIZE OF THE DIFFERENT HASHES:
% SHA1: 20 bytes = 20 hex codes = 40 char hash string
% SHA256: 32 bytes = 32 hex codes = 64 char hash string
% SHA384: 48 bytes = 48 hex codes = 96 char hash string
% SHA512: 64 bytes = 64 hex codes = 128 char hash string
% MD5: 16 bytes = 16 hex codes = 32 char hash string
参考资料:
1) https://en.wikipedia.org/wiki/SHA-1
2) https://defuse.ca/checksums.htm#checksums
我刚用过这个,效果很好。
适用于字符串、文件和不同的数据类型。
对于一个文件,我通过文件资源管理器与 CRC SHA 进行了比较,得到了相同的答案。
https://www.mathworks.com/matlabcentral/fileexchange/31272-datahash
如何在 MATLAB 中使用 SHA/MD5 哈希将消息转换为哈希值?有内置函数或固定代码吗?
matlab 中没有计算哈希值的函数。但是,您可以直接从 matlab 调用 Java(任何 OS)或 .Net(仅 Windows)函数,其中任何一个都可以实现您想要的。
请注意,您尚未指定字符串的编码。如果考虑 ASCII、UTF8、UTF16 等格式的字符串,哈希值会有所不同
另请注意,matlab 没有 160 位或 256 位整数,因此哈希显然不能是单个整数。
无论如何,使用 .Net:
SHA256
string = 'some string';
sha256hasher = System.Security.Cryptography.SHA256Managed;
sha256 = uint8(sha256hasher.ComputeHash(uint8(string)));
dec2hex(sha256)
SHA1
sha1hasher = System.Security.Cryptography.SHA1Managed;
sha1= uint8(sha1hasher.ComputeHash(uint8(string)));
dec2hex(sha1)
基于Java的解决方案可以在下面找到link https://www.mathworks.com/matlabcentral/answers/45323-how-to-calculate-hash-sum-of-a-string-using-java
MATLAB 的 .NET 类 似乎比 JAVA 散列法更新。
但是,这些 类 没有可用的 much/any public 文档。玩了一会儿之后,我找到了一种方法,可以根据需要指定几种哈希算法中的一种。
"System.Security.Cryptography.HashAlgorithm" 构造函数接受哈希算法名称(字符串)。根据您传入的字符串名称,它 returns 不同的散列器 类 (.SHA256Managed 只是一种类型)。请参阅下面的示例以获取完整的字符串输入 ==> 哈希字符串输出生成。
% Available options are 'SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5'
algorithm = 'SHA1';
% SHA1 category
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA1'); % DEFAULT
% SHA2 category
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA256');
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA384');
hasher = System.Security.Cryptography.HashAlgorithm.Create('SHA512');
% SHA3 category: Does not appear to be supported
% MD5 category
hasher = System.Security.Cryptography.HashAlgorithm.Create('MD5');
% GENERATING THE HASH:
str = 'Now is the time for all good men to come to the aid of their country';
hash_byte = hasher.ComputeHash( uint8(str) ); % System.Byte class
hash_uint8 = uint8( hash_byte ); % Array of uint8
hash_hex = dec2hex(hash_uint8); % Array of 2-char hex codes
% Generate the hex codes as 1 long series of characters
hashStr = str([]);
nBytes = length(hash_hex);
for k=1:nBytes
hashStr(end+1:end+2) = hash_hex(k,:);
end
fprintf(1, '\n\tThe %s hash is: "%s" [%d bytes]\n\n', algorithm, hashStr, nBytes);
% SIZE OF THE DIFFERENT HASHES:
% SHA1: 20 bytes = 20 hex codes = 40 char hash string
% SHA256: 32 bytes = 32 hex codes = 64 char hash string
% SHA384: 48 bytes = 48 hex codes = 96 char hash string
% SHA512: 64 bytes = 64 hex codes = 128 char hash string
% MD5: 16 bytes = 16 hex codes = 32 char hash string
参考资料: 1) https://en.wikipedia.org/wiki/SHA-1 2) https://defuse.ca/checksums.htm#checksums
我刚用过这个,效果很好。
适用于字符串、文件和不同的数据类型。 对于一个文件,我通过文件资源管理器与 CRC SHA 进行了比较,得到了相同的答案。
https://www.mathworks.com/matlabcentral/fileexchange/31272-datahash