独特的 KEY 生成算法
我需要使用 ipv4 和 ipv6 地址生成唯一值,即如果我输入 192.37.4.60;应该生成一个唯一的密钥,当我输入 192.60.4.37 时,应该生成另一个密钥。我该如何执行此操作,任何人都可以帮助我,我还可以输入 ipv6 地址以及如何为每个输入生成唯一值。任何人都可以提出任何算法或任何现有算法
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 generated 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将 IP 转换为其数字(十进制)表示形式:
10.0.0.1 -> 00001010 00000000 00000000 00000001 --> 167772161
这就是内部存储大量IP地址的方式。这很好,因为它只需要 32 位存储。您也可以对 IPv6 执行此操作,但需要比 uint32 更大的东西。
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.
IP 非常独特:) 尤其是 IPv6 地址。
此外,您始终可以使用哈希算法(例如 MD5、SHA1 等)来创建“密钥”。只要输入数据也是唯一的,它就会是唯一的:)
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 :)
输出输入的IP地址:瞧,满足要求了!
(如果我的解决方案不适合您,则意味着您需要为您的问题添加更多详细信息)
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)
一种可能的解决方案是使用左移运算符并添加。例如,如果 a、b、c 和 d 代表八位字节,则以下代码将为您提供唯一值
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
根据您的问题的需要,有几种可能的解决方案。
您可能希望为您的算法使用多个来源。考虑将您可以从运行应用程序的计算机/客户端获取的任何其他材料相关信息与 MAC 地址结合使用
There are a couple of solution possible depending of what are the needs of your problem.
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
除了密钥应该是唯一的之外,您无需声明密钥的任何必需属性,因此显而易见的解决方案是使用规范化的 IP 地址作为密钥。您可以通过显而易见的方式将地址转换为数字,但请注意 IPv6 地址会产生巨大的数字,因此无论您使用什么语言,都需要 BigInt 实现。
(如果您并不是实际上意味着所有 340 个 undecillion 地址都应该具有唯一的键,那么您当然应该查看普通的哈希函数。)
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.)
另一种选择是使用 inet_pton< /a> 直接。
Another option can be to use the inet_pton directly.