What's the most efficient way to convert an md5 hash to a unique integer to perform a modulus operation?
相关问题
- facebook error invalid key hash for some devices
- Change first key of multi-dimensional Hash in perl
- Bool.hashValue valid to convert to Int?
- Is the c++ hash function reasonably safe for passw
- Reliably reproduce in C# a legacy password hashing
相关文章
- Bcrypt vs Hash in laravel
- What is the fastest way to map group names of nump
- Finding out whether there exist two identical subs
- Oracle STANDARD_HASH not available in PLSQL?
- Looking for a fast hash-function
- Python: Is there any reason *not* to cache an obje
- C# how to calculate hashcode from an object refere
- How Docker calculates the hash of each layer? Is i
You'll need to define your own hash function that converts an MD5 string into an integer of the desired width. If you want to interpret the MD5 hash as a plain string, you can try the FNV algorithm. It's pretty quick and fairly evenly distributed.
If all you need is modulus, you don't actually need to convert it to 128-byte integer. You can go digit by digit or byte by byte, like this.
Since the solution language was not specified, Python is used for this example.
You haven't said what platform you're running on, or what the format of this hash is. Presumably it's hex, so you've got 16 bytes of information.
In order to convert that to a unique integer, you basically need a 16-byte (128-bit) integer type. Many platforms don't have such a type available natively, but you could use two
long
values in C# or Java, or aBigInteger
in Java or .NET 4.0.Conceptually you need to parse the hex string to bytes, and then convert the bytes into an integer (or two). The most efficient way of doing that will entirely depend on which platform you're using.
There is more data in a MD5 than will fit in even a 64b integer, so there's no way (without knowing what platform you are using) to get a unique integer. You can get a somewhat unique one by converting the hex version to several integers worth of data then combining them (addition or multiplication). How exactly you would go about that depends on what language you are using though.
Alot of language's will implement either an
unpack
orsscanf
function, which are good places to start looking.