Created
October 9, 2018 06:14
-
-
Save erikson1970/3abb6af035d0757cf5d0f50848847d65 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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- (because 0xFF, even : "Yucatán") | |
| import os, re, sys, getopt | |
| _words = [ | |
| ["aardvark", "adroitness"], ["absurd", "adviser"], ["accrue", "aftermath"], ["acme", "aggregate"], ["adrift", "alkali"], ["adult", "almighty"], | |
| ["afflict", "amulet"], ["ahead", "amusement"], ["aimless", "antenna"], ["Algol", "applicant"], ["allow", "Apollo"], ["alone", "armistice"], | |
| ["ammo", "article"], ["ancient", "asteroid"], ["apple", "Atlantic"], ["artist", "atmosphere"], ["assume", "autopsy"], ["Athens", "Babylon"], | |
| ["atlas", "backwater"], ["Aztec", "barbecue"], ["baboon", "belowground"], ["backfield", "bifocals"], ["backward", "bodyguard"], | |
| ["banjo", "bookseller"], ["beaming", "borderline"], ["bedlamp", "bottomless"], ["beehive", "Bradbury"], ["beeswax", "bravado"], | |
| ["befriend", "Brazilian"], ["Belfast", "breakaway"], ["berserk", "Burlington"], ["billiard", "businessman"], ["bison", "butterfat"], | |
| ["blackjack", "Camelot"], ["blockade", "candidate"], ["blowtorch", "cannonball"], ["bluebird", "Capricorn"], ["bombast", "caravan"], ["bookshelf", "caretaker"], | |
| ["brackish", "celebrate"], ["breadline", "cellulose"], ["breakup", "certify"], ["brickyard", "chambermaid"], ["briefcase", "Cherokee"], ["Burbank", "Chicago"], | |
| ["button", "clergyman"], ["buzzard", "coherence"], ["cement", "combustion"], ["chairlift", "commando"], ["chatter", "company"], ["checkup", "component"], | |
| ["chisel", "concurrent"], ["choking", "confidence"], ["chopper", "conformist"], ["Christmas", "congregate"], ["clamshell", "consensus"], | |
| ["classic", "consulting"], ["classroom", "corporate"], ["cleanup", "corrosion"], ["clockwork", "councilman"], ["cobra", "crossover"], ["commence", "crucifix"], | |
| ["concert", "cumbersome"], ["cowbell", "customer"], ["crackdown", "Dakota"], ["cranky", "decadence"], ["crowfoot", "December"], ["crucial", "decimal"], | |
| ["crumpled", "designing"], ["crusade", "detector"], ["cubic", "detergent"], ["dashboard", "determine"], ["deadbolt", "dictator"], ["deckhand", "dinosaur"], | |
| ["dogsled", "direction"], ["dragnet", "disable"], ["drainage", "disbelief"], ["dreadful", "disruptive"], ["drifter", "distortion"], ["dropper", "document"], | |
| ["drumbeat", "embezzle"], ["drunken", "enchanting"], ["Dupont", "enrollment"], ["dwelling", "enterprise"], ["eating", "equation"], ["edict", "equipment"], | |
| ["egghead", "escapade"], ["eightball", "Eskimo"], ["endorse", "everyday"], ["endow", "examine"], ["enlist", "existence"], ["erase", "exodus"], ["escape", "fascinate"], | |
| ["exceed", "filament"], ["eyeglass", "finicky"], ["eyetooth", "forever"], ["facial", "fortitude"], ["fallout", "frequency"], ["flagpole", "gadgetry"], | |
| ["flatfoot", "Galveston"], ["flytrap", "getaway"], ["fracture", "glossary"], ["framework", "gossamer"], ["freedom", "graduate"], ["frighten", "gravity"], | |
| ["gazelle", "guitarist"], ["Geiger", "hamburger"], ["glitter", "Hamilton"], ["glucose", "handiwork"], ["goggles", "hazardous"], ["goldfish", "headwaters"], | |
| ["gremlin", "hemisphere"], ["guidance", "hesitate"], ["hamlet", "hideaway"], ["highchair", "holiness"], ["hockey", "hurricane"], ["indoors", "hydraulic"], | |
| ["indulge", "impartial"], ["inverse", "impetus"], ["involve", "inception"], ["island", "indigo"], ["jawbone", "inertia"], ["keyboard", "infancy"], | |
| ["kickoff", "inferno"], ["kiwi", "informant"], ["klaxon", "insincere"], ["locale", "insurgent"], ["lockup", "integrate"], ["merit", "intention"], | |
| ["minnow", "inventive"], ["miser", "Istanbul"], ["Mohawk", "Jamaica"], ["mural", "Jupiter"], ["music", "leprosy"], ["necklace", "letterhead"], | |
| ["Neptune", "liberty"], ["newborn", "maritime"], ["nightbird", "matchmaker"], ["Oakland", "maverick"], ["obtuse", "Medusa"], ["offload", "megaton"], | |
| ["optic", "microscope"], ["orca", "microwave"], ["payday", "midsummer"], ["peachy", "millionaire"], ["pheasant", "miracle"], ["physique", "misnomer"], | |
| ["playhouse", "molasses"], ["Pluto", "molecule"], ["preclude", "Montana"], ["prefer", "monument"], ["preshrunk", "mosquito"], ["printer", "narrative"], | |
| ["prowler", "nebula"], ["pupil", "newsletter"], ["puppy", "Norwegian"], ["python", "October"], ["quadrant", "Ohio"], ["quiver", "onlooker"], ["quota", "opulent"], | |
| ["ragtime", "Orlando"], ["ratchet", "outfielder"], ["rebirth", "Pacific"], ["reform", "pandemic"], ["regain", "Pandora"], ["reindeer", "paperweight"], | |
| ["rematch", "paragon"], ["repay", "paragraph"], ["retouch", "paramount"], ["revenge", "passenger"], ["reward", "pedigree"], ["rhythm", "Pegasus"], | |
| ["ribcage", "penetrate"], ["ringbolt", "perceptive"], ["robust", "performance"], ["rocker", "pharmacy"], ["ruffled", "phonetic"], ["sailboat", "photograph"], | |
| ["sawdust", "pioneer"], ["scallion", "pocketful"], ["scenic", "politeness"], ["scorecard", "positive"], ["Scotland", "potato"], ["seabird", "processor"], | |
| ["select", "provincial"], ["sentence", "proximate"], ["shadow", "puberty"], ["shamrock", "publisher"], ["showgirl", "pyramid"], ["skullcap", "quantity"], | |
| ["skydive", "racketeer"], ["slingshot", "rebellion"], ["slowdown", "recipe"], ["snapline", "recover"], ["snapshot", "repellent"], ["snowcap", "replica"], | |
| ["snowslide", "reproduce"], ["solo", "resistor"], ["southward", "responsive"], ["soybean", "retraction"], ["spaniel", "retrieval"], ["spearhead", "retrospect"], | |
| ["spellbind", "revenue"], ["spheroid", "revival"], ["spigot", "revolver"], ["spindle", "sandalwood"], ["spyglass", "sardonic"], ["stagehand", "Saturday"], | |
| ["stagnate", "savagery"], ["stairway", "scavenger"], ["standard", "sensation"], ["stapler", "sociable"], ["steamship", "souvenir"], ["sterling", "specialist"], | |
| ["stockman", "speculate"], ["stopwatch", "stethoscope"], ["stormy", "stupendous"], ["sugar", "supportive"], ["surmount", "surrender"], | |
| ["suspense", "suspicious"], ["sweatband", "sympathy"], ["swelter", "tambourine"], ["tactics", "telephone"], ["talon", "therapist"], ["tapeworm", "tobacco"], | |
| ["tempest", "tolerance"], ["tiger", "tomorrow"], ["tissue", "torpedo"], ["tonic", "tradition"], ["topmost", "travesty"], ["tracker", "trombonist"], | |
| ["transit", "truncated"], ["trauma", "typewriter"], ["treadmill", "ultimate"], ["Trojan", "undaunted"], ["trouble", "underfoot"], ["tumor", "unicorn"], | |
| ["tunnel", "unify"], ["tycoon", "universe"], ["uncut", "unravel"], ["unearth", "upcoming"], ["unwind", "vacancy"], ["uproot", "vagabond"], ["upset", "vertigo"], | |
| ["upshot", "Virginia"], ["vapor", "visitor"], ["village", "vocalist"], ["virus", "voyager"], ["Vulcan", "warranty"], ["waffle", "Waterloo"], | |
| ["wallet", "whimsical"], ["watchword", "Wichita"], ["wayside", "Wilmington"], ["willow", "Wyoming"], ["woodlark", "yesteryear"], ["Zulu", "Yucatán"]] | |
| _unwords="".join(["|%-15s"%x[0].lower() for x in _words]+["|%-15s"%x[1].lower() for x in _words]) | |
| hexre = re.compile("[a-fA-F0-9]+") | |
| def wordify(seq): | |
| """Convert seq (hexadecimal string) to PGP words """ | |
| seq = filter(lambda x: x not in (' ', '\n', '\t'), seq) | |
| seq = "".join(seq) # Python3 compatibility | |
| if not hexre.match(seq): | |
| raise Exception("Input is not a valid hexadecimal value.") | |
| if len(seq) % 2: | |
| raise Exception("Input contains an odd number of bytes.") | |
| ret = [] | |
| for i in range(0, len(seq), 2): | |
| ret.append(_words[int(seq[i:i+2], 16)][(i//2)%2]) | |
| return ret | |
| def wordifyByteLong(longIn): | |
| """Convert longIn (long) to PGP words """ | |
| return wordify(hex(longIn)[2:][:-1]) | |
| def hexify(seq): | |
| """Convert word sequence of PGP words to hex string""" | |
| #cleanup seq first | |
| seq=re.sub("\s\s+", " ",seq.lower()) | |
| tokens=seq.split(" ") | |
| hexes=[[y / 265, y % 256] for y in [_unwords.find("|%s"%x)/16 for x in tokens]] | |
| ret = [] | |
| for idx,hh in enumerate(hexes): | |
| ret.append(hh[1]) | |
| if hh[0]<0: | |
| raise Exception("Input contains bad pgp word at position %d: %s" % (idx,tokens[idx])) | |
| elif hh[0]!=idx%2: | |
| raise Exception("Odd-Even word choice error in input at position %d: %s" % (idx,tokens[idx])) | |
| return "".join(["%02x"%rr for rr in ret]) | |
| def usage(): | |
| print("Usage:") | |
| print(" {0} [fingerprint...] ==> encode hex to pgpwords".format(os.path.basename(sys.argv[0]))) | |
| print(" {0} -d [\"wordlist...\"] ==> decode pgpwords to hex".format(os.path.basename(sys.argv[0]))) | |
| print(" {0} -w ==> print hex to word map".format(os.path.basename(sys.argv[0]))) | |
| print(" {0} -h ==> print word to hex map".format(os.path.basename(sys.argv[0]))) | |
| print("") | |
| print("If called with multiple arguments, they will be concatenated") | |
| print("and treated as a single fingerprint.") | |
| print("") | |
| print("If called with no arguments, input is read from stdin,") | |
| print("and each line is treated as a single fingerprint. In this") | |
| print("mode, invalid values are silently ignored.") | |
| exit(1) | |
| if __name__ == '__main__': | |
| skip=0 | |
| if 1 == len(sys.argv): | |
| fps = sys.stdin.readlines() | |
| else: | |
| fps = [" ".join(sys.argv[1:])] | |
| try: | |
| opts, args = getopt.getopt(argv, "whd:", ["decode="]) | |
| except getopt.GetoptError: | |
| usage() | |
| sys.exit(2) | |
| for opt, arg in opts: | |
| if opt == '-w' or opt == '-h': | |
| for i,w in enumerate(_words): | |
| print("%6s %28s "%(hex(i),str(w))), | |
| if (i+1) % 3 ==0: | |
| print(" ") | |
| sys.exit() | |
| elif opt in ("-d", "--decode"): | |
| decode = True | |
| fps = [" ".join(sys.argv[2:])] | |
| for fp in fps: | |
| try: | |
| if decode: | |
| print("TODO: Implement Decoding") | |
| else: | |
| words = wordify(fp) | |
| print("\n{0}: ".format(fp.strip())) | |
| sys.stdout.write("\t") | |
| for i in range(0, len(words)): | |
| sys.stdout.write(words[i] + " ") | |
| if (not (i+1) % 4) and not i == len(words)-1: | |
| sys.stdout.write("\n\t") | |
| print("") | |
| except Exception as e: | |
| if len(fps) == 1: | |
| print (e) | |
| usage() | |
| print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment