# Toy cryptographic hash function for education purposes?

I'm teaching some high school students about number theory and cryptography, and I'd like a hash function (or ideally, a family of hash functions) that I can use as simple demonstration for cryptographic hash functions. The students have learned everything involved in a simple implementation of RSA.

It will be used for inputs that are numbers in decimal format between 8 and 12 digits long.

• Can be worked out fairly quickly and easily with paper and a scientific calculator for decimal inputs of around 8-12 digits
• A short digest (say around 4 digits)
• Digest should change significantly with small modifications to the input
• It should seem difficult (at least on paper) to find an input that produces a given digest
• It should seem difficult to see a way to modify an input without modifying its digest
• It should seem difficult to find two inputs that will have the same digest

Not all of these properties are required but the more it has the better it will be for demonstration purposes. It's okay if it's reversible, so long as it isn't obvious at first glance how to do that.

Any ideas?

## 2 Answers

I'm looking in my algorithms textbook now for information on hash functions. For positive integers $n$, it says that the most common hash function is to simply take $n\pmod{p}$ for some prime $p$. This doesn't really fit the requirement of "large changes in output for small changes in input."

However, things get a bit more interesting if you don't use the hash function for numbers, but rather for text. For example, my textbook (by Sedgewick and Wayne) says an example process to hash a string is to:

hash = 0
for each character c in the string:
hash = (R*hash + c) % p


...Where R is some number that is greater than the numeric value of any character, and p is a prime (e.g. $31$ seems to be used often). You'd need some way of converting characters to numbers, which could be as simple as $A\mapsto 1, \ldots, Z\mapsto 26$

This doesn't fit your exact input/output requirements, but it would be a fairly straightforward example to teach high schoolers.

• I had the thought of taking $n \bmod p$ and $n \bmod q$ for two distinct primes, and concatenating the result. This isn't actually hard to reverse (using the Chinese Remainder Theorem) but it is sufficiently nonobvious that it will probably do if I can't think of doing anything better. – RichN Oct 7 '14 at 4:01
• Actually I wonder if the string example you just gave would work if you used it for the digits of the number... – RichN Oct 7 '14 at 4:03
• @RichN It probably would... The hash function for the string basically converts the string to a base-R number (if I'm understanding it right) and takes the modulus of that with respect to some prime. It could also make for an interesting exercise/at-home project: "using the algorithm from class (which worked with numbers), think up a way to hash strings." – apnorton Oct 7 '14 at 4:05

You can see a good number of hash functions here by Joshua Holden. He was also an Associate Professor in the Mathematics Department of Rose-Hulman Institute of Technology. He has created JSA, JSA-1 and JSA-2 which are second-preimage resistant.