Hello everyone,
I wanted to know if there is a possibility to hash any type of data through simulink without the need to use the Cryptography Toolbox in MATLAB.
I have attempted to use the MATLAB Function block in Simulink which seemed like a promising approach. However, I encountered a compatibility error
I am reaching out to this forum to seek guidance and suggestions on how I can successfully hash data in Simulink using other alternative approaches or workarounds, I would greatly appreciate your input.
Thank you in advance for your assistance.

6 Kommentare

Rik
Rik am 11 Jul. 2023
What is your goal with the hash?
Z
Z am 11 Jul. 2023
I have data from sensors that i would like to convert into a fixed-length string of random letters and numbers.
So i need a hash function to do so.
Rik
Rik am 11 Jul. 2023
In that case you might also be interested in my ComputeNonCryptHash function. It is not cryptographically secure, but if all you want is a pseudo-random string of hex numbers, you might want this.
The main reason I wrote this, is to have a way create a checksum on a very wide range of releases of Matlab, as well as GNU Octave.
Z
Z am 11 Jul. 2023
oh thank you, ill give it a look and test it too probably!
Walter Roberson
Walter Roberson am 13 Jul. 2023
@Z flagged (their own) question with the words
Hashing laws in US
Z:
The USA has no laws preventing the discussion of hashing algorithms, or of posting hashing code. US laws specifically permit the use of hashing for authentication, for example.
The USA does have laws that restrict the discussion of encryption algorithms. The distinction between hashing and cryptography is not always clear, but generally speaking, it is cryptography when there is an intention to recover information. When the purpose of an algorithm is to validate that the contents of a message have not been altered, or to provide mapping from longer original data to slots ("hashing"), or to authenticate that the message is from the claimed sender, then none of those functions are (typically) considered encryption.
Is it possible to use "hashing" / "message digest" functions that are not controlled by US law, and end up with "encryption" functionality? Sort of; it is not "supposed to be" possible, but I read a concept paper in which someone proved that in theory you can use authentication processes to effectively carry information (encryption); the particular approach they outlined involved re-sending the data multiple times, so that particular approach was not "efficient", but it did demonstrate that you cannot create a clear boundary between hashing and "encryption".
Z
Z am 13 Jul. 2023
Oh i see your point.
Because my last post got flagged too for this reason:
@Walter Roberson : "Not appropriate for MATLAB Answers - sorry due to the laws of the United States of America, we cannot discuss encryption algorithms here".
But i see the distinction now from your comment.
Thank you.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Pratyush
Pratyush am 7 Jul. 2023

0 Stimmen

Hi ally. It is my understanding that you want to acheive hashing in MATLAB function block of Simulink without using the Cryptography toolbox. Here is how you can achieve it using the MD5 (message-digest algorithm).
import java.security.*;
import java.math.*;
md = MessageDigest.getInstance('MD5');
hash = md.digest(double('myPassword'));
bi = BigInteger(1, hash);
char(bi.toString(16))
ans = 'deb1536f480475f7d593219aa1afd74c'
The above example uses MD5, a one-way cryptography function that accpets a message of any length and returns a fixed length digest value. You may refer the resources below to know more about the MD5 hashing algorithm and the method shown above.

3 Kommentare

Z
Z am 7 Jul. 2023
Hello,
Thank you for you response.
The md5, sha1, sha256 work fine when calling java instances in matlab.
The error is with the Simulink model. When i use MATLAB Function block in my Simulink model
the Matlab code block does not support import statements:
import java.security.*;
import java.math.*;
That is why i had the problem in the first place and even when using the matlab function block to call any hashing algorithm with java.security.MessageDigest.getInstance still same Undefined function or variable error problem:
function hash = fcn(bitSequence)
bitString = num2str(bitSequence, '%d');
bytes = reshape(bitString, 8, []).';
bytes = uint8(bin2dec(bytes));
sha256hasher = java.security.MessageDigest.getInstance('SHA-256');
hash = typecast(sha256hasher.digest(bytes), 'uint8');
end
Im looking for other alternative approaches or workarounds in Simulink, I would greatly appreciate your input.
Thank you in advance for your assistance.
Pratyush
Pratyush am 7 Jul. 2023
Can you try with MATLAB system block in Simulink. It can take a string as an input, internally perform the hashing and return the hashed string as the output. Since the MATLAB system block uses matlab script for execution, it might work.
Here is the documentation for MATLAB System block.
Z
Z am 7 Jul. 2023
Will try it, thank you !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2023a

Gefragt:

Z
Z
am 6 Jul. 2023

Kommentiert:

Z
Z
am 13 Jul. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by