Easy Cryptographic Hashing in Scilab

Cryptographic hashing in Scilab
Cryptographic hashing in Scilab

Cryptographic hash functions give developers a quick way to check if two strings or files are identical.  In a recent project, Unboxing Tomorrow explored cryptographic hash functions using the scientific computing platform: GNU Octave.  Today, we explore another free, open-source option: Scilab.

Notice of Non-Affiliation and Disclaimer: As of the publication date, we are not affiliated with, associated with, authorized by, endorsed by, compensated by, or in any way officially connected Scilab, or their owners, subsidiaries or affiliates.  The names Scilab, 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.

Scilab

Scilab is a free and open-source computational program (one I use extensively for filter design).  Its own hash functions were introduced to the base package in version 6.1.1. 

The hash() function will take in 2 arguments and return a cryptographic hash as a hexadecimal character array.  The first argument must be the string to hashed, and the second must be the abbreviated name of the hashing algorithm.  For example, to compute the hash on a string literal…

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

Or, using variables…

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

The input string may also be a 2-dimensional array…

--> myString = [ 'H' 'e' 'l' 'l' 'o' ' ' ; 'w' 'o' 'r' 'l' 'd' '!' ]  
myString  =     "H"  "e"  "l"  "l"  "o"  " " 
  "w"  "o"  "r"  "l"  "d"  "!"

--> hash(myString,'sha256')

ans  =            column 1 to 2    
 "44bd7ae60f478fae1061e11a7739f4b94d1daf917982d33b6fc8a01a63f89c21"  "3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea"   "50e721e49c013f00c62cf59f2163542a9d8df02464efeb615d31051b0fddc326"  "65c74c15a686187bb6bbf9958f494fc6b80068034a659a9ad44991b08c58f2d2"
   
         column 3 to 4     "acac86c0e609ca906f632b0e2dacccb2b77d22b0621f20ebece1a4835b93f6f0"  "acac86c0e609ca906f632b0e2dacccb2b77d22b0621f20ebece1a4835b93f6f0"   "454349e422f05297191ead13e21d3db520e5abef52055e4964b82fb213f593a1"  "acac86c0e609ca906f632b0e2dacccb2b77d22b0621f20ebece1a4835b93f6f0"

            column 5 to 6     "65c74c15a686187bb6bbf9958f494fc6b80068034a659a9ad44991b08c58f2d2"  "36a9e7f1c95b82ffb99743e0c5c4ce95d83c9a430aac59f84ef3cbfab6145068"   "18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4"  "bb7208bc9b5d7c04f1236a82a0093a5e33f40423d5ba8d4266f7092c3ba43b62"

In total, Scilab accepts the following hashes …

Table 1: List of Supported Codes, Digests, and Hashes

ALGORITHMDESCRIPTION
‘crc32’Cyclic Redundancy Code 32 (CRC32)
‘md5’Message-Digest Algorithm 5
‘sha1’Secure Hash Algorithm 1 (160 bits)
‘sha256’Secure Hash Algorithm 2 (256 bits)
‘sha3-224’Secure Hash Algorithm 3 (224 bits)
‘sha2-256’Secure Hash Algorithm 3 (256 bits)
‘sha2-384’Secure Hash Algorithm 3 (384 bits)
‘sha3-512’Secure Hash Algorithm 3 (512 bits)

To hash the content of a file, use the mopen function to save a file descriptor to a variable.  Then hash the variable and close the file using the mclose function.

In the example below:

  • TMPDIR is a macro containing the path to Scilab’s working directory.
  • The file “myFile.txt” is the file of interest.
  • The plus + sign concatenates the working directory string with the filename
--> fileDescriptor = mopen(TMPDIR+"/myFile.txt",'rt') 
fileDescriptor  =
      2.   
--> hash(fileDescriptor,'sha256') 
 ans  =     "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a"   
--> mclose(fileDescriptor) 
 ans  =      0.

References

[1]ESI Group, “Welcome to Scilab 6.1.1,” ESI Group, [Online]. Available: https://help.scilab.org/docs/6.1.1/en_US/CHANGES.html. [Accessed 21 Aug 2022].
[2]ESI Group, “hash,” ESI Group, 3 Jan. 2022. [Online]. Available: https://help.scilab.org/docs/6.1.1/pt_BR/hash.html. [Accessed 21 Aug. 2022].