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?
Asked
Active
Viewed 518 times
8
9Harris
- 175
- 6
-
1I'm about to write my own Luhn algorithm implementation, but would prefer to borrow yours. Care to share it? – Stephen Wilkus Sep 26 '21 at 04:13
-
I don't suggest using any thing I write without vetting that it's up to your standards first. But it's top notch compared to the majority of the stuff I write! lol. How do I share it? It's too long for the character length there. – 9Harris Sep 27 '21 at 22:33
-
Sawilkus@gmail.com will work. – Stephen Wilkus Oct 10 '21 at 23:30
-
@StephenWilkus. Just saw your comment. Check your email. – 9Harris Mar 13 '22 at 19:32
1 Answers
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
-
1Daniel, do you need to use
Unprotecthere, or could you do something withTagSetto add a hash type? – CA Trevillian Jan 19 '20 at 20:40 -
1@CATrevillian It can be done with
TagSetDelayedif the second arg is a symbol rather than a string. – Daniel Lichtblau Jan 19 '20 at 22:03