Skip to content

Commit

Permalink
Moved password_hash to dedicated file
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
332fg-raven committed Apr 22, 2024
1 parent 03e6e6b commit ed04673
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
38 changes: 38 additions & 0 deletions dcs/password.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 2 additions & 30 deletions dcs/unitgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ed04673

Please sign in to comment.