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$
4
  • 1
    $\begingroup$ I'm about to write my own Luhn algorithm implementation, but would prefer to borrow yours. Care to share it? $\endgroup$ Commented Sep 26, 2021 at 4:13
  • $\begingroup$ 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. $\endgroup$
    – 9Harris
    Commented Sep 27, 2021 at 22:33
  • $\begingroup$ [email protected] will work.😁 $\endgroup$ Commented Oct 10, 2021 at 23:30
  • $\begingroup$ @StephenWilkus. Just saw your comment. Check your email. $\endgroup$
    – 9Harris
    Commented Mar 13, 2022 at 19:32

1 Answer 1

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$
3
  • $\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
    Commented Jan 19, 2020 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$ Commented Jan 19, 2020 at 20:40
  • 1
    $\begingroup$ @CATrevillian It can be done with TagSetDelayed if the second arg is a symbol rather than a string. $\endgroup$ Commented Jan 19, 2020 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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