Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting #66

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions examples/hash_cracker_ray_ready.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import argparse
from datetime import datetime
import itertools
from typing import Optional
from datetime import datetime
from hashlib import sha256
from typing import Optional

# `test`: 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
# `foo`: 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
Expand All @@ -11,20 +11,25 @@
# `x`: 2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881

# the character table that we want to use to construct possible phrases
CHARS = [chr(c) for c in itertools.chain(
range(ord("a"), ord("z") + 1),
range(ord("A"), ord("Z") + 1),
range(ord("0"), ord("9") + 1),
range(ord(" "), ord("/") + 1),
range(ord(":"), ord("@") + 1),
range(ord("["), ord("`") + 1),
range(ord("{"), ord("~") + 1),
)]
CHARS = [
chr(c)
for c in itertools.chain(
range(ord("a"), ord("z") + 1),
range(ord("A"), ord("Z") + 1),
range(ord("0"), ord("9") + 1),
range(ord(" "), ord("/") + 1),
range(ord(":"), ord("@") + 1),
range(ord("["), ord("`") + 1),
range(ord("{"), ord("~") + 1),
)
]

start_time = datetime.now()

parser = argparse.ArgumentParser()
parser.add_argument("-l", "--length", type=int, default=3, help="brute force length, default: %(default)s")
parser.add_argument(
"-l", "--length", type=int, default=3, help="brute force length, default: %(default)s"
)
parser.add_argument("hash", type=str)
args = parser.parse_args()

Expand All @@ -44,12 +49,12 @@ def str_to_int(value: str) -> int:
base = len(CHARS) + 1
intval = 0
for position, digit in zip(itertools.count(), [CHARS.index(v) + 1 for v in reversed(value)]):
intval += digit * base ** position
intval += digit * base**position

return intval


def int_to_str(intval: int, round_nulls = False) -> Optional[str]:
def int_to_str(intval: int, round_nulls=False) -> Optional[str]:
"""
Convert an integer value back to the equivalent string.

Expand Down Expand Up @@ -97,5 +102,5 @@ def brute_force_range(start_string: str, end_string: str):

print(
f"finished in {datetime.now() - start_time},",
f"match found: {result}" if result else f"match not found"
f"match found: {result}" if result else f"match not found",
)
37 changes: 24 additions & 13 deletions examples/hash_cracker_without_ray.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import argparse
from datetime import datetime
from itertools import chain
from hashlib import sha256
from itertools import chain

# `test`: 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
# `foo`: 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
# `glm`: 2bf548d8056029c73e6e28132d19a3a277a49daf32b1c1ba7b0b7fc7e78bf5cd
# `be`: 46599c5bb5c33101f80cea8438e2228085513dbbb19b2f5ce97bd68494d3344d
# `x`: 2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881

CHARS = [chr(c) for c in chain(
range(ord("a"), ord("z") + 1),
range(ord("A"), ord("Z") + 1),
range(ord("0"), ord("9") + 1),
range(ord(" "), ord("/") + 1),
range(ord(":"), ord("@") + 1),
range(ord("["), ord("`") + 1),
range(ord("{"), ord("~") + 1),
)]
CHARS = [
chr(c)
for c in chain(
range(ord("a"), ord("z") + 1),
range(ord("A"), ord("Z") + 1),
range(ord("0"), ord("9") + 1),
range(ord(" "), ord("/") + 1),
range(ord(":"), ord("@") + 1),
range(ord("["), ord("`") + 1),
range(ord("{"), ord("~") + 1),
)
]

start_time = datetime.now()

parser = argparse.ArgumentParser()
parser.add_argument("-w", "--words", type=str, help="dictionary file")
parser.add_argument("-l", "--length", type=int, default=0, help="brute force length (if the `words` dictionary is not provided), default: %(default)s")
parser.add_argument(
"-l",
"--length",
type=int,
default=0,
help="brute force length (if the `words` dictionary is not provided), default: %(default)s",
)
parser.add_argument("hash", type=str)
args = parser.parse_args()

Expand All @@ -35,7 +44,6 @@ def word_file(words_file: str):


def brute_force(max_len: int):

def brute_force_len(length: int):
for char in CHARS:
if length <= 1:
Expand All @@ -62,4 +70,7 @@ def brute_force_len(length: int):
result = word.decode("utf-8")
break

print(f"finished in {datetime.now() - start_time},", f"match found: {result}" if result else f"match not found")
print(
f"finished in {datetime.now() - start_time},",
f"match found: {result}" if result else f"match not found",
)
35 changes: 20 additions & 15 deletions examples/mandelbrot_image.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import argparse
from datetime import datetime, timezone
import math
from typing import Tuple, NamedTuple
from datetime import datetime, timezone
from typing import NamedTuple, Tuple

from PIL import Image
import ray
from PIL import Image

ZOOM_BASE = 2.0

Expand Down Expand Up @@ -32,7 +32,6 @@ def mandel(x0, y0, max_iter):
return draw_pixel(i / max_iter)



def calculate_mandel(
tgt_range_x: Tuple[int, int],
tgt_range_y: Tuple[int, int],
Expand Down Expand Up @@ -66,7 +65,7 @@ def draw_mandelbrot(
y_range: Tuple[float, float],
max_iter: int,
num_chunks: int = 1,
use_ray: bool=True,
use_ray: bool = True,
):
chunks = list()

Expand Down Expand Up @@ -130,35 +129,40 @@ def draw_mandelbrot(
def argument_parser():
parser = argparse.ArgumentParser("mandelbrot on ray")
parser.add_argument(
"-s", "--size",
"-s",
"--size",
nargs=2,
metavar=("X", "Y"),
help="size of the output image, default=%(default)s",
type=int,
default=(500, 500),
)
parser.add_argument(
"-c", "--center",
"-c",
"--center",
nargs=2,
metavar=("X", "Y"),
help="center of the drawn region, default=%(default)s",
type=float,
default=(-.743643135, .131825963),
default=(-0.743643135, 0.131825963),
)
parser.add_argument(
"-z", "--zoom",
"-z",
"--zoom",
help="magnification of the drawn region, default=%(default)s",
type=float,
default=200000
default=200000,
)
parser.add_argument(
"-i", "--max-iterations",
"-i",
"--max-iterations",
help="maximum number of iterations to perform per pixel, default=%(default)s",
type=int,
default=500,
)
parser.add_argument(
"-n", "--num-chunks",
"-n",
"--num-chunks",
help="number of chunks to divide output into, default=%(default)s",
type=int,
default=16,
Expand All @@ -168,14 +172,15 @@ def argument_parser():
help="number of CPUs for ray to use",
type=int,
)
parser.add_argument("-r", "--use-ray", action='store_true', help="use ray")
parser.add_argument("-r", "--use-ray", action="store_true", help="use ray")
parser.add_argument(
"-R", "--no-use-ray", dest='use_ray', action='store_false', help="don't use ray"
"-R", "--no-use-ray", dest="use_ray", action="store_false", help="don't use ray"
)
parser.set_defaults(use_ray=True)

return parser


####


Expand All @@ -196,7 +201,7 @@ def argument_parser():
x_range=(args.center[0] - ZOOM_BASE / args.zoom, args.center[0] + ZOOM_BASE / args.zoom),
y_range=(
args.center[1] - ZOOM_BASE / (args.zoom * aspect_ratio),
args.center[1] + ZOOM_BASE / (args.zoom * aspect_ratio)
args.center[1] + ZOOM_BASE / (args.zoom * aspect_ratio),
),
max_iter=args.max_iterations,
num_chunks=args.num_chunks,
Expand Down