Created
January 9, 2018 03:39
-
-
Save breqdev/58769b207f17b58e63eadbf26786dfd0 to your computer and use it in GitHub Desktop.
Convert IPv6 addresses to words so that they'll be more memorable (in theory).
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
import ipaddress | |
import requests | |
words = requests.get( | |
"https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt").text | |
words = words.split("\n")[:-1] | |
for word in range(len(words)): | |
words[word] = words[word].split("\t")[-1] | |
def to_decimal(number, base): | |
return sum([int(character) * base ** index for index,character in enumerate(number[::-1])]) | |
def numberToBase(n, b): | |
if n == 0: | |
return [0] | |
digits = [] | |
while n: | |
digits.append(int(n % b)) | |
n //= b | |
return digits[::-1] | |
def ip_to_words(ip): | |
ip = ipaddress.IPv6Address(ip) | |
ip = int.from_bytes(ip.packed, byteorder="big") | |
digits = numberToBase(ip, len(words)) | |
ipwords = [] | |
for i in digits: | |
ipwords.append(words[i]) | |
return ".".join(ipwords) | |
def words_to_ip(ipwords): | |
ipwords = ipwords.split(".") | |
for ipword in range(len(ipwords)): | |
ipwords[ipword] = words.index(ipwords[ipword]) | |
ip = to_decimal(ipwords, len(words)) | |
ip = ipaddress.IPv6Address(ip) | |
return ip.exploded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment