Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 32-char md5 string to integer

Tags:

hash

md5

What's the most efficient way to convert an md5 hash to a unique integer to perform a modulus operation?

like image 766
ensnare Avatar asked Nov 22 '09 20:11

ensnare


People also ask

Is MD5 int?

An int is a 32-bit value, while a normal MD5 hash is a 128-bit value. Thus there is no representation in one of the integer data types in C#. ulong has the largest domain with 64 bits. The only "native" data type which transports 128 bits is a Guid.

Can we convert MD5 to string?

You cannot reverse the MD5 function, so your only option is to generate a new password and send that to the user (preferably over some secure channel).

Is MD5 time dependent?

Yes, it's consistent, the md5 algorithm specification defines it regardless of platform.


4 Answers

Since the solution language was not specified, Python is used for this example.

import os
import hashlib

array = os.urandom(1 << 20)
md5 = hashlib.md5()
md5.update(array)
digest = md5.hexdigest()
number = int(digest, 16)

print(number % YOUR_NUMBER)
like image 181
Noctis Skytower Avatar answered Sep 29 '22 11:09

Noctis Skytower


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 a BigInteger 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.

like image 31
Jon Skeet Avatar answered Sep 29 '22 10:09

Jon Skeet


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 or sscanf function, which are good places to start looking.

like image 43
Matthew Scharley Avatar answered Sep 29 '22 11:09

Matthew Scharley


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.

mod=0
for(i=0;i<32;i++)
{
   digit=md5[i]; //I presume you can convert chart to digit yourself.
   mod=(mod*16+digit) % divider;
}
like image 26
yu_sha Avatar answered Sep 29 '22 10:09

yu_sha