def init_captcha(): dice = SystemRandom() normalset, oddballset = dice.sample(current_app.captcha_data, 2) normals = dice.sample(normalset, NORMALS) oddballs = dice.sample(oddballset - normalset, ODDBALLS) united = normals + oddballs dice.shuffle(united) challenge = ' '.join(united) expiry = datetime.today() + AUTHENTICATION_TIME session['captcha-answer'] = map(lambda s: s.lower(), oddballs) session['captcha-expires'] = expiry if 'captcha-quarantine' in session: del session['captcha-quarantine'] session.modified = True return {'captcha_expires': str(expiry), 'captcha_challenge': challenge}
def new_password(self, user, passhash): """ Return new random password """ chars = '23456qwertasdfgzxcvbQWERTASDFGZXCVB789yuiophjknmYUIPHJKLNM' r = SystemRandom() return ''.join(r.sample(chars, 8))
def __init__(self, length=16): rand = SystemRandom() rand_chars = rand.sample(RandomString.charsets,length) self.generated_string = "".join(rand_chars) m = hashlib.md5() m.update(self.generated_string) self.md5 = m.digest()
def generate_password(wordcount, words): """Takes a dictionary of words, and selects a password. The password has the number of words equal to 'wordcount'. The words are separated by spaces.""" from random import SystemRandom rand = SystemRandom() return reduce(lambda a, b: a + " " + b, map(lambda key: words[key], rand.sample(words, wordcount)))
def __init__(self, secret, parts, subset_parts): if subset_parts >= parts: raise ValueError('Subset parts must be less or equal' 'than all parts count') rand = SystemRandom() parameters = rand.sample(list(range(1, secret)), subset_parts - 1) polynom = lambda x: secret + sum(c * (x**(i + 1)) for i, c in enumerate(parameters)) self.__subset_parts = subset_parts self.__points = [(x, polynom(x)) for x in range(1, parts + 1)]
def simple_dice(xs, ys): r = SystemRandom() x = remove_outliers(np.array(xs), 0, np.max(xs)) y = remove_outliers(np.array(ys), 0, np.max(ys)) x.sort() y.sort() nx = len(x) ny = len(y) darray = np.zeros(50) for i in xrange(0, 50): if nx < ny: xprime = x yprime = r.sample(y, nx) n = float(nx) else: xprime = r.sample(x, ny) yprime = y n = float(ny) xprime.sort() yprime.sort() darray[i] = kumar_hassebrook(xprime, yprime) mu = np.mean(darray) sigma = np.std(darray) conf_level = 0.99 alpha = 1 - conf_level alpha = 1.0 - conf_level z_alpha_2 = norm.ppf(1 - (alpha / 2.0)) e = z_alpha_2 * sigma / math.sqrt(len(darray)) return mu + e
def simple_dice(xs, ys): r = SystemRandom() x = remove_outliers(np.array(xs), 0, np.max(xs)) y = remove_outliers(np.array(ys), 0, np.max(ys)) x.sort() y.sort() nx = len(x) ny = len(y) darray = np.zeros(50) for i in xrange(0,50): if nx < ny: xprime = x yprime = r.sample(y, nx) n = float(nx) else: xprime = r.sample(x, ny) yprime = y n = float(ny) xprime.sort() yprime.sort() darray[i] = kumar_hassebrook(xprime,yprime) mu = np.mean(darray) sigma = np.std(darray) conf_level = 0.99 alpha = 1-conf_level alpha = 1.0 - conf_level z_alpha_2 = norm.ppf(1-(alpha/2.0)) e = z_alpha_2 * sigma/math.sqrt(len(darray)) return mu+e
def corrupt(source, dest, number=200): random = SystemRandom() sbuf = source.getbuffer() length = len(source.getvalue().decode('utf8', errors='replace').split('\n')) waitlist = set() for i in range(0, number): f = '/'.join([dest, '%03d.kif' % i]) lines = set() for j in range(0, random.randint(0, int(length / 1000))): line = random.randint(1, length - j) lines.add('%dd' % line) deletes = ';'.join(lines) args = ['sed', deletes] popen = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout, _ = popen.communicate(sbuf) popen.wait() with open(f, 'w+b') as cur: cur.write(sbuf) cur.flush() if random.choice([True, False]): count = random.randint(1, 6) src = ''.join(random.sample(ALPHABET, count)) dst = ''.join(random.sample(ALPHABET, count)) args = ['sed', 'y/%s/%s/' % (src, dst)] cur = open(f, 'w+b') waitlist.add( (subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE), cur, stdout)) else: with open(f, 'w+b') as cur: cur.write(stdout) cur.flush() for proc, cur, content in waitlist: stdout, _ = proc.communicate(content) proc.wait() cur.write(stdout) cur.flush() cur.close()
def __generate(self): random = SystemRandom() b, i, n, g, o = \ [random.sample(list(range(x, (x * 15) + 1)), 5) for x in range(1, 6)] result = [] for x in range(1, 26): if x == 13: # free spot result.append(0) # zero means 'opened' continue y = x % 5 if y == 1: result.append(b.pop()) elif y == 2: result.append(i.pop()) elif y == 3: result.append(n.pop()) elif y == 4: result.append(g.pop()) elif y == 0: result.append(o.pop()) return result
def simexp(nvars): nvar1 = nvars[0] nvar2 = nvars[1] r = SystemRandom() npr.seed(r.randint(0, 1e15)) x = npr.exponential(30, nvar1) npr.seed(r.randint(0, 1e15)) y = npr.exponential(35, nvar2) if False: x.sort() y.sort() darray = np.zeros(100) for i in xrange(0, 100): yprime = r.sample(y, len(x)) yprime.sort() darray[i] = util.dice(x, yprime) return max_conf_est(darray, 0.99) return new_similarity_invcdf(x, y)
class chall: def __init__(self, size, bits): self.rnd = SystemRandom() self.bits = bits self.size = size self.exp = self.rnd.sample(range(32, size - 1), bits) def get_rand_int(self): res = 2**(self.size - 1) for i in range(self.bits): if self.rnd.randint(0, 1) == 1: res += 2**self.exp[i] return res def get_prime(self): return int(next_prime(self.get_rand_int())) def get_key(self): p = self.get_prime() q = self.get_prime() e = 0x10001 n = p * q phi = (p - 1) * (q - 1) d = inverse(e, phi) pubkey = (n, e) privkey = (n, e, d, p, q) return (pubkey, privkey) def encrypt(self, pt, pubkey): n, e = pubkey return pow(pt, e, n) def decrypt(self, ct, privkey): n, e, d, p, q = privkey return pow(ct, d, n)
def simpareto(nvars): nvar1 = nvars[0] nvar2 = nvars[1] p1 = Pareto(2000.0, 2.0) p2 = Pareto(2000.0, 1.0) x = p1.rnd(nvar1) y = p2.rnd(nvar2) x.sort() y.sort() if False: r = SystemRandom() darray = np.zeros(100) for i in xrange(0, 100): yprime = r.sample(y, len(x)) darray[i] = util.dice(x, yprime) return max_conf_est(darray, 0.99) return new_similarity_invcdf(x, y)
def simexp(nvars): nvar1 = nvars[0] nvar2 = nvars[1] r = SystemRandom() npr.seed(r.randint(0,1e15)) x = npr.exponential(30, nvar1) npr.seed(r.randint(0,1e15)) y = npr.exponential(35,nvar2) if False: x.sort() y.sort() darray = np.zeros(100) for i in xrange(0,100): yprime = r.sample(y, len(x)) yprime.sort() darray[i] = util.dice(x,yprime) return max_conf_est(darray, 0.99) return new_similarity_invcdf(x,y)
def simpareto(nvars): nvar1 = nvars[0] nvar2 = nvars[1] p1 = Pareto(2000.0, 2.0) p2 = Pareto(2000.0, 1.0) x = p1.rnd(nvar1) y = p2.rnd(nvar2) x.sort() y.sort() if False: r = SystemRandom() darray = np.zeros(100) for i in xrange(0,100): yprime = r.sample(y, len(x)) darray[i] = util.dice(x,yprime) return max_conf_est(darray, 0.99) return new_similarity_invcdf(x, y)
def random_digit_string(length): cryptorandom = SystemRandom() randomlist = cryptorandom.sample(range(0, 9), length) return ''.join(str(x) for x in randomlist)
#!/bin/env python3 from string import ascii_uppercase as UC from random import SystemRandom from enigma.machine import EnigmaMachine from secretstuff import FLAG, PLUGBOARD_SETTINGS assert FLAG.isupper() # Without pbcft{...} random = SystemRandom() for _ in range(50): ROTORS = [random.choice(("I","II","III","IV","V","VI","VII","VIII")) for _ in range(3)] REFLECTOR = random.choice(("B", "C", "B-Thin", "C-Thin")) RING_SETTINGS = [random.randrange(26) for _ in range(3)] machine = EnigmaMachine.from_key_sheet( rotors=ROTORS, reflector=REFLECTOR, ring_settings=RING_SETTINGS, plugboard_settings=PLUGBOARD_SETTINGS) machine.set_display(''.join(random.sample(UC, 3))) ciphertext = machine.process_text(FLAG) print(ciphertext)
def _generate_seed(count=15, allow_dups=True): wordList = ( 'abandon', 'ability', 'able', 'about', 'above', 'absent', 'absorb', 'abstract', 'absurd', 'abuse', 'access', 'accident', 'account', 'accuse', 'achieve', 'acid', 'acoustic', 'acquire', 'across', 'act', 'action', 'actor', 'actress', 'actual', 'adapt', 'add', 'addict', 'address', 'adjust', 'admit', 'adult', 'advance', 'advice', 'aerobic', 'affair', 'afford', 'afraid', 'again', 'age', 'agent', 'agree', 'ahead', 'aim', 'air', 'airport', 'aisle', 'alarm', 'album', 'alcohol', 'alert', 'alien', 'all', 'alley', 'allow', 'almost', 'alone', 'alpha', 'already', 'also', 'alter', 'always', 'amateur', 'amazing', 'among', 'amount', 'amused', 'analyst', 'anchor', 'ancient', 'anger', 'angle', 'angry', 'animal', 'ankle', 'announce', 'annual', 'another', 'answer', 'antenna', 'antique', 'anxiety', 'any', 'apart', 'apology', 'appear', 'apple', 'approve', 'april', 'arch', 'arctic', 'area', 'arena', 'argue', 'arm', 'armed', 'armor', 'army', 'around', 'arrange', 'arrest', 'arrive', 'arrow', 'art', 'artefact', 'artist', 'artwork', 'ask', 'aspect', 'assault', 'asset', 'assist', 'assume', 'asthma', 'athlete', 'atom', 'attack', 'attend', 'attitude', 'attract', 'auction', 'audit', 'august', 'aunt', 'author', 'auto', 'autumn', 'average', 'avocado', 'avoid', 'awake', 'aware', 'away', 'awesome', 'awful', 'awkward', 'axis', 'baby', 'bachelor', 'bacon', 'badge', 'bag', 'balance', 'balcony', 'ball', 'bamboo', 'banana', 'banner', 'bar', 'barely', 'bargain', 'barrel', 'base', 'basic', 'basket', 'battle', 'beach', 'bean', 'beauty', 'because', 'become', 'beef', 'before', 'begin', 'behave', 'behind', 'believe', 'below', 'belt', 'bench', 'benefit', 'best', 'betray', 'better', 'between', 'beyond', 'bicycle', 'bid', 'bike', 'bind', 'biology', 'bird', 'birth', 'bitter', 'black', 'blade', 'blame', 'blanket', 'blast', 'bleak', 'bless', 'blind', 'blood', 'blossom', 'blouse', 'blue', 'blur', 'blush', 'board', 'boat', 'body', 'boil', 'bomb', 'bone', 'bonus', 'book', 'boost', 'border', 'boring', 'borrow', 'boss', 'bottom', 'bounce', 'box', 'boy', 'bracket', 'brain', 'brand', 'brass', 'brave', 'bread', 'breeze', 'brick', 'bridge', 'brief', 'bright', 'bring', 'brisk', 'broccoli', 'broken', 'bronze', 'broom', 'brother', 'brown', 'brush', 'bubble', 'buddy', 'budget', 'buffalo', 'build', 'bulb', 'bulk', 'bullet', 'bundle', 'bunker', 'burden', 'burger', 'burst', 'bus', 'business', 'busy', 'butter', 'buyer', 'buzz', 'cabbage', 'cabin', 'cable', 'cactus', 'cage', 'cake', 'call', 'calm', 'camera', 'camp', 'can', 'canal', 'cancel', 'candy', 'cannon', 'canoe', 'canvas', 'canyon', 'capable', 'capital', 'captain', 'car', 'carbon', 'card', 'cargo', 'carpet', 'carry', 'cart', 'case', 'cash', 'casino', 'castle', 'casual', 'cat', 'catalog', 'catch', 'category', 'cattle', 'caught', 'cause', 'caution', 'cave', 'ceiling', 'celery', 'cement', 'census', 'century', 'cereal', 'certain', 'chair', 'chalk', 'champion', 'change', 'chaos', 'chapter', 'charge', 'chase', 'chat', 'cheap', 'check', 'cheese', 'chef', 'cherry', 'chest', 'chicken', 'chief', 'child', 'chimney', 'choice', 'choose', 'chronic', 'chuckle', 'chunk', 'churn', 'cigar', 'cinnamon', 'circle', 'citizen', 'city', 'civil', 'claim', 'clap', 'clarify', 'claw', 'clay', 'clean', 'clerk', 'clever', 'click', 'client', 'cliff', 'climb', 'clinic', 'clip', 'clock', 'clog', 'close', 'cloth', 'cloud', 'clown', 'club', 'clump', 'cluster', 'clutch', 'coach', 'coast', 'coconut', 'code', 'coffee', 'coil', 'coin', 'collect', 'color', 'column', 'combine', 'come', 'comfort', 'comic', 'common', 'company', 'concert', 'conduct', 'confirm', 'congress', 'connect', 'consider', 'control', 'convince', 'cook', 'cool', 'copper', 'copy', 'coral', 'core', 'corn', 'correct', 'cost', 'cotton', 'couch', 'country', 'couple', 'course', 'cousin', 'cover', 'coyote', 'crack', 'cradle', 'craft', 'cram', 'crane', 'crash', 'crater', 'crawl', 'crazy', 'cream', 'credit', 'creek', 'crew', 'cricket', 'crime', 'crisp', 'critic', 'crop', 'cross', 'crouch', 'crowd', 'crucial', 'cruel', 'cruise', 'crumble', 'crunch', 'crush', 'cry', 'crystal', 'cube', 'culture', 'cup', 'cupboard', 'curious', 'current', 'curtain', 'curve', 'cushion', 'custom', 'cute', 'cycle', 'dad', 'damage', 'damp', 'dance', 'danger', 'daring', 'dash', 'daughter', 'dawn', 'day', 'deal', 'debate', 'debris', 'decade', 'december', 'decide', 'decline', 'decorate', 'decrease', 'deer', 'defense', 'define', 'defy', 'degree', 'delay', 'deliver', 'demand', 'demise', 'denial', 'dentist', 'deny', 'depart', 'depend', 'deposit', 'depth', 'deputy', 'derive', 'describe', 'desert', 'design', 'desk', 'despair', 'destroy', 'detail', 'detect', 'develop', 'device', 'devote', 'diagram', 'dial', 'diamond', 'diary', 'dice', 'diesel', 'diet', 'differ', 'digital', 'dignity', 'dilemma', 'dinner', 'dinosaur', 'direct', 'dirt', 'disagree', 'discover', 'disease', 'dish', 'dismiss', 'disorder', 'display', 'distance', 'divert', 'divide', 'divorce', 'dizzy', 'doctor', 'document', 'dog', 'doll', 'dolphin', 'domain', 'donate', 'donkey', 'donor', 'door', 'dose', 'double', 'dove', 'draft', 'dragon', 'drama', 'drastic', 'draw', 'dream', 'dress', 'drift', 'drill', 'drink', 'drip', 'drive', 'drop', 'drum', 'dry', 'duck', 'dumb', 'dune', 'during', 'dust', 'dutch', 'duty', 'dwarf', 'dynamic', 'eager', 'eagle', 'early', 'earn', 'earth', 'easily', 'east', 'easy', 'echo', 'ecology', 'economy', 'edge', 'edit', 'educate', 'effort', 'egg', 'eight', 'either', 'elbow', 'elder', 'electric', 'elegant', 'element', 'elephant', 'elevator', 'elite', 'else', 'embark', 'embody', 'embrace', 'emerge', 'emotion', 'employ', 'empower', 'empty', 'enable', 'enact', 'end', 'endless', 'endorse', 'enemy', 'energy', 'enforce', 'engage', 'engine', 'enhance', 'enjoy', 'enlist', 'enough', 'enrich', 'enroll', 'ensure', 'enter', 'entire', 'entry', 'envelope', 'episode', 'equal', 'equip', 'era', 'erase', 'erode', 'erosion', 'error', 'erupt', 'escape', 'essay', 'essence', 'estate', 'eternal', 'ethics', 'evidence', 'evil', 'evoke', 'evolve', 'exact', 'example', 'excess', 'exchange', 'excite', 'exclude', 'excuse', 'execute', 'exercise', 'exhaust', 'exhibit', 'exile', 'exist', 'exit', 'exotic', 'expand', 'expect', 'expire', 'explain', 'expose', 'express', 'extend', 'extra', 'eye', 'eyebrow', 'fabric', 'face', 'faculty', 'fade', 'faint', 'faith', 'fall', 'false', 'fame', 'family', 'famous', 'fan', 'fancy', 'fantasy', 'farm', 'fashion', 'fat', 'fatal', 'father', 'fatigue', 'fault', 'favorite', 'feature', 'february', 'federal', 'fee', 'feed', 'feel', 'female', 'fence', 'festival', 'fetch', 'fever', 'few', 'fiber', 'fiction', 'field', 'figure', 'file', 'film', 'filter', 'final', 'find', 'fine', 'finger', 'finish', 'fire', 'firm', 'first', 'fiscal', 'fish', 'fit', 'fitness', 'fix', 'flag', 'flame', 'flash', 'flat', 'flavor', 'flee', 'flight', 'flip', 'float', 'flock', 'floor', 'flower', 'fluid', 'flush', 'fly', 'foam', 'focus', 'fog', 'foil', 'fold', 'follow', 'food', 'foot', 'force', 'forest', 'forget', 'fork', 'fortune', 'forum', 'forward', 'fossil', 'foster', 'found', 'fox', 'fragile', 'frame', 'frequent', 'fresh', 'friend', 'fringe', 'frog', 'front', 'frost', 'frown', 'frozen', 'fruit', 'fuel', 'fun', 'funny', 'furnace', 'fury', 'future', 'gadget', 'gain', 'galaxy', 'gallery', 'game', 'gap', 'garage', 'garbage', 'garden', 'garlic', 'garment', 'gas', 'gasp', 'gate', 'gather', 'gauge', 'gaze', 'general', 'genius', 'genre', 'gentle', 'genuine', 'gesture', 'ghost', 'giant', 'gift', 'giggle', 'ginger', 'giraffe', 'girl', 'give', 'glad', 'glance', 'glare', 'glass', 'glide', 'glimpse', 'globe', 'gloom', 'glory', 'glove', 'glow', 'glue', 'goat', 'goddess', 'gold', 'good', 'goose', 'gorilla', 'gospel', 'gossip', 'govern', 'gown', 'grab', 'grace', 'grain', 'grant', 'grape', 'grass', 'gravity', 'great', 'green', 'grid', 'grief', 'grit', 'grocery', 'group', 'grow', 'grunt', 'guard', 'guess', 'guide', 'guilt', 'guitar', 'gun', 'gym', 'habit', 'hair', 'half', 'hammer', 'hamster', 'hand', 'happy', 'harbor', 'hard', 'harsh', 'harvest', 'hat', 'have', 'hawk', 'hazard', 'head', 'health', 'heart', 'heavy', 'hedgehog', 'height', 'hello', 'helmet', 'help', 'hen', 'hero', 'hidden', 'high', 'hill', 'hint', 'hip', 'hire', 'history', 'hobby', 'hockey', 'hold', 'hole', 'holiday', 'hollow', 'home', 'honey', 'hood', 'hope', 'horn', 'horror', 'horse', 'hospital', 'host', 'hotel', 'hour', 'hover', 'hub', 'huge', 'human', 'humble', 'humor', 'hundred', 'hungry', 'hunt', 'hurdle', 'hurry', 'hurt', 'husband', 'hybrid', 'ice', 'icon', 'idea', 'identify', 'idle', 'ignore', 'ill', 'illegal', 'illness', 'image', 'imitate', 'immense', 'immune', 'impact', 'impose', 'improve', 'impulse', 'inch', 'include', 'income', 'increase', 'index', 'indicate', 'indoor', 'industry', 'infant', 'inflict', 'inform', 'inhale', 'inherit', 'initial', 'inject', 'injury', 'inmate', 'inner', 'innocent', 'input', 'inquiry', 'insane', 'insect', 'inside', 'inspire', 'install', 'intact', 'interest', 'into', 'invest', 'invite', 'involve', 'iron', 'island', 'isolate', 'issue', 'item', 'ivory', 'jacket', 'jaguar', 'jar', 'jazz', 'jealous', 'jeans', 'jelly', 'jewel', 'job', 'join', 'joke', 'journey', 'joy', 'judge', 'juice', 'jump', 'jungle', 'junior', 'junk', 'just', 'kangaroo', 'keen', 'keep', 'ketchup', 'key', 'kick', 'kid', 'kidney', 'kind', 'kingdom', 'kiss', 'kit', 'kitchen', 'kite', 'kitten', 'kiwi', 'knee', 'knife', 'knock', 'know', 'lab', 'label', 'labor', 'ladder', 'lady', 'lake', 'lamp', 'language', 'laptop', 'large', 'later', 'latin', 'laugh', 'laundry', 'lava', 'law', 'lawn', 'lawsuit', 'layer', 'lazy', 'leader', 'leaf', 'learn', 'leave', 'lecture', 'left', 'leg', 'legal', 'legend', 'leisure', 'lemon', 'lend', 'length', 'lens', 'leopard', 'lesson', 'letter', 'level', 'liar', 'liberty', 'library', 'license', 'life', 'lift', 'light', 'like', 'limb', 'limit', 'link', 'lion', 'liquid', 'list', 'little', 'live', 'lizard', 'load', 'loan', 'lobster', 'local', 'lock', 'logic', 'lonely', 'long', 'loop', 'lottery', 'loud', 'lounge', 'love', 'loyal', 'lucky', 'luggage', 'lumber', 'lunar', 'lunch', 'luxury', 'lyrics', 'machine', 'mad', 'magic', 'magnet', 'maid', 'mail', 'main', 'major', 'make', 'mammal', 'man', 'manage', 'mandate', 'mango', 'mansion', 'manual', 'maple', 'marble', 'march', 'margin', 'marine', 'market', 'marriage', 'mask', 'mass', 'master', 'match', 'material', 'math', 'matrix', 'matter', 'maximum', 'maze', 'meadow', 'mean', 'measure', 'meat', 'mechanic', 'medal', 'media', 'melody', 'melt', 'member', 'memory', 'mention', 'menu', 'mercy', 'merge', 'merit', 'merry', 'mesh', 'message', 'metal', 'method', 'middle', 'midnight', 'milk', 'million', 'mimic', 'mind', 'minimum', 'minor', 'minute', 'miracle', 'mirror', 'misery', 'miss', 'mistake', 'mix', 'mixed', 'mixture', 'mobile', 'model', 'modify', 'mom', 'moment', 'monitor', 'monkey', 'monster', 'month', 'moon', 'moral', 'more', 'morning', 'mosquito', 'mother', 'motion', 'motor', 'mountain', 'mouse', 'move', 'movie', 'much', 'muffin', 'mule', 'multiply', 'muscle', 'museum', 'mushroom', 'music', 'must', 'mutual', 'myself', 'mystery', 'myth', 'naive', 'name', 'napkin', 'narrow', 'nasty', 'nation', 'nature', 'near', 'neck', 'need', 'negative', 'neglect', 'neither', 'nephew', 'nerve', 'nest', 'net', 'network', 'neutral', 'never', 'news', 'next', 'nice', 'night', 'noble', 'noise', 'nominee', 'noodle', 'normal', 'north', 'nose', 'notable', 'note', 'nothing', 'notice', 'novel', 'now', 'nuclear', 'number', 'nurse', 'nut', 'oak', 'obey', 'object', 'oblige', 'obscure', 'observe', 'obtain', 'obvious', 'occur', 'ocean', 'october', 'odor', 'off', 'offer', 'office', 'often', 'oil', 'okay', 'old', 'olive', 'olympic', 'omit', 'once', 'one', 'onion', 'online', 'only', 'open', 'opera', 'opinion', 'oppose', 'option', 'orange', 'orbit', 'orchard', 'order', 'ordinary', 'organ', 'orient', 'original', 'orphan', 'ostrich', 'other', 'outdoor', 'outer', 'output', 'outside', 'oval', 'oven', 'over', 'own', 'owner', 'oxygen', 'oyster', 'ozone', 'pact', 'paddle', 'page', 'pair', 'palace', 'palm', 'panda', 'panel', 'panic', 'panther', 'paper', 'parade', 'parent', 'park', 'parrot', 'party', 'pass', 'patch', 'path', 'patient', 'patrol', 'pattern', 'pause', 'pave', 'payment', 'peace', 'peanut', 'pear', 'peasant', 'pelican', 'pen', 'penalty', 'pencil', 'people', 'pepper', 'perfect', 'permit', 'person', 'pet', 'phone', 'photo', 'phrase', 'physical', 'piano', 'picnic', 'picture', 'piece', 'pig', 'pigeon', 'pill', 'pilot', 'pink', 'pioneer', 'pipe', 'pistol', 'pitch', 'pizza', 'place', 'planet', 'plastic', 'plate', 'play', 'please', 'pledge', 'pluck', 'plug', 'plunge', 'poem', 'poet', 'point', 'polar', 'pole', 'police', 'pond', 'pony', 'pool', 'popular', 'portion', 'position', 'possible', 'post', 'potato', 'pottery', 'poverty', 'powder', 'power', 'practice', 'praise', 'predict', 'prefer', 'prepare', 'present', 'pretty', 'prevent', 'price', 'pride', 'primary', 'print', 'priority', 'prison', 'private', 'prize', 'problem', 'process', 'produce', 'profit', 'program', 'project', 'promote', 'proof', 'property', 'prosper', 'protect', 'proud', 'provide', 'public', 'pudding', 'pull', 'pulp', 'pulse', 'pumpkin', 'punch', 'pupil', 'puppy', 'purchase', 'purity', 'purpose', 'purse', 'push', 'put', 'puzzle', 'pyramid', 'quality', 'quantum', 'quarter', 'question', 'quick', 'quit', 'quiz', 'quote', 'rabbit', 'raccoon', 'race', 'rack', 'radar', 'radio', 'rail', 'rain', 'raise', 'rally', 'ramp', 'ranch', 'random', 'range', 'rapid', 'rare', 'rate', 'rather', 'raven', 'raw', 'razor', 'ready', 'real', 'reason', 'rebel', 'rebuild', 'recall', 'receive', 'recipe', 'record', 'recycle', 'reduce', 'reflect', 'reform', 'refuse', 'region', 'regret', 'regular', 'reject', 'relax', 'release', 'relief', 'rely', 'remain', 'remember', 'remind', 'remove', 'render', 'renew', 'rent', 'reopen', 'repair', 'repeat', 'replace', 'report', 'require', 'rescue', 'resemble', 'resist', 'resource', 'response', 'result', 'retire', 'retreat', 'return', 'reunion', 'reveal', 'review', 'reward', 'rhythm', 'rib', 'ribbon', 'rice', 'rich', 'ride', 'ridge', 'rifle', 'right', 'rigid', 'ring', 'riot', 'ripple', 'risk', 'ritual', 'rival', 'river', 'road', 'roast', 'robot', 'robust', 'rocket', 'romance', 'roof', 'rookie', 'room', 'rose', 'rotate', 'rough', 'round', 'route', 'royal', 'rubber', 'rude', 'rug', 'rule', 'run', 'runway', 'rural', 'sad', 'saddle', 'sadness', 'safe', 'sail', 'salad', 'salmon', 'salon', 'salt', 'salute', 'same', 'sample', 'sand', 'satisfy', 'satoshi', 'sauce', 'sausage', 'save', 'say', 'scale', 'scan', 'scare', 'scatter', 'scene', 'scheme', 'school', 'science', 'scissors', 'scorpion', 'scout', 'scrap', 'screen', 'script', 'scrub', 'sea', 'search', 'season', 'seat', 'second', 'secret', 'section', 'security', 'seed', 'seek', 'segment', 'select', 'sell', 'seminar', 'senior', 'sense', 'sentence', 'series', 'service', 'session', 'settle', 'setup', 'seven', 'shadow', 'shaft', 'shallow', 'share', 'shed', 'shell', 'sheriff', 'shield', 'shift', 'shine', 'ship', 'shiver', 'shock', 'shoe', 'shoot', 'shop', 'short', 'shoulder', 'shove', 'shrimp', 'shrug', 'shuffle', 'shy', 'sibling', 'sick', 'side', 'siege', 'sight', 'sign', 'silent', 'silk', 'silly', 'silver', 'similar', 'simple', 'since', 'sing', 'siren', 'sister', 'situate', 'six', 'size', 'skate', 'sketch', 'ski', 'skill', 'skin', 'skirt', 'skull', 'slab', 'slam', 'sleep', 'slender', 'slice', 'slide', 'slight', 'slim', 'slogan', 'slot', 'slow', 'slush', 'small', 'smart', 'smile', 'smoke', 'smooth', 'snack', 'snake', 'snap', 'sniff', 'snow', 'soap', 'soccer', 'social', 'sock', 'soda', 'soft', 'solar', 'soldier', 'solid', 'solution', 'solve', 'someone', 'song', 'soon', 'sorry', 'sort', 'soul', 'sound', 'soup', 'source', 'south', 'space', 'spare', 'spatial', 'spawn', 'speak', 'special', 'speed', 'spell', 'spend', 'sphere', 'spice', 'spider', 'spike', 'spin', 'spirit', 'split', 'spoil', 'sponsor', 'spoon', 'sport', 'spot', 'spray', 'spread', 'spring', 'spy', 'square', 'squeeze', 'squirrel', 'stable', 'stadium', 'staff', 'stage', 'stairs', 'stamp', 'stand', 'start', 'state', 'stay', 'steak', 'steel', 'stem', 'step', 'stereo', 'stick', 'still', 'sting', 'stock', 'stomach', 'stone', 'stool', 'story', 'stove', 'strategy', 'street', 'strike', 'strong', 'struggle', 'student', 'stuff', 'stumble', 'style', 'subject', 'submit', 'subway', 'success', 'such', 'sudden', 'suffer', 'sugar', 'suggest', 'suit', 'summer', 'sun', 'sunny', 'sunset', 'super', 'supply', 'supreme', 'sure', 'surface', 'surge', 'surprise', 'surround', 'survey', 'suspect', 'sustain', 'swallow', 'swamp', 'swap', 'swarm', 'swear', 'sweet', 'swift', 'swim', 'swing', 'switch', 'sword', 'symbol', 'symptom', 'syrup', 'system', 'table', 'tackle', 'tag', 'tail', 'talent', 'talk', 'tank', 'tape', 'target', 'task', 'taste', 'tattoo', 'taxi', 'teach', 'team', 'tell', 'ten', 'tenant', 'tennis', 'tent', 'term', 'test', 'text', 'thank', 'that', 'theme', 'then', 'theory', 'there', 'they', 'thing', 'this', 'thought', 'three', 'thrive', 'throw', 'thumb', 'thunder', 'ticket', 'tide', 'tiger', 'tilt', 'timber', 'time', 'tiny', 'tip', 'tired', 'tissue', 'title', 'toast', 'tobacco', 'today', 'toddler', 'toe', 'together', 'toilet', 'token', 'tomato', 'tomorrow', 'tone', 'tongue', 'tonight', 'tool', 'tooth', 'top', 'topic', 'topple', 'torch', 'tornado', 'tortoise', 'toss', 'total', 'tourist', 'toward', 'tower', 'town', 'toy', 'track', 'trade', 'traffic', 'tragic', 'train', 'transfer', 'trap', 'trash', 'travel', 'tray', 'treat', 'tree', 'trend', 'trial', 'tribe', 'trick', 'trigger', 'trim', 'trip', 'trophy', 'trouble', 'truck', 'true', 'truly', 'trumpet', 'trust', 'truth', 'try', 'tube', 'tuition', 'tumble', 'tuna', 'tunnel', 'turkey', 'turn', 'turtle', 'twelve', 'twenty', 'twice', 'twin', 'twist', 'two', 'type', 'typical', 'ugly', 'umbrella', 'unable', 'unaware', 'uncle', 'uncover', 'under', 'undo', 'unfair', 'unfold', 'unhappy', 'uniform', 'unique', 'unit', 'universe', 'unknown', 'unlock', 'until', 'unusual', 'unveil', 'update', 'upgrade', 'uphold', 'upon', 'upper', 'upset', 'urban', 'urge', 'usage', 'use', 'used', 'useful', 'useless', 'usual', 'utility', 'vacant', 'vacuum', 'vague', 'valid', 'valley', 'valve', 'van', 'vanish', 'vapor', 'various', 'vast', 'vault', 'vehicle', 'velvet', 'vendor', 'venture', 'venue', 'verb', 'verify', 'version', 'very', 'vessel', 'veteran', 'viable', 'vibrant', 'vicious', 'victory', 'video', 'view', 'village', 'vintage', 'violin', 'virtual', 'virus', 'visa', 'visit', 'visual', 'vital', 'vivid', 'vocal', 'voice', 'void', 'volcano', 'volume', 'vote', 'voyage', 'wage', 'wagon', 'wait', 'walk', 'wall', 'walnut', 'want', 'warfare', 'warm', 'warrior', 'wash', 'wasp', 'waste', 'water', 'wave', 'way', 'wealth', 'weapon', 'wear', 'weasel', 'weather', 'web', 'wedding', 'weekend', 'weird', 'welcome', 'west', 'wet', 'whale', 'what', 'wheat', 'wheel', 'when', 'where', 'whip', 'whisper', 'wide', 'width', 'wife', 'wild', 'will', 'win', 'window', 'wine', 'wing', 'wink', 'winner', 'winter', 'wire', 'wisdom', 'wise', 'wish', 'witness', 'wolf', 'woman', 'wonder', 'wood', 'wool', 'word', 'work', 'world', 'worry', 'worth', 'wrap', 'wreck', 'wrestle', 'wrist', 'write', 'wrong', 'yard', 'year', 'yellow', 'you', 'young', 'youth', 'zebra', 'zero', 'zone', 'zoo') from random import SystemRandom sysrandom = SystemRandom() if not allow_dups: return sysrandom.sample(wordList, count) try: return sysrandom.choices(wordList, count) # available from python3.6 except: pass return [sysrandom.choice(wordList) for i in range(count)]
class DHTSpyder(UDPServer): def __init__(self, bootstrap_nodes, node_id=None, miner_interval=0.001, **kwargs): super(DHTSpyder, self).__init__(**kwargs) self.bootstrap_nodes = bootstrap_nodes self.node_id = node_id or generate_node_id() self.miner_interval = miner_interval self.routing_table = [set() for _ in range(160)] self.candidates = set() self.searchers = {} self.searchers_seq = 0 self.random = SystemRandom() def connection_made(self): # Bootstrap for node in self.bootstrap_nodes: self.find_node(node, self.node_id) # Start miner asyncio.ensure_future(self._dig_periodically(), loop=self.loop) asyncio.ensure_future(self._update_searchers_periodically(), loop=self.loop) async def datagram_received(self, data, addr): try: msg = bdecode(data, decoder=decode_bkeys) except BTFailure: # TODO: Log error pass else: self.handle_message(msg, addr) def _fetch_random_candidates(self, k): result = self.random.sample(self.candidates, k) for item in result: self.candidates.remove(item) return result def _new_event(self, handler): asyncio.ensure_future(handler, loop=self.loop) async def _dig_periodically(self): while True: target_id = generate_node_id() nodes = [ *self.get_closest_nodes(target_id), *self._fetch_random_candidates(min(len(self.candidates), 7)) ] for _, host, port in nodes: self.find_node((host, port), target_id) await asyncio.sleep(self.miner_interval) async def _update_searchers_periodically(self): while True: now = datetime.now() for t, item in self.searchers.copy().items(): if (now - item.timestamp).seconds >= 120: if item.values: self._new_event( self.peers_values_received(item.info_hash, item.values)) self.searchers.pop(t) await asyncio.sleep(1) def send_message(self, data, addr): self.send(bencode(data), addr) def find_node(self, addr, target=None): self.send_message( { "t": generate_id(), "y": "q", "q": "find_node", "a": { "id": generate_node_id(), "target": target or generate_node_id() } }, addr) def get_peers(self, addr, info_hash, t=None): self.send_message( { "t": t or generate_id(), "y": "q", "q": "get_peers", "a": { "id": generate_node_id(), "info_hash": info_hash } }, addr) def get_closest_nodes(self, target_id, k_value=8): closest_l = set() closest_r = set() r_table_idx = get_routing_table_index(xor(self.node_id, target_id)) idx = r_table_idx while idx >= 0 and len(closest_l) < k_value: closest_l |= set( fetch_k_closest_nodes(self.routing_table[idx], target_id, k_value)) idx -= 1 idx = r_table_idx + 1 while idx < 160 and len(closest_r) < k_value: closest_r |= set( fetch_k_closest_nodes(self.routing_table[idx], target_id, k_value)) idx += 1 return fetch_k_closest_nodes(closest_l | closest_r, target_id, k_value) def add_node(self, node): r_table_index = get_routing_table_index(xor(node.id, self.node_id)) rt = self.routing_table[r_table_index] if len(rt) < 1600: rt.add(node) elif get_rand_bool() and node not in rt: rt.remove(self.random.sample(rt, 1)[0]) else: self.find_node((node.host, node.port)) def search_peers(self, info_hash): if self.searchers_seq >= 2**32 - 1: self.searchers_seq = 0 else: self.searchers_seq += 1 t = self.searchers_seq.to_bytes(4, "big") self.searchers[t] = Searcher(info_hash, set(), set(), 8, datetime.now()) for node in self.get_closest_nodes(info_hash, 16): self.get_peers((node.host, node.port), info_hash, t) def update_peers_searcher(self, t, nodes, values): info_hash, old_nodes, old_values, attempts_count, timestamp = self.searchers.pop( t) # "nodes" and "values" must be instance of "set" type new_nodes = old_nodes | nodes new_values = old_values | values # If new_closest contains same data as old_closest, we decrease attempt counter new_closest, old_closest = (set(fetch_k_closest_nodes( n, info_hash, 16)) for n in (new_nodes, old_nodes)) if new_closest == old_closest: attempts_count -= 1 if attempts_count > 0: self.searchers[t] = Searcher(info_hash, new_nodes, new_values, attempts_count, timestamp) for node in new_closest: self.get_peers((node.host, node.port), info_hash, t) elif new_values: self._new_event(self.peers_values_received(info_hash, new_values)) def handle_message(self, msg, addr): try: msg_type = str(msg.get("y", b""), "utf-8") if msg_type == "r": self.handle_response(msg, addr) elif msg_type == "q": self.handle_query(msg, addr) except: self.send_message( { "y": "e", "e": [202, "Server Error"], "t": msg.get("t", "") }, addr) raise def handle_response(self, msg, addr): args = msg["r"] t = msg["t"] node_id = args["id"] nodes = set(decode_nodes(args.get("nodes", b""))) values = set(decode_values(args.get("values", []))) if t in self.searchers: self.update_peers_searcher(t, nodes, values) else: new_candidates = self.random.sample(nodes, min(len(nodes), 8)) if len(self.candidates) > 160000: self._fetch_random_candidates(len(self.candidates) - 160000) self.candidates.update(new_candidates) self.add_node(Node(node_id, addr[0], addr[1])) def handle_query(self, msg, addr): args = msg["a"] node_id = args["id"] query_type = str(msg["q"], "utf-8") response_id = self.node_id if query_type == "ping": self.send_message( { "t": msg["t"], "y": "r", "r": { "id": response_id } }, addr) self._new_event(self.ping_received(node_id, addr)) elif query_type == "find_node": target_node_id = args["target"] self.send_message( { "t": msg["t"], "y": "r", "r": { "id": response_id, "nodes": encode_nodes(self.get_closest_nodes(target_node_id)) } }, addr) self._new_event( self.find_node_received(node_id, target_node_id, addr)) elif query_type == "get_peers": info_hash = args["info_hash"] token = generate_node_id() self.send_message( { "t": msg["t"], "y": "r", "r": { "id": response_id, "nodes": encode_nodes( self.get_closest_nodes(info_hash)), "token": token } }, addr) self._new_event(self.get_peers_received(node_id, info_hash, addr)) elif query_type == "announce_peer": info_hash = args["info_hash"] port = args.get("port", None) self.send_message( { "t": msg["t"], "y": "r", "r": { "id": response_id } }, addr) self._new_event( self.announce_peer_received(node_id, info_hash, port, addr)) self.add_node(Node(node_id, addr[0], addr[1])) async def ping_received(self, node_id, addr): pass async def find_node_received(self, node_id, target, addr): pass async def get_peers_received(self, node_id, info_hash, addr): pass async def announce_peer_received(self, node_id, info_hash, port, addr): pass async def peers_values_received(self, info_hash, peers): pass
class DHTCrawler(asyncio.DatagramProtocol): def __init__(self, bootstrap_nodes, node_id=None, loop=None, interval=0.001): self.node_id = node_id or generate_node_id() self.loop = loop or asyncio.get_event_loop() self.bootstrap_nodes = bootstrap_nodes self.interval = interval self.routing_table = [set() for _ in range(160)] self.candidates = [] self.searchers = {} self.searchers_seq = 0 self.transport = None self.__running = False self.random = SystemRandom() def connection_made(self, transport): self.transport = transport def connection_lost(self, exc): self.stop() self.transport.close() def datagram_received(self, data, addr): try: msg = bdecode(data) asyncio.ensure_future(self.handle_message(msg, addr), loop=self.loop) except BTFailure: pass def run(self, host, port): coro = self.loop.create_datagram_endpoint(lambda: self, local_addr=(host, port)) self.loop.run_until_complete(coro) # Bootstrap for node in self.bootstrap_nodes: self.find_node(node, self.node_id) asyncio.ensure_future(self.auto_find_nodes(), loop=self.loop) self.loop.run_forever() self.loop.close() def stop(self): self.__running = False self.loop.call_later(self.interval, self.loop.stop) def send_message(self, data, addr): self.transport.sendto(bencode(data), addr) def find_node(self, addr, target=None): self.send_message({ "t": generate_id(), "y": "q", "q": "find_node", "a": { "id": generate_node_id(), "target": target or generate_node_id() } }, addr) def get_peers(self, addr, info_hash, t=None): self.send_message({ "t": t or generate_id(), "y": "q", "q": "get_peers", "a": { "id": generate_node_id(), "info_hash": info_hash } }, addr) def get_closest_nodes(self, target_id, k_value=8): closest_l = set() closest_r = set() r_table_idx = get_routing_table_index(xor(self.node_id, target_id)) idx = r_table_idx while idx >= 0 and len(closest_l) < k_value: closest_l |= set(fetch_k_closest_nodes(self.routing_table[idx], target_id, k_value)) idx -= 1 idx = r_table_idx + 1 while idx < 160 and len(closest_r) < k_value: closest_r |= set(fetch_k_closest_nodes(self.routing_table[idx], target_id, k_value)) idx += 1 return fetch_k_closest_nodes(closest_l | closest_r, target_id, k_value) def add_node(self, node): r_table_index = get_routing_table_index(xor(node.id, self.node_id)) rt = self.routing_table[r_table_index] if len(rt) < 1600: rt.add(node) elif get_rand_bool() and node not in rt: rt.remove(self.random.sample(rt, 1)[0]) else: self.find_node((node.host, node.port)) self.routing_table[r_table_index] = rt # ??? async def search_peers(self, info_hash): if self.searchers_seq >= 2 ** 32 - 1: self.searchers_seq = 0 else: self.searchers_seq += 1 t = self.searchers_seq.to_bytes(4, "big") self.searchers[t] = Searcher(info_hash, set(), set(), 8, datetime.now()) for node in self.get_closest_nodes(info_hash, 16): self.get_peers((node.host, node.port), info_hash, t) async def update_peers_searcher(self, t, nodes, values): searcher = self.searchers.pop(t, None) if not searcher: return info_hash, old_nodes, old_values, attempts_count, timestamp = searcher # "nodes" and "values" must be instance of "set" type new_nodes = old_nodes | nodes new_values = old_values | values # If new_closest contains same data as old_closest, we decrease attempt counter new_closest, old_closest = (set(fetch_k_closest_nodes(n, info_hash, 16)) for n in (new_nodes, old_nodes)) if new_closest == old_closest: attempts_count -= 1 if attempts_count > 0: self.searchers[t] = Searcher(info_hash, new_nodes, new_values, attempts_count, timestamp) for node in new_closest: self.get_peers((node.host, node.port), info_hash, t) else: await self.peers_values_received(info_hash, new_values) async def handle_message(self, msg, addr): try: msg_type = str(msg.get("y", b""), "utf-8") if msg_type == "r": await self.handle_response(msg, addr) elif msg_type == "q": await self.handle_query(msg, addr) except: self.send_message({ "y": "e", "e": [202, "Server Error"], "t": msg.get("t", "") }, addr) raise async def handle_response(self, msg, addr): args = msg["r"] t = msg["t"] node_id = args["id"] nodes = set(decode_nodes(args.get("nodes", b""))) values = set(decode_values(args.get("values", []))) if t in self.searchers: await self.update_peers_searcher(t, nodes, values) else: if len(self.candidates) >= 16000: self.candidates.pop(self.random.randrange(len(self.candidates))) self.candidates.append(self.random.sample(nodes, min(len(nodes), 8))) self.add_node(Node(node_id, addr[0], addr[1])) await asyncio.sleep(self.interval) async def handle_query(self, msg, addr): args = msg["a"] node_id = args["id"] query_type = str(msg["q"], "utf-8") response_id = self.node_id if query_type == "ping": self.send_message({ "t": msg["t"], "y": "r", "r": { "id": response_id } }, addr) await self.ping_received(node_id, addr) elif query_type == "find_node": target_node_id = args["target"] self.send_message({ "t": msg["t"], "y": "r", "r": { "id": response_id, "nodes": encode_nodes(self.get_closest_nodes(target_node_id)) } }, addr) await self.find_node_received(node_id, target_node_id, addr) elif query_type == "get_peers": info_hash = args["info_hash"] token = generate_node_id() self.send_message({ "t": msg["t"], "y": "r", "r": { "id": response_id, "nodes": encode_nodes(self.get_closest_nodes(info_hash)), "token": token } }, addr) await self.get_peers_received(node_id, info_hash, addr) elif query_type == "announce_peer": info_hash = args["info_hash"] port = args.get("port", None) self.send_message({ "t": msg["t"], "y": "r", "r": { "id": response_id } }, addr) await self.announce_peer_received(node_id, info_hash, port, addr) self.add_node(Node(node_id, addr[0], addr[1])) await asyncio.sleep(self.interval) async def auto_find_nodes(self): self.__running = True while self.__running: await asyncio.sleep(self.interval) target_id = generate_node_id() nodes = self.get_closest_nodes(target_id) for _ in range(min(len(self.candidates), 7)): nodes.extend(self.candidates.pop(self.random.randrange(len(self.candidates)))) for _, host, port in nodes: self.find_node((host, port), target_id) now = datetime.now() for t, item in self.searchers.copy().items(): if (now - item.timestamp).seconds >= 60: await self.peers_values_received(item.info_hash, item.values) self.searchers.pop(t) async def ping_received(self, node_id, addr): pass async def find_node_received(self, node_id, target, addr): pass async def get_peers_received(self, node_id, info_hash, addr): pass async def announce_peer_received(self, node_id, info_hash, port, addr): pass async def peers_values_received(self, info_hash, peers): pass
class FruitMachine: """A Fruit Machine bot.""" random: SystemRandom def __init__(self, resources: Optional[Resources] = None): """Initialise FruitMachine.""" if not resources: self.res = Resources() else: self.res = resources self.random = SystemRandom() def get_random_machine(self): """Get a random Machine style.""" return self.random.choice(self.res.get_machines()) def get_random_reel_symbols(self, count=3) -> SpunReels: """Get a random set of 'count' symbols for each reel.""" reels: MutableSequence[SpunReel] = [] for reel in self.res.get_reels(): spun_reel = [] for symbol in self.random.sample(reel.symbols, count): selected_image = self.random.choice(symbol.image_files) selected = ConcreteReelSymbol(description=symbol.description, image_file=selected_image) spun_reel.append(selected) reels.append(tuple(spun_reel)) return tuple(reels) def randomize_machine(self) -> Tuple[MachineStyle, SpunReels, str, str]: """Generate a machine and its messages.""" machine = self.get_random_machine() reels = self.get_random_reel_symbols(count=len(machine.positions[0])) payline = [r[1].description for r in reels] description = f"A {machine.description} Fruit Machine with the " \ f"centre payline showing a combination of {payline[0]}, " \ f"{payline[1]}, and {payline[2]}." status_message = PhraseGenerator(self.res.get_status_templates()) \ .generate_phrase(machine=machine, reels=reels) return machine, reels, description, status_message def generate_image(self, machine: MachineStyle, reels: SpunReels, fp: BinaryIO): """Generate and write the Fruit Machine image to a file pointer.""" img = Image.open(machine.background).convert(mode='RGBA') for reel, reel_pos in zip(reels, machine.positions): for symbol, position in zip(reel, reel_pos): im_symb = Image.new(mode='RGBA', size=img.size) im_symb.paste(Image.open(symbol.image_file), position) img = Image.alpha_composite(img, im_symb) im_fg = Image.open(machine.foreground).convert(mode='RGBA') img = Image.alpha_composite(img, im_fg) img.save(fp, format='PNG') def generate(self, image_fp: BinaryIO) -> Tuple[str, str]: """Generate a random fruit machine image, description and status.""" machine, reels, description, status_message = self.randomize_machine() self.generate_image(machine=machine, reels=reels, fp=image_fp) return description, status_message
class DataStore(ABC): """ A data store of names of people who can save Eorzea. This base class provides in memory storage, and has two abstract functions of `_write_append` for incremental updates and `_write_list` for full rebuilds of a backing store, of which one of which SHOULD have a complete implementation. """ known: Optional[Dict[str, Record]] rand: SystemRandom = SystemRandom() seen: Set[str] def __init__(self, values: Optional[List[Record]] = None): """Sets up the data store, with the initial set of data that was loaded out of the data store""" super().__init__() # store the initial set of values. self.known = {r.name: r for r in values} if values else None self.seen = set() self.rand = SystemRandom() def add(self, value: str, added_by: str, added_from: str) -> bool: """Adds a value to the DataStore. If this value is already in the store, this function is a no-op that will always return true. Otherwise, this function will return true if the data is successfully written to both the in-memory storage and to the backing store.""" if self.known and value in self.known: return True record = Record(value, added_by, added_from) # Function succeeds iff the backing store if updated, # _or_ if this DataStore does not support incremental # updates. if self._write_append(record) in [False]: return False if self.known: self.known[record.name] = record return True def random(self) -> Record: """Selects a random element from this store.""" if not self.known: raise Exception("Empty storage") record = self.rand.sample(list(self.known.values()), 1)[0] self.seen.add(record.name) return record @abstractmethod def _write_append(self, record: Record) -> Optional[bool]: """Append a value to the underlying data store this type implements. This function may be a no-op method, in which case it MUST return None. Otherwise, it should return if the write succeeded. Values passed to this function SHOULD NOT exist in the store already, so the implement does not need to consider de-duplication. """ @abstractmethod def _write_list(self, record: List[Record]) -> Optional[bool]: """Writes an entire list to the backing store, replacing any existing list. This function may be a no-op, in which case it must always return None. Otherwise, it should return whether or not the operation succeeded.""" def __len__(self) -> int: if not self.known: raise Exception("Empty storage") return len(self.known) def __enter__(self) -> DataStore: return self def __exit__(self, exception_type: RaiseType, message: Any, traceback: Any) -> Optional[bool]: if self.known: if self._write_list(list(self.known.values())) in [False]: raise Exception("Error writing list to DataStore") return exception_type is None
class zulilyLogin(object): """.. :py:class:: zulilyLogin login, check whether login, fetch page. """ def __init__(self): """.. :py:method:: variables need to be used """ self.login_url = 'https://www.zulily.com/auth' self.badpage_url = re.compile('.*zulily.com/z/.*html') self.rd = SystemRandom() self.conf = ConfigParser.ConfigParser() self.data = { 'login[username]': get_login_email('zulily'), 'login[password]': login_passwd } self.current_email = self.data['login[username]'] # self.reg_check = re.compile(r'https://www.zulily.com/auth/create.*') # need to authentication self._signin = {} def create_new_account(self): regist_url = 'https://www.zulily.com/auth/create/' regist_data = { 'firstname': 'first', 'lastname': 'last', 'email': ''.join( self.rd.sample(alphabet, 10) ) + '@gmail.com', 'password': login_passwd, 'confirmation': login_passwd, } ret = req.post(regist_url, data=regist_data) return ret.url, regist_data['email'] def change_username(self): """ Already signed up. https://www.zulily.com/auth/?email=abcd1234%40gmail.com&firstname=first&lastname=last Could not process your sign up. Please try again. https://www.zulily.com/auth/create/?email=tty1tty1%40gmail.com&firstname=first&lastname=last """ returl, email = self.create_new_account() times = 0 while returl.startswith('https://www.zulily.com/auth/'): returl, email = self.create_new_account() times += 1 if times >= 3: time.sleep(600) times = 0 self.conf.read( os.path.join(os.path.dirname(__file__), '../common/username.ini') ) self.conf.set('username', DB, email) self.conf.write(open(os.path.join(os.path.dirname(__file__), '../common/username.ini'), 'w')) return email def whether_itis_badpage(self, url): if self.badpage_url.match(url): self.logout_account() email = self.change_username() self.current_email = email self.login_account() return True else: return False def login_account(self): """.. :py:method:: use post method to login """ self.data['login[username]'] = self.current_email req.post(self.login_url, data=self.data) self._signin[self.current_email] = True def logout_account(self): req.get('https://www.zulily.com/auth/logout/') def check_signin(self, username=''): """.. :py:method:: check whether the account is login """ if username == '': self.logout_account() self.login_account() elif username not in self._signin: self.current_email = username self.logout_account() self.login_account() else: self.current_email = username def fetch_page(self, url): """.. :py:method:: fetch page. check whether the account is login, if not, login and fetch again """ ret = req.get(url) time.sleep(SLEEP) if self.whether_itis_badpage(ret.url): ret = req.get(url) if ret.url == u'http://www.zulily.com/oops-event': return -404 if ret.url == 'http://www.zulily.com/?tab=new-today': self.logout_account() self.login_account() ret = req.get(url) if ret.ok: return ret.content return ret.status_code def fetch_listing_page(self, url): """.. :py:method:: fetch page. check whether the account is login, if not, login and fetch again """ ret = req.get(url) time.sleep(SLEEP) if self.whether_itis_badpage(ret.url): ret = req.get(url) if ret.url == u'http://www.zulily.com/oops-event': return -404 if ret.url == u'http://www.zulily.com/' or 'http://www.zulily.com/brand/' in ret.url: return -302 if ret.url == 'http://www.zulily.com/?tab=new-today': self.logout_account() self.login_account() ret = req.get(url) if ret.ok: return ret.content return ret.status_code
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70 ]) red_balls = set([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ]) print("Adding entropy to seeder.") seeder = SystemRandom() # Create quick pick ticket print("Selecting quick pick for odds test.") qp_numbers = seeder.sample(white_balls, 5) qp_meganumber = seeder.sample(red_balls, 1) # Sort quick pick qp_numbers.sort() # Show quick pick print("[QP Test]:", str(qp_numbers), str(qp_meganumber)) # Test odds # TODO: see if using numpy, set, compare, etc is more efficient. # TODO: Add check for duplicate drawings (from previous) print("Performing odds test...") draw_count = 0 loss = 0 zero_and_mega = 0 one_and_mega = 0 two_and_mega = 0
DRY_RUN = True url = "smtp.gmail.com" port = 465 username = "******" password = "******" pseudo_id = namedtuple('pseudo_id', ['pseudonym', 'code', 'cryptonym']) voter_list = voters_in_text.strip().splitlines() word_list = unique_words.strip().splitlines() random = SystemRandom() random_words = random.sample(word_list, len(voter_list)) random_keys = random.sample(code_range, len(voter_list)) print("There are %d voters in upcoming election:\n" % len(voter_list)) for voter in voter_list: print(voter) random.shuffle(voter_list) print() print("Distributing pseudonyms...\n") pseudo = []
ROLL_OF_TITLECASE, GENERAL_CASE, ) WORD_FILE = "./eff_large_wordlist.txt" WORD_DICT = {} NO_DICE_WORD_FILE = "./diceware8k.txt" NO_DICE_WORD_LIST = [] nums = tuple(map(str, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))) syms = ('&', '.', '-', '$', '_', '@', '!') mapper = { ROLL_OF_NUMBER: lambda x: ''.join(sys_rand.sample(nums, sys_rand.randint(2, 7))), ROLL_OF_TITLECASE: lambda x: x.title(), GENERAL_CASE: lambda x: x } WORDS_TO_GENERATE = 7 PASSPHRASES_TO_GENERATE = 20 map_target_list = [] for i in range(100): for k in VALID_CASES_WORD_CHAOS: if i <= k: map_target_list.append(k) break
words.add(l) wf.close() len_words = len(words) def count_choices(len_words, word_count): if word_count == 1: return len_words assert word_count > 1 return len_words * count_choices(len_words - 1, word_count - 1) choices = count_choices(len_words, word_count) choices_desc = '%2d*[%d words (%d-%d letters) from %s]' % ( word_count, len_words, min_word_len, max_word_len, ':'.join(words_paths), ) entropies.append((choices, choices_desc, )) if len(entropies) > 1: print 'Bit Entropy comparisons' entropies.sort() for (n, d, ) in entropies: print '%5.1f bits - %s' % (math.log(n, 2), d, ) random = SystemRandom() words = random.sample(words, word_count) for word in words: print word print word_separator.join(words) if __name__ == '__main__': main()
from json import dump from random import SystemRandom random = SystemRandom() k, n, d = 20, 120, 0.8 B = 2**(n / d) A = [random.randint(1, B) for _ in range(n)] s = sum(A[index] for index in random.sample(range(n), k)) dump((s, A), open("data", "w"))