def htpasswd(password, hash): if hash.startswith('$apr1$'): return md5crypt(password, hash[6:].split('$')[0], '$apr1$') elif hash.startswith('{SHA}'): return '{SHA}' + sha1(password).digest().encode('base64')[:-1] elif passlib_ctxt is not None and hash.startswith('$5$') and \ 'sha256_crypt' in passlib_ctxt.policy.schemes(): return passlib_ctxt.encrypt(password, scheme="sha256_crypt", rounds=5000, salt=hash[3:].split('$')[0]) elif passlib_ctxt is not None and hash.startswith('$6$') and \ 'sha512_crypt' in passlib_ctxt.policy.schemes(): return passlib_ctxt.encrypt(password, scheme="sha512_crypt", rounds=5000, salt=hash[3:].split('$')[0]) elif crypt is None: # crypt passwords are only supported on Unix-like systems raise NotImplementedError( _("""The \"crypt\" module is unavailable on this platform.""")) else: if hash.startswith('$5$') or hash.startswith('$6$'): # Import of passlib failed, now check, if crypt is capable. if not crypt(password, hash).startswith(hash): # No, so bail out. raise NotImplementedError( _("""Neither are \"sha2\" hash algorithms supported by the \"crypt\" module on this platform nor is \"passlib\" available.""")) return crypt(password, hash)
def htpasswd(password, hash): if hash.startswith('$apr1$'): return md5crypt(password, hash[6:].split('$')[0], '$apr1$') elif hash.startswith('{SHA}'): return '{SHA}' + sha1(password).digest().encode('base64')[:-1] elif passlib_ctxt is not None and hash.startswith('$5$') and \ 'sha256_crypt' in passlib_ctxt.policy.schemes(): return passlib_ctxt.encrypt(password, scheme="sha256_crypt", rounds=5000, salt=hash[3:].split('$')[0]) elif passlib_ctxt is not None and hash.startswith('$6$') and \ 'sha512_crypt' in passlib_ctxt.policy.schemes(): return passlib_ctxt.encrypt(password, scheme="sha512_crypt", rounds=5000, salt=hash[3:].split('$')[0]) elif crypt is None: # crypt passwords are only supported on Unix-like systems raise NotImplementedError(_("""The \"crypt\" module is unavailable on this platform.""")) else: if hash.startswith('$5$') or hash.startswith('$6$'): # Import of passlib failed, now check, if crypt is capable. if not crypt(password, hash).startswith(hash): # No, so bail out. raise NotImplementedError(_( """Neither are \"sha2\" hash algorithms supported by the \"crypt\" module on this platform nor is \"passlib\" available.""")) return crypt(password, hash)
def htpasswd(password, hash): if hash.startswith('$apr1$'): return md5crypt(password, hash[6:].split('$')[0], '$apr1$') elif hash.startswith('{SHA}'): return '{SHA}' + sha1(password).digest().encode('base64')[:-1] elif crypt is None: # crypt passwords are only supported on Unix-like systems raise NotImplementedError(_("""The \"crypt\" module is unavailable on this platform.""")) else: return crypt(password, hash)