2021-04-22 02:36:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
|
|
|
|
*
|
|
|
|
* This file is part of Logar, simple tool to manage http log files.
|
|
|
|
*
|
|
|
|
* Logar is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Logar is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with Logar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-22 18:11:27 +02:00
|
|
|
package fr.devinsy.logar.app.anonymizer;
|
2021-04-22 02:36:11 +02:00
|
|
|
|
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
2021-04-26 19:53:28 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2021-04-22 02:36:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2021-05-12 15:04:05 +02:00
|
|
|
* The Class Ipv6Generator.
|
2021-04-22 02:36:11 +02:00
|
|
|
*/
|
|
|
|
public final class Ipv6Generator
|
|
|
|
{
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(Ipv6Generator.class);
|
|
|
|
|
|
|
|
/**
|
2021-05-12 15:04:05 +02:00
|
|
|
* Instantiates a new ipv 6 generator.
|
2021-04-22 02:36:11 +02:00
|
|
|
*/
|
|
|
|
private Ipv6Generator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Random.
|
|
|
|
*
|
|
|
|
* @param ip
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String random(final String ip)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (ip == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StringBuffer buffer = new StringBuffer(ip.length());
|
|
|
|
for (int index = 0; index < ip.length(); index++)
|
|
|
|
{
|
|
|
|
char c = ip.charAt(index);
|
|
|
|
if (c == ':')
|
|
|
|
{
|
|
|
|
buffer.append(c);
|
|
|
|
}
|
2021-04-26 18:49:35 +02:00
|
|
|
else if (index == 0)
|
|
|
|
{
|
|
|
|
buffer.append('b');
|
|
|
|
}
|
|
|
|
else if (index == 1)
|
|
|
|
{
|
|
|
|
buffer.append('a');
|
|
|
|
}
|
|
|
|
else if (index == 2)
|
|
|
|
{
|
|
|
|
buffer.append('d');
|
|
|
|
}
|
2021-04-22 02:36:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer.append(RandomStringUtils.random(1, "0123456789abcdef"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = buffer.toString();
|
|
|
|
|
2021-04-26 19:53:28 +02:00
|
|
|
while (StringUtils.equals(result, ip))
|
2021-04-22 02:36:11 +02:00
|
|
|
{
|
2021-04-26 19:53:28 +02:00
|
|
|
result = random(ip);
|
2021-04-22 02:36:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|