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

Salt is the only spice. #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 25 additions & 3 deletions git/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getpass
import hashlib
import json
import pathlib
import random
Expand Down Expand Up @@ -27,8 +28,9 @@ def get_credentials():
def authenticate(username, pass_text, pwdb):
success = False
if username in pwdb:
# compare with stored password
if pass_text == pwdb[username]:
salt = pwdb[username]["salt"]
# calculate hash and compare with stored hash
if pwhash(pass_text, salt) == pwdb[username]["hash"]:
success = True
return success

Expand All @@ -37,9 +39,12 @@ def add_user(username, password, pwdb, pwdb_path):
if username in pwdb:
err(f'Username already exists [{username}]', 2)
else:
pwdb[username] = password
uniq_salt = get_salt()
pwdb[username] = {"hash": pwhash(password, uniq_salt),
"salt": uniq_salt}
write_pwdb(pwdb, pwdb_path)


def read_pwdb(pwdb_path):
# try to read from the database
# if anything happens, report the error!
Expand All @@ -58,6 +63,23 @@ def write_pwdb(pwdb, pwdb_path):
with open(pwdb_path, 'wt') as pwdb_file:
json.dump(pwdb, pwdb_file)


def pwhash(password, salt):
# encode to bytes
pass_text = password + salt
bpass = pass_text.encode('utf8')
return hashlib.sha256(bpass).hexdigest()


def get_salt():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are the comments?

salt = ""
for _ in range(5):
num = random.randrange(65, 123)
salt += chr(num)
return salt



if __name__ == '__main__':
# ask for credentials
username, password = get_credentials()
Expand Down
33 changes: 30 additions & 3 deletions hpp/parallel/overcommit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
import sys
import numpy as np

N = int(sys.argv[1]) if len(sys.argv) > 1 else 10000
x = np.zeros((N, N), dtype="float64")
import matplotlib.pyplot as plt
import threadpoolctl as th
import timeit

y = x @ x


# N = int(sys.argv[1]) if len(sys.argv) > 1 else 5000
# x = np.zeros((N, N), dtype="float64")

time_dict = {}

Ns = [1000, 2000, 3000, 4000, 5000]
# Ns = [1, 2, 5, 100, 500]

for n in Ns:
x = np.zeros((n, n), dtype="float64")
time_dict[n] = []
for limit_num in range(1,6):
with th.threadpool_limits(limits=limit_num, user_api='blas'):
time = timeit.timeit(lambda: x @ x, number=5)
time_dict[n].append(time)
print(f"N = {n}, threads = {limit_num}: {time} seconds")


for n in time_dict:
plt.plot(list(range(1,6)), time_dict[n], 'o', label=f"n={n}")
plt.xlabel("limit num")
plt.ylabel("seconds")
plt.legend()
plt.savefig("plot.jpg", dpi=600)
plt.show()
Binary file added hpp/parallel/plot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.