简体   繁体   English

PHP 从 IPADDRESS 生成一个唯一的散列

[英]PHP Generate an unique hash from an IPADDRESS

I have a board and i want to disable registrations instead i want the users to be able to post freely with an unique ID based on their IPAddress我有一个板,我想禁用注册而不是我希望用户能够使用基于他们的 IP 地址的唯一 ID 自由发帖

for example:例如:

if "Joe" IP is 200.100.15.117, "Joe" nickname will become the hash function of 200.100.15.117如果“Joe”IP为200.100.15.117,“Joe”昵称将成为200.100.15.117的哈希函数

if "Joe" IP changes he will get another ID, that doesn't matter, i only want one unique ID per IPAddress如果“Joe”IP 更改,他将获得另一个 ID,没关系,我只希望每个 IP 地址有一个唯一 ID

There are also two important things:还有两个重要的事情:

  • unique ID must be 8 characters long唯一 ID 的长度必须为 8 个字符
  • hash should be secure, i don't want hackers be abble to crack my users IPaddresses hash 应该是安全的,我不希望黑客能够破解我的用户 IP 地址

I thought in using an MD5 function and then trim it to 8 chars, but how unique is that?我想过使用 MD5 函数然后将其修剪为 8 个字符,但这有多独特? is there a better way?有没有更好的办法?

You can turn the ip address string to LONG(4 bytes) and do whatever you want next.您可以将 ip 地址字符串转换为 LONG(4 个字节),然后执行任何您想做的事情。

see the php function ip2long .请参阅 php 函数ip2long

The thing with this is that you cant really diferentiate 2 persons on the same network, so if I post on there, and my dorm neighbour goes to the site, the site thinks he is me.这样做的问题是你不能真正区分同一网络上的两个人,所以如果我在那里发帖,而我的宿舍邻居去了这个网站,这个网站就会认为他是我。

You should consider using cookies, these can easily be made unique, and AFAIK, can't be hacked remotely.您应该考虑使用 cookie,这些可以很容易地变得独一无二,并且 AFAIK 不能被远程攻击。

For the unique ID part, if you are storing the "users" in a database, you could just assign the primary key from your "user" table and use that in the cookie.对于唯一 ID 部分,如果您将“用户”存储在数据库中,您可以只分配“用户”表中的主键并在 cookie 中使用它。

If it HAS to be 8 chars long, you could prepend 0's to the ID - eg 00000001, 00000002.... Atleast this way its unique, and 8 chars long-如果它必须是 8 个字符长,您可以在 ID 前面加上 0 - 例如 00000001、00000002 .... 至少这样它是唯一的,8 个字符长 -

EDIT According to OP comment on this answer.编辑根据 OP 对此答案的评论。

Code below uses BC Math library to translate a MD5 hash into a Base-90 string.下面的代码使用 BC Math 库将 MD5 哈希转换为 Base-90 字符串。 This converts a 32 char string into a 20 char one.这会将 32 个字符的字符串转换为 20 个字符的字符串。 This is far from desired 8 chars but it is the minimum string length using ASCII range (it is possible to increase the base to Base-94 by using chars ' " \ [space] but this does not affect to the string length and instead may cause problems while handling data).这远非所需的 8 个字符,但它是使用 ASCII 范围的最小字符串长度(可以使用 chars ' " \ [space]将基数增加到 Base-94,但这不会影响字符串长度,相反可能在处理数据时引起问题)。

$ip      = '200.100.15.117';
$hash    = md5($ip);
$chars16 = array(
    '0' => 0,  '1' => 1,  '2' => 2,  '3' => 3, '4' => 4,  '5' => 5,
    '6' => 6,  '7' => 7,  '8' => 8,  '9' => 9, 'a' => 10, 'b' => 11,
    'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15
);
$base10 = '0';
for ($i = strlen($hash) - 1; $i > 0; $i--) {
    $base10 = bcadd($base10, bcmul($chars16[$hash[$i]], bcpow(16, $i)));
}
$chars   = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,;.:-_+*?!$%&@#~^=/<>[](){}`';
$base    = (string)strlen($chars);
$baseX   = '';
while (bccomp($base10, $base) === 1 || bccomp($base10, $base) === 0) {
    $baseX  = substr($chars, bcmod($base10, $base), 1) . $baseX;
    $base10 = preg_replace('/\.\d*$/', '', bcdiv($base10, $base));
}
$baseX = substr($chars, $base10, 1) . $baseX;
echo $baseX; // Shows: 1BS[JdZf/7J$J{ud&r5i

You can salt the ip address, eg您可以对 ip 地址加盐,例如

$salt = "e45re%$#";
$ip = "200.111.123.111";
$hash = md5($ip.$salt);

$hash = substr($hash,0,8); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM