Skip to content

Instantly share code, notes, and snippets.

@firegate666
Created March 1, 2017 12:06
Show Gist options
  • Save firegate666/273fa0e589218d78fa9cf79caedb6ba6 to your computer and use it in GitHub Desktop.
Save firegate666/273fa0e589218d78fa9cf79caedb6ba6 to your computer and use it in GitHub Desktop.
IP anonimizer written in scala for IPv4 and IPv6
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