Преобразуйте строку md5 с 32 символами в целое число

В процессе обнаружения "домена" Вы формируете общий язык, это и разработчики и все другие заинтересованные стороны в проекте понимают.

модель предметной области и ее "малопонятный жаргон" довольно заметны в исходном коде для готового изделия. Это - по крайней мере, мой опыт

15
задан ensnare 22 November 2009 в 20:27
поделиться

5 ответов

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)
27
ответ дан 1 December 2019 в 01:30
поделиться

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.

3
ответ дан 1 December 2019 в 01:30
поделиться

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.

2
ответ дан 1 December 2019 в 01:30
поделиться

Если все, что вам нужно, это модуль, вам на самом деле не нужно преобразовывать его в 128-байтовое целое число. Вы можете переходить по цифрам или байтам, например так:

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;
}
2
ответ дан 1 December 2019 в 01:30
поделиться

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.

1
ответ дан 1 December 2019 в 01:30
поделиться
Другие вопросы по тегам:

Похожие вопросы: