8

Currently Mathematica offers support of a bunch of different hash algorithms. I would like to add my own to the list. In this way I can still use the function Hash[]. This allows me to switch between one of Mathematica's predefined algorithms, say "Keccak512", and my own algorithm, say "Luhn". This is in a similar vein to how Mathematica lets you define your own Random Number Generator.
1. Is this even possible?
2. If so, can someone provide an example?

9Harris
  • 175
  • 6

1 Answers1

12

Something like this?

Unprotect[Hash];

Hash[str_String, "MyType"] := Mod[Total[ToCharacterCode[str]], 307]

Example:

In[575]:= Hash["3wrt", "MyType"]

(* Out[575]= 93 *)
Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199
  • I forgot you can unprotect Mathematica's functions. Moreover I did not know you could use Unprotect[] to achieve something to this effect. This works for me. I'm sure I'll keep this in mind down the road for creating other solutions as well. Thanks! – 9Harris Jan 19 '20 at 20:29
  • 1
    Daniel, do you need to use Unprotect here, or could you do something with TagSet to add a hash type? – CA Trevillian Jan 19 '20 at 20:40
  • 1
    @CATrevillian It can be done with TagSetDelayed if the second arg is a symbol rather than a string. – Daniel Lichtblau Jan 19 '20 at 22:03