Easy Cryptographic Hashing in GNU Octave

Sha2 hashing in Octave
Sha2 hashing in Octave

This project will generate cryptographic hashes for two types of inputs.  The first is a simple text string that these programs can accept via their command lines.  The second input is a file, which may be of any type or extension so long as the application can open it.

The application featured here is GNU Octave.  Octave is a free, open-source computing platform with a language not unlike MATLAB.  There are even a few MATLAB to Octave converters available.  The code below has been tested on GNU version 5.1.0.0.

Notice of Non-Affiliation and Disclaimer: As of the publication date, we are not affiliated with, associated with, authorized with, endorsed by, compensated by, or in any way officially connected with GNU Octave Project, MATLAB, or their owners, subsidiaries or affiliates. The names GNU, Octave, MATLAB, as well as related names, marks, emblems, and images are trademarks of their respective owners. External Links: Links to external web pages have been provided as a convenience and for informational purposes only. Unboxing Tomorrow and Voxidyne Media bear no responsibility for the accuracy, legality or content of the external site or for that of subsequent links. Contact the external site for answers to questions regarding its content.

What is a Hashing Function?

As explained by the GNU website: “It is often necessary to find if two strings or files are identical. This might be done by comparing them character by character and looking for differences. However, this can be slow, and so comparing a hash of the string or file can be a rapid way of finding if the files differ.”

Hashing a String

GNU Octave version 5 introduced its “hash()” function, which replaced the now-deprecated md5sum function.  This function accepts 2 strings as inputs.  If it’s successful, hash() will return the hash as a hexadecimal character array. 

  • The first parameter must be the name of the hash (lowercase and with single or double quotes).  The hash directly determines the length of the response.
  • The second parameter must be the string to be hashed.  This can be a string literal, for example:
>> hash('sha256','Hello world!') ans = c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a

…or a class (i.e. variable) of type char…

>> myString = "Hello world!"; >> hash('sha256',myString) ans = c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a

The input can even be a 2-dimensional character array.

>> myArray = [ 'H' 'e' 'l' 'l' 'o' ' ' ; 'w' 'o' 'r' 'l' 'd' '!' ] myArray =   Hello world!   >> hash('sha256',myArray) ans = 2ec5a3f0c2fc3e6dcee0f6f3a5735a6c69d2056579a5452095b75802094043a8

In total, Octave accepts the following hashes and the previously-supported MD5sum digest…

HFUNDESCRIPTION
MD2Message-Digest Algorithm 2 (RFC 1319)
MD4Message-Digest Algorithm 4 (RFC 1320).
MD5Message-Digest Algorithm 5 (RFC 1321)
SHA1Secure Hash Algorithm 1 (RFC 3174)
SHA224Secure Hash Algorithm 2 (224 Bits, RFC 3874)
SHA256Secure Hash Algorithm 2 (256 Bits, RFC 6234)
SHA384Secure Hash Algorithm 2 (384 Bits, RFC 6234)
SHA512Secure Hash Algorithm 2 (512 Bits, RFC 6234)
Table 1: List of Digests and Hashes Supported by GNU Octave

File Content Hashing

To hash the content of a file in Octave’s working directory, replace the string with a fileread() function.  Then pass fileread() the name of the file within quotation marks.

>> hash('sha256',fileread(‘myFile.txt')) ans = c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a

References

[1]GNU Octave Project, “Hashing Functions (GNU Octave (version 6.3.0)),” GNU Octave Project, [Online]. Available: https://docs.octave.org/v6.3.0/Hashing-Functions.html. [Accessed 17 Aug. 2022].
[2]GNU Octave Project, “Hashing Functions (GNU Octave (version 7.2.0)),” GNU Octave Project, [Online]. Available: https://docs.octave.org/v7.2.0/Hashing-Functions.html. [Accessed 17 Aug. 2022].

Important Notice: This article and its contents (the “Information”) belong to Unboxing-tomorrow.com and Voxidyne Media LLC. No license is granted for the use of it other than for information purposes. No license of any intellectual property rights is granted.  The Information is subject to change without notice. The Information supplied is believed to be accurate, but Voxidyne Media LLC assumes no responsibility for its accuracy or completeness, any error in or omission from it or for any use made of it.  Liability for loss or damage resulting from any reliance on the Information or use of it (including liability resulting from negligence or where Voxidyne Media LLC was aware of the possibility of such loss or damage arising) is excluded.