Alex Rivera | Logout

Best algorithm for hashing number values?

Asked 2009-08-31T22:17:55.690
12

When dealing with a series of numbers, and wanting to use hash results for security reasons, what would be the best way to generate a hash value from a given series of digits? Examples of input would be credit card numbers, or bank account numbers. Preferred output would be a single unsigned integer to assist in matching purposes.

My feeling is that most of the string implementations appear to have low entropy when run against such a short range of characters and because of that, the collision rate might be higher than when run against a larger sample.

The target language is Delphi, however answers from other languages are welcome if they can provide a mathmatical basis which can lead to an optimal solution.

The purpose of this routine will be to determine if a previously received card/account was previously processed or not. The input file could have multiple records against a database of multiple records so performance is a factor.

Edit
Report

2 Answers

1

By definition, a cryptographic hash will work perfectly for your use case. Even if the characters are close, the hash should be nicely distributed.

So I advise you to use any cryptographic hash (SHA-256 for example), with a salt.

answered 2009-08-31T22:22:52.480
1

For a non cryptographic approach you could take a look at the FNV hash it's fast with a low collision rate.

As a very fast alternative, I've also used this algorithm for a few years and had few collision issues however I can't give you a mathematical analysis of it's inherent soundness but for what it's worth here it is

=Edit - My code sample was incorrect - now fixed =

In c/c++

unsigned int Hash(const char *s)
{
    int hash = 0;

    while (*s != 0)
    {
        hash *= 37;
            hash += *s;
        s++;
    }

    return hash;
}

Note that '37' is a magic number, so chosen because it's prime

answered 2009-08-31T22:44:32.407

Your Answer