I've recently been testing CherryPy 3.2.0 and
looking into their new tool-based support for Digest Authentication.
But I didn't have a simple way to generate the correctly formatted lines for CherryPy's
built-in get_ha1_file_htdigest
function that expects an Apache-styled htdigest
-like
credentials store file.
Until now.
# file: htdigest.py
import sys
from hashlib import md5
class Options(object):
pass
def main(opts):
ht5 = lambda x: md5(':'.join(x)).hexdigest()
htpwd = ':'.join((opts.user, opts.realm,
ht5((opts.user, opts.realm, opts.pwd))))
print htpwd
if __name__ == '__main__':
usage = '''
usage: python htdigest.py --user USER --pwd PASSWORD [--realm REALM]
'''
opts = Options()
for o in ['--user', '--pwd', '--realm']:
try:
setattr(opts, o.replace('--',''), sys.argv[sys.argv.index(o)+1])
except ValueError:
pass
except IndexError:
pass
opts.realm = getattr(opts, 'realm', 'default_realm')
if not hasattr(opts, 'user') or not hasattr(opts, 'pwd'):
print("[ERROR] must supply both a username and password")
print(usage)
exit(1)
main(opts)
which generates results like:
C:\tools>python htdigest.py --user remi --pwd lerante remi:default_realm:7076fd3cc727f3c4770ff0bb5905b9d8 C:\tools>
While the helper script simply outputs to stdout
, you can use your shell
redirectors >
and >>
to create a new, or append to an existing,
htdigest
-like credentials store file.