Last active
July 30, 2017 22:13
-
-
Save nlitsme/7c3157e4b9d447ef2ca6fada437c4d65 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
""" | |
Python script for finding a still available pretty phonenumber on the sha2017 POC network. | |
Register your phone number on: https://poc.sha2017.org/ | |
Author: Willem Hengeveld <[email protected]> | |
This code is released under the Beerware license. | |
""" | |
from __future__ import print_function | |
try: | |
from urllib2 import urlopen | |
except ImportError: | |
from urllib.request import urlopen | |
import xml.etree.ElementTree as etree | |
def download_phonebook(): | |
""" download the phonebook in xml format """ | |
response = urlopen('https://poc.sha2017.org/phonebook.xml') | |
return response.read() | |
def parse_phonebook(xmldata): | |
""" convert the xml data to a dictionary of { extension => name } """ | |
pb = dict() | |
tree = etree.fromstring(xmldata) | |
for ent in tree.find('entries'): | |
ext = ent.find('extension') | |
name = ent.find('name') | |
pb[ext.text] = name.text | |
return pb | |
def genpretty2(i, j): | |
""" yield all 'pretty' numbers with 2 distinct digits """ | |
yield "%d%d%d%d" % (i,j,j,j) | |
yield "%d%d%d%d" % (j,i,j,j) | |
yield "%d%d%d%d" % (j,j,i,j) | |
yield "%d%d%d%d" % (j,j,j,i) | |
yield "%d%d%d%d" % (i,j,i,j) | |
yield "%d%d%d%d" % (i,i,j,j) | |
yield "%d%d%d%d" % (i,j,j,i) | |
def genpretty1(i): | |
""" yield all 'pretty' numbers with 1 digit """ | |
yield "%d%d%d%d" % (i,i,i,i) | |
yield "%d%d%d%d" % (i,(i+1)%10,(i+2)%10,(i+3)%10) | |
yield "%d%d%d%d" % (i,(i-1)%10,(i-2)%10,(i-3)%10) | |
def genpretty(i, j): | |
if i==j: | |
for nr in genpretty1(i): | |
yield nr | |
else: | |
for nr in genpretty2(i, j): | |
yield nr | |
def findpretty(pb): | |
""" return a set of pretty numbers which are not yet in the phonebook """ | |
found = set() | |
for i in range(10): | |
for j in range(10): | |
for nr in genpretty(i,j): | |
if nr >= "2000" and nr <= "9999" and not nr in pb: | |
found.add(nr) | |
return found | |
def main(): | |
pb = parse_phonebook(download_phonebook()) | |
found = findpretty(pb) | |
print("pretty phonenumbers still available on the SHA2017 POC network") | |
print(" ".join(found)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running code is available here