def test_DTE(self): vault_file = hny_config.VAULT_FILE + '~test' mpw = 'Masterpassw0rd' + str(random.randint(0, 100)) hv = HoneyVault(vault_file, mpw) PW_set = dict((i, hv.dte.decode_pw(pw_encodings)) for i, pw_encodings in enumerate(hv.S) if hv.machine_pass_set[i] == '0') for i, pw in list(PW_set.items()): s = hv.dte.encode_pw(pw) tpw = hv.dte.decode_pw(s) self.assertEqual( tpw, pw, "Encode-decode pw is wrong. {}: {} ----> {}".format( i, pw, tpw)) print("All encode-decoing test passed") return hv.add_password( {'google.com': 'password' + str(random.randint(0, 1000))}) ignore = set([hv.get_domain_index('google.com')]) for i, pw_encodings in enumerate(hv.S): if hv.machine_pass_set[i] == '0' and i not in ignore: npw = hv.dte.decode_pw(pw_encodings) self.assertEqual( PW_set[i], npw, "New passwords changed!!! Expecting: {!r} at {}. Got {!r}". format(PW_set[i], i, npw))
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 getVal(arr, val): """ arr -> is a list of values of type same as 'val' and also has a frequency e.g. arr: [['a',0,123], ['asd', 1, 31], ['ADR', 1, 345]] val -> is a value of same type of values in arr, e.g. 'asd' returns: a random number between, cumulative probability of 'asd' and the element just before it, i.e. 'a'. """ c = 0 found = False totalC = 0; t = -1; for i, x in enumerate(arr): # print x c += x[2] totalC += x[2] if not found and x[0] == val: if i == 0: a = 0; else: a = c - x[2] t = random.randint(a, c - 1) found = True print(x, t) if t > -1: # to deal with floating this is used, basically converted the numebrs in # (mod totalC) ASSUMPTION: max value of sum of frequency is # 4,294,967,295, i.e. ~ 4.29 billion!! return convert2group(t, totalC) return t
def encode_pw(self, pw): p = list(pw[:]) must_4 = [DTE_random.get_char(p, m) for m in must_set] for x in must_4: del p[x[0]] pw_random_order = DTE_random.decode2string( random.randint(0, self.fact_map[len(p) - 1]), p) code_g = [random.randint(0, MAX_INT/self.MAX_ALLOWED) * \ self.MAX_ALLOWED + len(pw)] for i, x in enumerate(must_4): code_g.append(DTE_random.get_random_for_this(x[1], must_set[i])) for p in pw_random_order: code_g.append(DTE_random.get_random_for_this(p, All)) code_g.append(encode2number(pw)) extra = hny_config.PASSWORD_LENGTH - len(code_g) code_g.extend([convert2group(0, 1) for x in range(extra)]) return code_g
def encode2number(s): sorted_s = sorted(s) n = len(s) code = 0 for i, ch in enumerate(s): l = sorted_s.index(ch) code += l * DTE_random.fact_map[n - i - 2] del sorted_s[l] p = random.randint(0, MAX_INT / DTE_random.fact_map[n - 1]) return DTE_random.fact_mapp[n - 1] * p + code
def encode_vault_size(self, lhs, n): v = self.G.get(lhs, {}) n = str(n) try: i = v.keys().index(n) x = sum(v.values()[:i]) y = x + v.values()[i] except ValueError: return convert2group(0, v['__total__']) return convert2group(random.randint(x, y-1), v['__total__'])
def encode_pw(self, pw): p = list(pw[:]) must_4 = [DTE_random.get_char(p, m) for m in must_set] for x in must_4: del p[x[0]] pw_random_order = DTE_random.decode2string( random.randint(0, self.fact_map[len(p) - 1]), p) code_g = [random.randint(0, MAX_INT / self.MAX_ALLOWED) * \ self.MAX_ALLOWED + len(pw)] for i, x in enumerate(must_4): code_g.append(DTE_random.get_random_for_this( x[1], self.must_set[i])) for p in pw_random_order: code_g.append(DTE_random.get_random_for_this( p, self.All)) code_g.append(self.encode2number(pw)) extra = hny_config.PASSWORD_LENGTH - len(code_g); code_g.extend([convert2group(0, 1) for x in range(extra)]) return code_g
def test_DTE(self): vault_file = VAULT_FILE+'~test' mpw = 'Masterpassw0rd'+str(random.randint(0,100)) hv = HoneyVault(vault_file, mpw) PW_set = dict((i, hv.dte.decode_pw(pw_encodings)) for i, pw_encodings in \ enumerate(hv.S) if hv.machine_pass_set[i]=='0') for i, pw in PW_set.items(): s = hv.dte.encode_pw(pw) tpw = hv.dte.decode_pw(s) self.assertEqual(tpw, pw, "Encode-decode pw is wrong. {}: {} ----> {}".format(i, pw, tpw)) print "All encode-decoing test passed" return hv.add_password({'google.com': 'password' + str(random.randint(0, 1000))}) ignore = set([hv.get_domain_index('google.com')]) for i, pw_encodings in enumerate(hv.S): if hv.machine_pass_set[i] == '0' and i not in ignore: npw = hv.dte.decode_pw(pw_encodings) self.assertEqual(PW_set[i], npw, "New passwords changed!!!"\ "Expecting: '{}' at {}. Got '{}'".format(PW_set[i], npw))
def test_enc_dec_vault_size(self): vd = pcfg.VaultDistPCFG() X = vd.encode_vault_size('D', 3) t = vd.decode_vault_size('D', X) self.assertEqual(t, 3, str((X, t))) for i in range(25): k = random.randint(1,pcfg.MAX_ALLOWED) lhs = random.choice(vd.G.keys()) e = vd.encode_vault_size(lhs, k) self.assertEqual(vd.decode_vault_size(lhs, e), k, "VaultSizeDecodingError:"\ " Expecting: {} for (lhs: {}, e:{}), but decoded to: {}"\ .format(k, lhs, e, vd.decode_vault_size(lhs, e)))
def test_enc_dec_vault_size(self): vd = pcfg.VaultDistPCFG() X = vd.encode_vault_size('D', 3) t = vd.decode_vault_size('D', X) self.assertEqual(t, 3, str((X, t))) for i in range(25): k = random.randint(1, pcfg.MAX_ALLOWED) lhs = random.choice(vd.G.keys()) e = vd.encode_vault_size(lhs, k) self.assertEqual(vd.decode_vault_size(lhs, e), k, "VaultSizeDecodingError:"\ " Expecting: {} for (lhs: {}, e:{}), but decoded to: {}"\ .format(k, lhs, e, vd.decode_vault_size(lhs, e)))
def encode_rule(self, l, r): rhs_dict = self.G[l] try: i = rhs_dict.keys().index(r) if DEBUG: c = rhs_dict.keys()[i] assert c==r, "The index is wrong" except ValueError: print "'{}' not in the rhs_dict (l: '{}', rhs_dict: {})".format(r, l, self.G[l]) raise ValueError l_pt = sum(rhs_dict.values()[:i]) r_pt = l_pt + rhs_dict[r]-1 return convert2group(random.randint(l_pt,r_pt), rhs_dict['__total__'])
def encode_rule(self, l, r): rhs_dict = self.G[l] try: i = list(rhs_dict.keys()).index(r) if DEBUG: c = list(rhs_dict.keys())[i] assert c == r, "The index is wrong" except ValueError: raise ValueError("'{}' not in the rhs_dict (l: '{}')".format(r, l)) l_pt = sum(list(rhs_dict.values())[:i]) r_pt = l_pt + rhs_dict[r] - 1 assert l_pt <= r_pt, "Rule with zero freq! rhs_dict[{}] = {} (l={})\n{}"\ .format(r, rhs_dict, l, self.G) return convert2group(random.randint(l_pt, r_pt), rhs_dict['__total__'])
def generate_random_password(self, size=10): """ Generates random password of given size it ensures - 1 uppercase 1 lowercase 1 digit 1 punc """ P = [random.choice(s) for s in self.must_set] P.extend([random.choice(self.All) for n in range(size - len(self.must_set))]) n = random.randint(0, MAX_INT) password = self.decode2string(n, P) return password
def encode_rule(self, l, r): rhs_dict = self.G[l] try: i = list(rhs_dict.keys()).index(r) if DEBUG: c = list(rhs_dict.keys())[i] assert c == r, "The index is wrong" except ValueError: raise ValueError("'{}' not in the rhs_dict (l: '{}')" .format(r, l)) l_pt = sum(list(rhs_dict.values())[:i]) r_pt = l_pt + rhs_dict[r] - 1 assert l_pt <= r_pt, "Rule with zero freq! rhs_dict[{}] = {} (l={})\n{}"\ .format(r, rhs_dict, l, self.G) return convert2group(random.randint(l_pt, r_pt), rhs_dict['__total__'])
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 get_random_for_this(c, arr): i = arr.index(c) n = len(arr) return random.randint(0, MAX_INT / n) * n + t