2018-09-12 16:46:59 -07:00
|
|
|
"""
|
|
|
|
|
Functions to normalize various inputs
|
|
|
|
|
"""
|
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_system_username(username):
|
|
|
|
|
"""
|
|
|
|
|
Generate a posix username from given username.
|
|
|
|
|
|
|
|
|
|
If username < 26 char, we just return it.
|
|
|
|
|
Else, we hash the username, truncate username at
|
2021-11-01 09:42:45 +01:00
|
|
|
26 char, append a '-' and first add 5char of hash.
|
2018-09-12 16:46:59 -07:00
|
|
|
This makes sure our usernames are always under 32char.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if len(username) < 26:
|
|
|
|
|
return username
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
userhash = hashlib.sha256(username.encode("utf-8")).hexdigest()
|
|
|
|
|
return "{username_trunc}-{hash}".format(
|
2021-11-01 09:42:45 +01:00
|
|
|
username_trunc=username[:26], hash=userhash[:5]
|
|
|
|
|
)
|