import sys
from random import SystemRandom as sr
import crypt_r
def crypt_password(password):
"""Crypt a password.
Process a password with appropriate salted one-way algorithm.
:param str password: password to be crypted
:returns: crypted representation of the original password
:rtype: str
"""
b64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
setting = "$y$j9T$" + "".join(sr().choice(b64) for _sc in range(24))
try:
cryptpw = crypt_r.crypt(password, setting)
except OSError:
sys.stderr.write("yescrypt is not supported, falling back to sha512crypt\n")
try:
cryptpw = crypt_r.crypt(password, crypt_r.METHOD_SHA512)
except OSError as exc:
raise RuntimeError(
f"Unable to encrypt password: unsupported algorithm {crypt_r.METHOD_SHA512}"
) from exc
return cryptpw
try:
print(crypt_password(sys.argv[1]), end="")
except Exception as e:
sys.stderr.write(str(e) + "\n")
sys.exit(1)