Algorithm for unique KEY generation
i need to generate unique values by using ipv4 and ipv6 addresses i.e if i input 192.37.4.60; a unique key should be genera开发者_JAVA技巧ted and when i enter 192.60.4.37 a another key should be generated. How can i do this can any one help me out and i can also input ipv6 address also how to generate unique values to each input. can any one propse any algorithm or any present algorithm
Convert the IP into its numerical (decimal) representation:
10.0.0.1 -> 00001010 00000000 00000000 00000001 -> 167772161
This is how a lot of IP addresses are stored internally. It's nice because it only requires 32 bits of storage. You can do this for IPv6 too, but it's going to require something bigger than a uint32.
The IPs are pretty unique :) Especially IPv6 addresses. Also, you can always use a hash algorithm (e.g. MD5, SHA1 etc.) to create a "key". It will be unique, as long as the input data is also unique :)
Output the input IP address: voilà, requirements met!
(If my solution doesn't work for you, it means you need to add more details to your question)
one possible solution can be to use left shift operator and add. For example if a, b, c and d represent the octets then following code will give you a unique value
int a=1;
int b=2;
int c=3;
int d=4;
int value =(a<<24)+(b<<16)+(c<<8)+d;
There are a couple of solution possible depending of what are the needs of your problem.
- You could use the IP address themselves, but keep in mind that an IP address can be spoofed.
- If you intend to use this key amongst multiple peers in order to secure a communication channel, then you might want to take a look towards the symmetric-key or public-key algorithms
- If you only want to use them for some static data you can use either of these : MD5, AES and SHA*.
You might want to look to use multiple source for your algorithm. Consider using, in combination with the MAC address, any other material-related information that you can obtain from the machine/client on which the application will run
You don't state any required properties of the keys excet that thy should be unique, so the obvious solution is to use the canonicalized IP addresses as keys. You can turn the addresses into numbers the obvious way, but be warned that IPv6 addresses make for huge numbers, so you'll need the BigInt implementation in whatever language you use.
(If you didn't actually mean that all 340 undecillion addresses should have unique keys, then of course you should look at normal hash functions instead.)
Another option can be to use the inet_pton directly.
精彩评论