dataset/seed/openldap/funcs/ldap.py
2022-10-17 18:28:22 +02:00

31 lines
989 B
Python

import __main__
from os import urandom as _urandom, environ as _environ
from hashlib import sha1 as _sha1
from base64 import encodebytes as _encodebytes, b64encode as _b64encode
from json import load as _load, dump as _dump
from os.path import join as _join, isfile as _isfile
_HERE = _environ['PWD']
_SSHA_PASSWORD_DIR = _join(_HERE, 'password', 'ssha.json')
# unproudly borrowed from
# http://www.openldap.org/faq/data/cache/347.html
def ssha_encode(password):
# do not regenerate SSHA
if _isfile(_SSHA_PASSWORD_DIR):
with open(_SSHA_PASSWORD_DIR, 'r') as fh:
passwords = _load(fh)
else:
passwords = {}
if password in passwords:
return passwords[password]
salt = _urandom(4)
h = _sha1(password.encode())
h.update(salt)
ret = _b64encode(b"{SSHA}" + _encodebytes(h.digest() + salt)[:-1]).decode()
passwords[password] = ret
with open(_SSHA_PASSWORD_DIR, 'w') as fh:
_dump(passwords, fh)
return ret