def initialize_vault(self, mp): vd = VaultDistPCFG() if not os.path.exists(self.vault_fl): print_production("\nCould not find the vault file @ {}, so, sit tight, " \ "creating a dummy vault for you." \ "\nShould not take too long...\n".format(self.vault_fl)) t_s = random.randints(0, MAX_INT, hny_config.HONEY_VAULT_ENCODING_SIZE) self.H = t_s[:hny_config.HONEY_VAULT_GRAMMAR_SIZE] t_s = t_s[hny_config.HONEY_VAULT_GRAMMAR_SIZE:] self.S = [ t_s[i:i + hny_config.PASSWORD_LENGTH] for i in range(0, self.s * hny_config.PASSWORD_LENGTH, hny_config.PASSWORD_LENGTH) ] assert all( len(s) == hny_config.PASSWORD_LENGTH for s in self.S ), "All passwords encodings are not of correct length.\n {}".format( (len(s), hny_config.PASSWORD_LENGTH) for s in self.S) self.machine_pass_set = list('0' * (self.mpass_set_size * 8)) k = int(math.ceil(hny_config.HONEY_VAULT_STORAGE_SIZE * \ hny_config.MACHINE_GENRATED_PASS_PROB / 1000.0)) for i in random.sample( list(range(hny_config.HONEY_VAULT_STORAGE_SIZE)), k): self.machine_pass_set[i] = '1' self.salt = os.urandom(8) self.save(mp) else: self.load(mp)
def test_random(self): for l in [1, 2, 5, 10, 100]: a = random.randints(0, 1, l) self.assertEqual(len(a), l, "Not returning correct length, should be {}"\ "returning {}".format(l, len(a))) self.assertTrue(all(x >= 0 and x < 1 for x in a), "All values are not in limit")
def generate_and_encode_password(self, size=10): """ Generates and encodes a password of size @size. first choose size+1 random numbers, where first one is to encode the length, and the rest are to encode the passwords """ assert size<self.MAX_ALLOWED, "Size of asked password={}, which is bigger"\ "than supported {}".format(size, self.MAX_ALLOWED) size = max(size, self.MIN_PW_LENGTH) N = random.randints(0, MAX_INT, size + 1) N[0] += (size - self.MIN_PW_LENGTH) - N[0] % ( self.MAX_ALLOWED - self.MIN_PW_LENGTH ) # s.t., N[0] % MAX_ALLOWED = size P = [s[N[i + 1] % len(s)] for i, s in enumerate(self.must_set)] # must set characters P.extend(self.All[n % len(self.All)] for n in N[len(self.must_set) + 1:]) n = random.randint(0, MAX_INT) password = self.decode2string(n, P) N.append(n) extra = hny_config.PASSWORD_LENGTH - len(N) N.extend([convert2group(0, 1) for x in range(extra)]) return password, N
def test_encode_decode_pw(self): H = random.randints(0, MAX_INT, hny_config.HONEY_VAULT_GRAMMAR_SIZE) tr_pcfg = pcfg.TrainedGrammar() G = tr_pcfg.decode_grammar(H) for i, pw in enumerate(random.sample(RANDOM_PW_SET, 30)): pwprime = G.decode_pw(G.encode_pw(pw)) self.assertEqual(pwprime, pw, "Password encode-decode error!\n"\ "EncodedPw={}, DecodedPw={}".format(pw, pwprime))
def test_encode_decode_subgrammar(self): H = random.randints(0, MAX_INT, hny_config.HONEY_VAULT_GRAMMAR_SIZE) tr_pcfg = pcfg.TrainedGrammar() G = tr_pcfg.decode_grammar(H) for i in range(10): Hprime = tr_pcfg.encode_grammar(G) Gprime = tr_pcfg.decode_grammar(Hprime) self.assertEqual(Gprime, G, "G and Gprime are not equal.\n"\ "G: {}\nGprime: {}\Difference: o-n = {}, n-o = {}"\ .format(G.nonterminals(), Gprime.nonterminals(), diff(G, Gprime), diff(Gprime, G)))
def test_encode_decode_rule(self): H = random.randints(0, MAX_INT, hny_config.HONEY_VAULT_GRAMMAR_SIZE) tr_pcfg = pcfg.TrainedGrammar() G = tr_pcfg.decode_grammar(H) for i, pw in enumerate(random.sample(RANDOM_PW_SET, 10)): pt = tr_pcfg.l_parse_tree(pw) for p in pt: t = tr_pcfg.encode_rule(*p) c = tr_pcfg.decode_rule(p[0], t) assert p[1] == c, "Decoding {} we got {}. Expecting {}"\ .format(t, c, p[1])
def initialize_vault(self, mp): vd = VaultDistPCFG() if not os.path.exists(self.vault_fl): print_production("\nCould not find the vault file @ {}, so, sit tight, "\ "creating a dummy vault for you."\ "\nShould not take too long...\n".format(self.vault_fl)) t_s = random.randints(0, MAX_INT, hny_config.HONEY_VAULT_ENCODING_SIZE) self.H = t_s[:hny_config.HONEY_VAULT_GRAMMAR_SIZE] t_s = t_s[hny_config.HONEY_VAULT_GRAMMAR_SIZE:] self.S = [t_s[i:i+hny_config.PASSWORD_LENGTH] for i in range(0, self.s*hny_config.PASSWORD_LENGTH, hny_config.PASSWORD_LENGTH)] assert all(len(s)==hny_config.PASSWORD_LENGTH for s in self.S), "All passwords encodings are not of correct length.\n {}".format((len(s), hny_config.PASSWORD_LENGTH) for s in self.S) self.machine_pass_set = list('0'*(self.mpass_set_size*8)) k = int(math.ceil(hny_config.HONEY_VAULT_STORAGE_SIZE * \ hny_config.MACHINE_GENRATED_PASS_PROB/1000.0)) for i in random.sample(range(hny_config.HONEY_VAULT_STORAGE_SIZE), k): self.machine_pass_set[i] = '1' self.salt = os.urandom(8) self.save(mp) else: self.load(mp)
def generate_and_encode_password(self, size=10): """ Generates and encodes a password of size @size. first choose size+1 random numbers, where first one is to encode the length, and the rest are to encode the passwords """ assert size<self.MAX_ALLOWED, "Size of asked password={}, which is bigger"\ "than supported {}".format(size, self.MAX_ALLOWED) size = max(size, self.MIN_PW_LENGTH) N = random.randints(0, MAX_INT, size+1) N[0] += (size - self.MIN_PW_LENGTH) - N[0] % (self.MAX_ALLOWED-self.MIN_PW_LENGTH) # s.t., N[0] % MAX_ALLOWED = size P = [s[N[i+1]%len(s)] for i,s in enumerate(self.must_set)] # must set characters P.extend(self.All[n%len(self.All)] for n in N[len(self.must_set)+1:]) n = random.randint(0, MAX_INT) password = self.decode2string(n, P) N.append(n) extra = hny_config.PASSWORD_LENGTH - len(N); N.extend([convert2group(0,1) for x in range(extra)]) return password, N
def __init__(self, *args, **kwargs): super(TestPCFG, self).__init__(*args, **kwargs) self.H = random.randints(0, MAX_INT, hny_config.HONEY_VAULT_GRAMMAR_SIZE) self.tr_pcfg = pcfg.TrainedGrammar() self.G = self.tr_pcfg.decode_grammar(self.H)
def test_random(self): for l in [1, 2, 5, 10, 100]: a = random.randints(0, 1, l) self.assertEqual(len(a), l, "Not returning correct length, should be {}"\ "returning {}".format(l, len(a))) self.assertTrue(all(x>=0 and x<1 for x in a), "All values are not in limit")