8
$\begingroup$

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?

$\endgroup$
12
$\begingroup$

Something like this?

Unprotect[Hash];

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

Example:

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

(* Out[575]= 93 *)
$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – 9Harris Jan 19 at 20:29
  • 1
    $\begingroup$ Daniel, do you need to use Unprotect here, or could you do something with TagSet to add a hash type? $\endgroup$ – CA Trevillian Jan 19 at 20:40
  • 1
    $\begingroup$ @CATrevillian It can be done with TagSetDelayed if the second arg is a symbol rather than a string. $\endgroup$ – Daniel Lichtblau Jan 19 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.