How to compute the hash of a string using SHA algorithms

144 ビュー (過去 30 日間)
Buddhini Angelika
Buddhini Angelika 2016 年 1 月 27 日
コメント済み: javad danesh 2021 年 8 月 23 日
Please tell me the set of codes/commands to be typed to compute the hash value of a string using SHA-1 hash and SHA-256 hash in MATLAB R2013a. Will the resultant when the hash is applied be a integer? If not please tell me the way to convert the resultant to an integer as well.
Please tell the answers seperately for SHA-1 hash and SHA-256 hash.
Please help me with the above question.
Thanks a lot in advance.

回答 (2 件)

Guillaume
Guillaume 2016 年 1 月 27 日
There are no functions in matlab to calculate hashes. However, you can call Java (any OS) or .Net (Windows only) functions directly from matlab and either of these implement what you want.
Note that you haven't specified the encoding of the string. The hash is different if you consider the string in ASCII, UTF8, UTF16, etc.
Also note that matlab does not have 160-bit or 256-bit integer, so the hash can't obviously be a single integer.
Anyway, using .Net:
string = 'some string';
sha256hasher = System.Security.Cryptography.SHA256Managed;
sha256 = uint8(sha256hasher.ComputeHash(uint8(string))); %consider the string as 8-bit characters
%display as hex:
dec2hex(sha256)
sha1hasher = System.Security.Cryptography.SHA1Managed;
sha1= uint8(sha1hasher.ComputeHash(uint8(string))); %consider the string as 8-bit characters
%display as hex:
dec2hex(sha1)
If you want to use Java you'll have to figure out that for yourself.
  2 件のコメント
Yumnam Kirani
Yumnam Kirani 2019 年 7 月 4 日
Thank you for the useful solution!
javad danesh
javad danesh 2021 年 8 月 23 日
Hi, what code should we use to input images to see the image hash output?

サインインしてコメントする。


Oliver Woodford
Oliver Woodford 2016 年 5 月 5 日
編集済み: Oliver Woodford 2016 年 5 月 5 日
The Java-based solution can be found here .

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by