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): pt = self.l_parse_tree(pw) code_g = [self.encode_rule(*p) for p in pt] extra = hny_config.PASSWORD_LENGTH - len(code_g); code_g.extend([convert2group(0,1) for x in range(extra)]) return code_g
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_grammar(self, G): # Encode sub-grammar vd = VaultDistribution() stack = ['G'] code_g = [] done = [] while stack: head = stack.pop() assert head not in done done.append(head) rule_dict = G[head] t_set = [] for rhs, f in rule_dict.items(): if rhs != '__total__': r = filter(lambda x: x not in done+stack, self.G.get_actual_NonTlist(head, rhs)) if r: for x in r: if (x not in t_set): t_set.append(x) t_set.reverse() stack.extend(t_set) n = len(rule_dict.keys())-1 code_g.append(vd.encode_vault_size(head, n)) if n<0: print "Sorry I cannot encode your password! Please choose" print "something different, password12" exit(0) assert n == vd.decode_vault_size(head, code_g[-1]) code_g.extend([self.encode(head, r) for r in rule_dict.keys() if r != '__total__']) extra = hny_config.HONEY_VAULT_GRAMMAR_SIZE - len(code_g); code_g.extend([convert2group(0,1) for x in range(extra)]) return code_g
def encode_rule(self, l, r): rhs_dict = self.G[l] i = rhs_dict.keys().index(r) assert i >= 0 l_pt = sum(rhs_dict.values()[:i]) r_pt = l_pt + rhs_dict[r] return convert2group(random.randint(l_pt, r_pt), rhs_dict['__total__'])
def encode_rule(self, l, r): rhs_dict = self.G[l] i = rhs_dict.keys().index(r) assert i >= 0 l_pt = sum(rhs_dict.values()[:i]) r_pt = l_pt + rhs_dict[r] return convert2group(random.randint(l_pt,r_pt), rhs_dict['__total__'])
def generate_and_encode_password(self, size=10): N = [random.randint(0, MAX_INT) for i in range(size+1)] N[0] += (size - N[0] % self.MAX_ALLOWED) P = [s[N[i+1]%len(s)] for i,s in enumerate(self.must_set)] 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 Encode_spcl( m, grammar ): print "Special Encoding::::", m return None # TODO W = m # break_into_words(m, trie) P = ['%s%d' % (whatchar(w), len(w)) for w in W ] E = []; for w,p in zip(W[:-1], P[:-1]): E.append( getVal( grammar['S'], p+',S') ) E.append( getVal( grammar[ p ], w ) ); E.append( getVal( grammar[ 'S' ], P[-1])) E.append( getVal( grammar[P[-1]], W[-1])); if PASSWORD_LENGTH>0: extra = PASSWORD_LENGTH - len(E); E.extend( [ convert2group(0,1) for x in range(extra) ] ) return E;
def main(): #print T.tokenize('P@$$w0rd', True) #exit(0) dte = DTE() scanner = Scanner() print "Resource:", resource.getrusage(resource.RUSAGE_SELF).ru_maxrss; #p='(NH4)2Cr2O7' # sys.stdin.readline().strip() p=u'iloveyou69' c = Encode(p, scanner, dte); #print "Encoding:", c m = Decode(c, dte); # print "After Decoding:", m #return # if PASSWORD_LENGTH>0: for s in range(0000): E = [] E.extend( [ convert2group(0,1) for x in range(PASSWORD_LENGTH) ] ) c_struct = struct.pack('%sI' % len(E), *E ) m = Decode(c_struct, dte); print s, ":", m
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 encode_pw(self, pw): pt = self.l_parse_tree(pw) code_g = [self.encode_rule(*p) for p in pt] extra = hny_config.PASSWORD_LENGTH - len(code_g) code_g.extend([convert2group(0, 1) for x in range(extra)]) return code_g