A hash function is a mathematical function that takes an input value, and produce a smaller value in some particular range. The hash function is considered good if the distribution of values in the smaller range is uniform for the anticipated values in the source space.

The input values will commonly be a representation of a string (a message or a key.)

Hash functions are commonly used in computer programming for efficient searching; by hashing an input value, you get a smaller number, which can be used as an index into a table. The original values are then usually stored as a linked list under each hash entry.

For some more specialized cases of hash functions, see Secure Hash Functions and Perfect Hash Functions.

The most primitive hash function is a simple modulus. However, it fails all the requirements of a secure hash function, and it's not even advisable to use it for a hash table because it doesn't defend well against skews.

But most good cryptographic hashing algorithms (like SHA-1 or MD5) are not particularly complex either, and if they were, that would actually make them less secure. Why? First, security through obscurity is not much security at all. A cryptographically "hard" algorithm usually depends on some kind of trapdoor function, which has been thoroughly examined by mathematicians. If none of them found a way to reverse it, you assume that there is no way. However, the more complex the algorithm is, the more likely it is that it actually has some weaknesses that just hasn't been found yet (or, worse, has only been found by the NSA or similar agencies).

One example of a hash function is that used by the programming language Perl. This hash function is used by Perl (prior to version 5.6) to produce integer indices for associative arrays (aka hash tables) the programmer defines in a Perl program.

Here's an implementation of it in C. Here, the char* pointer str points to an ASCII string which is the textual index given by the programmer which we assume has already been given a value.

/* variable definitions */
unsigned int hash = 0;
unsigned long int len = strlen(str);
char *s = str;

/* make the hash */
while (len--) {
        hash = (hash * 33) + *s;
        s++;
}

Perl then folds the given value such that it is less than the size of the array.

Log in or register to write something here or to contact authors.