【问题标题】:Convert a string into hash value in matlab在matlab中将字符串转换为哈希值
【发布时间】:2020-06-15 21:38:15
【问题描述】:

如何在 MATLAB 中使用 SHA/MD5 散列将消息转换为散列值?有没有内置函数或固定代码?

【问题讨论】:

    标签: hash matlab-figure


    【解决方案1】:

    matlab 中没有计算哈希的函数。但是,您可以直接从 matlab 调用 Java(任何操作系统)或 .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 的解决方案 https://www.mathworks.com/matlabcentral/answers/45323-how-to-calculate-hash-sum-of-a-string-using-java

    【讨论】:

    • 非常hacky,非常好:D
    • 如果我没记错的话,有两个不同的代码吗?第一个用于 SHA256 散列,第二个用于 SHA1 散列? @casillas
    • 感谢您的解决方案。
    【解决方案2】:

    MATLAB 的 .NET 类似乎是比 JAVA 散列更新的创建。
    但是,这些类没有太多/任何可用的公共文档。玩了一会儿之后,我找到了一种方法,可以根据需要指定几种哈希算法中的一种。

    “System.Security.Cryptography.HashAlgorithm”构造函数接受哈希算法名称(字符串)。根据您传入的字符串名称,它会返回不同的哈希类(.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

    【讨论】:

    • 谢谢!这组织得很好,非常适合帮助我创建一个计算哈希的函数。如果有人正在寻找相同的功能,我将函数上传为a gitHub repo,但您只需要 m 文件。
    【解决方案3】:

    我刚用过,效果很好。

    适用于字符串、文件、不同的数据类型。 对于我通过文件资源管理器与 CRC SHA 进行比较的文件,得到了相同的答案。

    https://www.mathworks.com/matlabcentral/fileexchange/31272-datahash

    【讨论】:

      猜你喜欢
      • 2019-08-26
      • 1970-01-01
      • 2017-06-02
      • 2010-11-05
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多