From ed046731a7cfddd28776180a96d0eda4be25b252 Mon Sep 17 00:00:00 2001 From: 332FG Raven <332fg.raven@gmail.com> Date: Tue, 27 Feb 2024 15:34:43 -0800 Subject: [PATCH] Moved password_hash to dedicated file * Passwords can be set for units as well as ground control roles therefore it's more suitable to have password_hash implemented as a utility. --- dcs/password.py | 38 ++++++++++++++++++++++++++++++++++++++ dcs/unitgroup.py | 32 ++------------------------------ 2 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 dcs/password.py diff --git a/dcs/password.py b/dcs/password.py new file mode 100644 index 00000000..3f47cd78 --- /dev/null +++ b/dcs/password.py @@ -0,0 +1,38 @@ +""" +The password module generates DCS compliant passwords from input string +""" + +import hashlib +import base64 +import string +import random + + +def password_hash(password: str) -> str: + # see https://www.reddit.com/r/hoggit/comments/uf2sh0/psa_creating_the_new_slot_passwords_outside_of_dcs/ + + # encode from https://github.com/simonwhitaker/base64url + def encode(b: bytes, trim: bool = False, break_at: int = 0) -> str: + encoded_string = base64.urlsafe_b64encode(b).decode("utf-8") + if trim: + encoded_string = encoded_string.rstrip("=") + + if break_at > 0: + i = 0 + result = "" + while i < len(encoded_string): + result += encoded_string[i: i + break_at] + "\n" + i += break_at + return result + else: + return encoded_string + + SALT_SIZE = 11 + DIGEST_SIZE = 32 + + key = ''.join(random.sample(string.digits + string.ascii_letters, SALT_SIZE)) + p_hash = hashlib.blake2b(key=key.encode(), digest_size=DIGEST_SIZE) + p_hash.update(password.encode()) + b64url_hash = encode(p_hash.digest(), trim=True) + + return key + ":" + b64url_hash diff --git a/dcs/unitgroup.py b/dcs/unitgroup.py index f16f949a..425698ed 100644 --- a/dcs/unitgroup.py +++ b/dcs/unitgroup.py @@ -20,9 +20,7 @@ import dcs.condition as condition import dcs.task as task import dcs.mapping as mapping -import hashlib -import base64 -import string +from dcs.password import password_hash PointT = TypeVar("PointT", bound=StaticPoint) UnitT = TypeVar("UnitT", bound=Unit) @@ -65,33 +63,7 @@ def add_point(self, point: PointT) -> None: self.points.append(point) def set_password(self, password: str) -> None: - # see https://www.reddit.com/r/hoggit/comments/uf2sh0/psa_creating_the_new_slot_passwords_outside_of_dcs/ - - # encode from https://github.com/simonwhitaker/base64url - def encode(b: bytes, trim: bool = False, break_at: int = 0) -> str: - encoded_string = base64.urlsafe_b64encode(b).decode("utf-8") - if trim: - encoded_string = encoded_string.rstrip("=") - - if break_at > 0: - i = 0 - result = "" - while i < len(encoded_string): - result += encoded_string[i: i + break_at] + "\n" - i += break_at - return result - else: - return encoded_string - - SALT_SIZE = 11 - DIGEST_SIZE = 32 - - key = ''.join(random.sample(string.digits + string.ascii_letters, SALT_SIZE)) - p_hash = hashlib.blake2b(key=key.encode(), digest_size=DIGEST_SIZE) - p_hash.update(password.encode()) - b64url_hash = encode(p_hash.digest(), trim=True) - - self.password = key + ":" + b64url_hash + self.password = password_hash(password) def set_no_password(self) -> None: self.password = None