Best algorithm for hashing number values?

前端 未结 8 1340
后悔当初
后悔当初 2021-02-01 10:24

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?

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 10:24

    I needed to look deeply into hash functions a few months ago. Here are some things I found.

    You want the hash to spread out hits evenly and randomly throughout your entire target space (usually 32 bits, but could be 16 or 64-bits.) You want every character of the input to have and equally large effect on the output.

    ALL the simple hashes (like ELF or PJW) that simply loop through the string and xor in each byte with a shift or a mod will fail that criteria for a simple reason: The last characters added have the most effect.

    But there are some really good algorithms available in Delphi and asm. Here are some references:

    See 1997 Dr. Dobbs article at burtleburtle.net/bob/hash/doobs.html
    code at burtleburtle.net/bob/c/lookup3.c

    SuperFastHash Function c2004-2008 by Paul Hsieh (AKA HsiehHash)
    www.azillionmonkeys.com/qed/hash.html

    You will find Delphi (with optional asm) source code at this reference:
    http://landman-code.blogspot.com/2008/06/superfasthash-from-paul-hsieh.html
    13 July 2008
    "More than a year ago Juhani Suhonen asked for a fast hash to use for his hashtable. I suggested the old but nicely performing elf-hash, but also noted a much better hash function I recently found. It was called SuperFastHash (SFH) and was created by Paul Hsieh to overcome his 'problems' with the hash functions from Bob Jenkins. Juhani asked if somebody could write the SFH function in basm. A few people worked on a basm implementation and posted it."

    The Hashing Saga Continues:
    2007-03-13 Andrew: When Bad Hashing Means Good Caching
    www.team5150.com/~andrew/blog/2007/03/hash_algorithm_attacks.html
    2007-03-29 Andrew: Breaking SuperFastHash
    floodyberry.wordpress.com/2007/03/29/breaking-superfasthash/
    2008-03-03 Austin Appleby: MurmurHash 2.0
    murmurhash.googlepages.com/
    SuperFastHash - 985.335173 mb/sec
    lookup3 - 988.080652 mb/sec
    MurmurHash 2.0 - 2056.885653 mb/sec
    Supplies c++ code MurmurrHash2.cpp and aligned-read-only implementation -
    MurmurHashAligned2.cpp
    //========================================================================
    // Here is Landman's MurmurHash2 in C#
    //2009-02-25 Davy Landman does C# implimentations of SuperFashHash and MurmurHash2
    //landman-code.blogspot.com/search?updated-min=2009-01-01T00%3A00%3A00%2B01%3A00&updated-max=2010-01-01T00%3A00%3A00%2B01%3A00&max-results=2
    //
    //Landman impliments both SuperFastHash and MurmurHash2 4 ways in C#:
    //1: Managed Code 2: Inline Bit Converter 3: Int Hack 4: Unsafe Pointers
    //SuperFastHash 1: 281 2: 780 3: 1204 4: 1308 MB/s
    //MurmurHash2 1: 486 2: 759 3: 1430 4: 2196

    Sorry if the above turns out to look like a mess. I had to just cut&paste it.

    At least one of the references above gives you the option of getting out a 64-bit hash, which would certainly have no collisions in the space of credit card numbers, and could be easily stored in a bigint field in MySQL.

    You do not need a cryptographic hash. They are much more CPU intensive. And the purpose of "cryptographic" is to stop hacking, not to avoid collisions.

提交回复
热议问题