Skip to content

Commit

Permalink
Merge pull request #36 from rad10/bug-fixes
Browse files Browse the repository at this point in the history
Added a few more test cases (#26 )
Fixed several unnoticed bugs
  • Loading branch information
rad10 authored Feb 18, 2022
2 parents 80388b1 + 6a830ef commit cb68eae
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
46 changes: 41 additions & 5 deletions brutesleuth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,15 @@ def __init__(self, length: int):
super().__init__(10, length)

def __next__(self) -> str:
return f"{super().__next__():0{self.length}d}"
return f"{super().__next__():0{self.length:d}d}"

def getRandom(self) -> str:
"""Returns a random configuration of the decimalchain. This can be useful
if used to implement a monkey sort brute forcer, or to create a random
password based on rulesets
"""
# Creating random values in memory
return f"{randrange(0, self.__len__()):0{self.length:d}d}"


class HexadecimalChain(BaseChain):
Expand All @@ -216,7 +224,15 @@ def __init__(self, length: int):
super().__init__(16, length)

def __next__(self) -> str:
return f"{super().__next__():0{self.length}x}"
return f"{super().__next__():0{self.length:d}x}"

def getRandom(self) -> str:
"""Returns a random configuration of the hexadecimalchain. This can be useful
if used to implement a monkey sort brute forcer, or to create a random
password based on rulesets
"""
# Creating random values in memory
return f"{randrange(0, self.__len__()):0{self.length:d}x}"


class OctalChain(BaseChain):
Expand All @@ -236,7 +252,15 @@ def __init__(self, length: int):
super().__init__(8, length)

def __next__(self) -> str:
return f"{super().__next__():0{self.length}d}"
return f"{super().__next__():0{self.length:d}o}"

def getRandom(self) -> str:
"""Returns a random configuration of the octalchain. This can be useful
if used to implement a monkey sort brute forcer, or to create a random
password based on rulesets
"""
# Creating random values in memory
return f"{randrange(0, self.__len__()):0{self.length:d}o}"


class BinaryChain(BaseChain):
Expand All @@ -256,7 +280,15 @@ def __init__(self, length: int):
super().__init__(2, length)

def __next__(self) -> str:
return f"{super().__next__():0{self.length}d}"
return f"{super().__next__():0{self.length:d}b}"

def getRandom(self) -> str:
"""Returns a random configuration of the binarychain. This can be useful
if used to implement a monkey sort brute forcer, or to create a random
password based on rulesets
"""
# Creating random values in memory
return f"{randrange(0, self.__len__()):0{self.length:d}b}"


class iterative_product:
Expand Down Expand Up @@ -551,12 +583,16 @@ def set_position(format_string: str, starting_string: str,
# Collecting variations between generators
values = get_string_variations(format_string, starting_string)

# pack values anyway if its only a string
if type(values) is str:
values = values,

# setting each portion
for i in range(len(generators)):
if type(generators[i]) == BruteChain:
generators[i].setIndex(values[i])
else:
generators[i].setIndex(int(values[i]))
generators[i].setIndex(int(values[i], generators[i].base))

return generators

Expand Down
30 changes: 28 additions & 2 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def test_incremental_result(self, length, base):
for result, expected in zip(chain, range(base ** length)):
assert result == expected

def test_get_random(self, length, base):
chain = brutesleuth.BaseChain(base, length)
for i in range(10):
assert(0 <= chain.getRandom() < base ** length)


@pytest.mark.parametrize("length", [
2, 3, 5, 8, 10
Expand All @@ -62,6 +67,11 @@ def test_incremental_result(self, length):
lambda i: f"{i:0{length:d}d}", range(2 ** length))):
assert result == expected

def test_get_random(self, length):
chain = brutesleuth.DecimalChain(length)
for i in range(10):
assert(0 <= int(chain.getRandom(), 10) < 10 ** length)


@pytest.mark.parametrize("length", [
2, 3, 5, 8, 10
Expand All @@ -84,6 +94,11 @@ def test_incremental_result(self, length):
lambda i: f"{i:0{length:d}x}", range(2 ** length))):
assert result == expected

def test_get_random(self, length):
chain = brutesleuth.HexadecimalChain(length)
for i in range(10):
assert(0 <= int(chain.getRandom(), 16) < 16 ** length)


@pytest.mark.parametrize("length", [
2, 3, 5, 8, 20
Expand All @@ -106,6 +121,11 @@ def test_incremental_result(self, length):
lambda i: f"{i:0{length:d}o}", range(2 ** length))):
assert result == expected

def test_get_random(self, length):
chain = brutesleuth.OctalChain(length)
for i in range(10):
assert(0 <= int(chain.getRandom(), 8) < 8 ** length)


@pytest.mark.parametrize("length", [
4, 5, 6, 8, 10, 20
Expand All @@ -128,6 +148,11 @@ def test_incremental_result(self, length):
lambda i: f"{i:0{length:d}b}", range(2 ** length))):
assert result == expected

def test_get_random(self, length):
chain = brutesleuth.BinaryChain(length)
for i in range(10):
assert(0 <= int(chain.getRandom(), 2) < 2 ** length)


@pytest.mark.skip(reason="WIP need to complete")
class TestIterativeProduct:
Expand Down Expand Up @@ -200,8 +225,9 @@ def test_get_string_variations(format_string, real_string, expected_results):

@pytest.mark.parametrize("format_string,set_string", [
("SKY-{4aA}-{4d}", "SKY-Aefa-0146"),
# ("Password{:2d}", "Password83"),
# ("Binary{:04b}", "Binary0100")
("Hash{:02d}-{:04b}", "Hash02-1100"),
("Password{:2d}", "Password83"),
("Binary{:04b}", "Binary0100")
])
def test_set_position(format_string, set_string):
format_frame, gens = brutesleuth.init_formatting(format_string)
Expand Down

0 comments on commit cb68eae

Please sign in to comment.