Skip to content

Commit

Permalink
Fix for setting password for Unit Groups
Browse files Browse the repository at this point in the history
* Password is not base64 encoded, but it's base64URL encoded.
  • Loading branch information
332fg-raven committed Feb 13, 2024
1 parent 07fe67e commit 52c9577
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions dcs/unitgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ def add_unit(self, unit: UnitT):
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

@property
def x(self):
if len(self.units) > 0:
Expand Down Expand Up @@ -232,23 +261,6 @@ def dict(self):
d["password"] = self.password
return d

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/

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())
b64hash = base64.encodebytes(p_hash.digest()).decode()

# remove '=\n' at the end.
self.password = key + ":" + b64hash[:len(b64hash) - 2]

def set_no_password(self) -> None:
self.password = None


class MovingGroup(Generic[UnitT], Group[UnitT, MovingPoint]):
def __init__(self, _id, name=None, start_time=0):
Expand Down

0 comments on commit 52c9577

Please sign in to comment.