Created
March 1, 2017 12:06
-
-
Save firegate666/273fa0e589218d78fa9cf79caedb6ba6 to your computer and use it in GitHub Desktop.
IP anonimizer written in scala for IPv4 and IPv6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IP(userIp: String) { | |
def get: String = userIp | |
def getAnonimized: String = { | |
if (userIp.contains(":")) { | |
anonimizeIpV6(userIp) | |
} else if (userIp.contains(".")) { | |
anonimizeIpV4(userIp) | |
} else { | |
userIp | |
} | |
} | |
private def anonimizeIpV6(userIp: String): String = { | |
(userIp.split(':').splitAt(3)._1 :+ "" :+ "").mkString(":") | |
} | |
private def anonimizeIpV4(userIp: String): String = { | |
(userIp.split('.').dropRight(1) :+ "0").mkString(".") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment