MCPcopy Create free account
hub / github.com/Bashfuscator/Bashfuscator / randUniqueStr

Method randUniqueStr

bashfuscator/core/engine/random.py:158–200  ·  view source on GitHub ↗

Generate a random string that is guaranteed to be unique. :param minStrLen: minimum length of generated string :type minStrLen: int :param maxStrLen: maximum length of generated string :type maxStrLen: int :param charList: list of characters that wil

(self, minStrLen=None, maxStrLen=None, charList=None, escapeChars="", noBOBL=True)

Source from the content-addressed store, hash-verified

156 return randomVar
157
158 def randUniqueStr(self, minStrLen=None, maxStrLen=None, charList=None, escapeChars="", noBOBL=True):
159 """
160 Generate a random string that is guaranteed to be unique.
161
162 :param minStrLen: minimum length of generated string
163 :type minStrLen: int
164 :param maxStrLen: maximum length of generated string
165 :type maxStrLen: int
166 :param charList: list of characters that will be used when
167 generating the random string. If it is not specified, the
168 default character set will be used
169 :type charList: str or list of chrs
170 :returns: unique random string
171
172 .. note::
173 Runtime will increase incrementally as more and more unique
174 strings are generated, unless
175 :meth:`~RandomGen.forgetUniqueStrs` is called.
176 """
177 minStrLen, maxStrLen = self._getSizes(minStrLen, maxStrLen)
178
179 if charList is None:
180 charList = RandomGen._randStrCharList
181
182 commonStrNum = 0
183
184 while True:
185 randStr = self.randGenStr(minStrLen, maxStrLen, charList, escapeChars, noBOBL)
186
187 if randStr not in RandomGen._uniqueRandStrs:
188 break
189 else:
190 commonStrNum += 1
191 # if 5 collisions are generated in a row, chances are that we are reaching the upper bound
192 # of our keyspace, so make the keyspace bigger so we can keep generating unique strings
193 if commonStrNum == 5:
194 minStrLen = maxStrLen
195 maxStrLen += 1
196 commonStrNum = 0
197
198 RandomGen._uniqueRandStrs.add(randStr)
199
200 return randStr
201
202 def randGenStr(self, minStrLen=None, maxStrLen=None, charList=None, escapeChars="", noBOBL=True):
203 """

Callers 3

mutateMethod · 0.80
setSizesMethod · 0.80
mutateMethod · 0.80

Calls 2

_getSizesMethod · 0.95
randGenStrMethod · 0.95

Tested by

no test coverage detected