def insert_number_one(word_list, limit=100): index_to_insert = randrange(len(word_list)) if word_list[index_to_insert].isalpha(): if index_to_insert < 1 or word_list[index_to_insert - 1].isalpha(): word_list.insert(index_to_insert, str(randrange(limit))) return word_list
def shorten_one(word_list, max_length=3): word_index = randrange(len(word_list)) if word_list[word_index].isalpha(): length = 1 + randrange(max_length - 1) new_word = word_list[word_index][:length] return [ word if i != word_index else new_word for i, word in enumerate(word_list) ] else: return word_list
def switch_case_one(word_list): word_index = randrange(len(word_list)) char_index = randrange(len(word_list[word_index])) char_to_substitute = word_list[word_index][char_index] char_substituted = char_to_substitute.upper( ) if char_to_substitute.islower() else char_to_substitute.lower() new_word_list = [] for word in word_list: if word != word_list[word_index]: new_word_list.append(word) else: new_word_list.append(word[:char_index] + char_substituted + word[char_index + 1:]) return new_word_list
def length(self, min_length, max_length): """ :param min_length: :param max_length: :return: >>> PronounceableWord().length(6, 10) 'centte' """ min_length = min_length or self.min_length max_length = max_length or self.max_length if not min_length and not max_length: return # When munging characters, we need to know where to start counting # letters from length = min_length + randrange(max_length - min_length) char = self._generate_nextchar(self.total_sum, self.start_freq) a = ord('a') word = chr(char + a) for i in range(1, length): char = self._generate_nextchar(self.row_sums[char], DIGRAPHS_FREQUENCY[char]) word += chr(char + a) return word
def insert_symbol_one(word_list): index_to_insert = randrange(len(word_list)) if word_list[index_to_insert].isalpha(): if index_to_insert < 1 or word_list[index_to_insert - 1].isalpha(): word_list.insert(index_to_insert, choice(string.punctuation)) return word_list
def leetify_one(self, word_list): word_index = randrange(len(word_list)) char_index = randrange(len(word_list[word_index])) char_to_substitute = word_list[word_index][char_index] char_substituted = choice( self.leetspeak.get(char_to_substitute.lower(), [char_to_substitute])) new_word_list = [] for word in word_list: if word != word_list[word_index]: new_word_list.append(word) else: new_word_list.append(word[:char_index] + char_substituted + word[char_index + 1:]) return new_word_list
def _generate_nextchar(self, all, freq): i, pos = 0, randrange(all) while i < (len(freq) - 1) and pos >= freq[i]: pos -= freq[i] i += 1 return i
def conformize(self, word_list, timeout=3, weak=False): """ :param list word_list: :param float timeout: :param bool weak: :return str | None: >>> Conformize().conformize(['unlikely', 'piezo', 'electric', 'grounds']) ';U$Piezo33lGrounds' """ word_list = self.modify.title_case_all(word_list) if not weak: word_list = self.modify.insert_number_one(word_list) word_list = self.modify.insert_symbol_one(word_list) else: index_to_insert = randrange(len(word_list)) word_list.insert(index_to_insert, ''.join( [choice(string.punctuation) for _ in range(self.policy.policy['punctuation_count'])])) index_to_insert = randrange(len(word_list)) word_list.insert(index_to_insert, ''.join( [choice(string.digits) for _ in range(self.policy.policy['digit_count'])])) while not self.policy.length(''.join(word_list)): word_list = self.modify.shorten_one(word_list) start = time() while time()-start < timeout: if not self.policy.length(''.join(word_list)): word_list = self.modify.shorten_one(word_list) elif not self.policy.is_conform(''.join(word_list)): word_list = choice([self.modify.insert_symbol_one(word_list), self.modify.switch_case_one(word_list), self.modify.leetify_one(word_list)]) else: return ''.join(word_list) print('Non-conformed password:'******''.join(word_list)) return None
import time from copy import deepcopy # This is a standalone performance test of the algorithm used in gcylc to # sort namespaces into "definition order", i.e. the order in which they are # defined in the suite.rc file. # Number of namespaces. N = 10000 # N names of length 5-15 characters each (c.f. namespaces in "definition # order"). names = [] for i in range(0, N): names.append(''.join(secrets.choice(string.ascii_letters) for n in range(5 + secrets.randrange(10)))) # N lists with 2-7 names each (c.f. tree view paths of the inheritance # hierarchy). paths1 = [] for i in range(0, N): p = [] for j in range(0, 2 + secrets.randrange(6)): z = secrets.randrange(0, N) p.append(names[z]) paths1.append(p) paths2 = deepcopy(paths1) # Alphanumeric sort. s = time.time()