J
J
jajabin2020-04-22 16:26:45
Mathematics
jajabin, 2020-04-22 16:26:45

What mathematical method or algorithm should be chosen to form a number from a range?

As I understand that knowledge lies in front of me, but I don’t know where to look.
For example: there is such a route

172.16.100.11:54550 -> 10.0.88.1:445
if you cast the address separately to a uint32 number, then
172.16.100.11 = 2886755339 and 10.0.88.1 = 167794699
and there are more ports, the important question is how to get from two numbers uint32 and two uint16 to the maximum a random number from the range of numbers 0-65535 (uint16) , naturally, that with the RST and FIN flags, the value will be released. Tell me where to look.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-04-22
@jajabin

What you are looking for is called a hash function .
Take the first (or any) two bytes from the result of any hash function on this data.
I hope you understand that the cardinality of your set of address pairs is much larger than 2**16. This means that collisions are inevitable.
Here's how to get this number in a bash one-liner using python:

py '(lambda a, b: 256 * a + b)(*hashlib.md5(b"any bytes for hashing").digest()[:2])'

Or like this, on the command line, you can hash any text into two bytes:
echo 'any text' | py '(lambda a, b: 256 * a + b)(*hashlib.md5(sys.stdin.read().encode("utf-8")).digest()[:2])'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question