Wednesday 13 June 2018

What is the default hash code that Mathematica uses?



The online documentation says



Hash[expr] 
gives an integer hash code for the expression expr.
Hash[expr,"type"]
gives an integer hash code of the specified type for expr.


It also gives "possible hash code types":




  • "Adler32" Adler 32-bit cyclic redundancy check

  • "CRC32" 32-bit cyclic redundancy check

  • "MD2" 128-bit MD2 code

  • "MD5" 128-bit MD5 code

  • "SHA" 160-bit SHA-1 code

  • "SHA256" 256-bit SHA code

  • "SHA384" 384-bit SHA code

  • "SHA512" 512-bit SHA code



Yet none of these correspond to the default returned by Hash[expr].



So my questions are:




  • What method does the default Hash use?

  • Are there any other hash codes built in?


Answer



The default hash algorithm is, more-or-less, a basic 32-bit hash function applied to the underlying expression representation, but the exact code is a proprietary component of the Mathematica kernel. It's subject to (and has) change between Mathematica versions, and lacks a number of desirable cryptographic properties, so I personally recommend you use MD5 or one of the SHA variants for any serious application where security matters. The built-in hash is intended for typical data structure use (e.g. in a hash table).



The named hash algorithms you list from the documentation are the only ones currently available. Are you looking for a different one in particular?


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...