blob: 40536f4cfcc257d52db7a7d52be01a0f43083c92 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# Copyright (c) 2018 Aaron LI <aly@aaronly.me>
# MIT License
"""
Custom Ansible template filters to crypt/hash passwords.
"""
import os
import base64
import crypt
import hashlib
def cryptpass(p):
"""
Crypt the given plaintext password with salted SHA512 scheme,
which is supported by Linux/BSDs.
"""
hashtype = "$6$"
saltlen = 16
salt = os.urandom(saltlen)
salt = base64.b64encode(salt)[:saltlen]
return crypt.crypt(p, hashtype+salt)
def dovecot_makepass(p):
"""
Generate the salted hashed password for Dovecot using the
SHA512-CRYPT scheme.
Implement the "doveadm pw -s SHA512-CRYPT" command.
Dovecot password format: {<scheme>}$<type>$<salt>$<hash>
"""
scheme = "SHA512-CRYPT"
cp = cryptpass(p)
return "{%s}%s" % (scheme, cp)
def znc_makepass(p, method="sha256", saltlen=20):
"""
Generate the salted hashed password for ZNC configuration.
Implement the "znc --makepass" command.
ZNC password format: <method>#<hash>#<salt>
"""
salt = os.urandom(saltlen)
salt = base64.b64encode(salt)[:saltlen]
s = p + salt
h = getattr(hashlib, method)(s)
return "%s#%s#%s" % (method, h.hexdigest(), salt)
class FilterModule(object):
def filters(self):
return {
"cryptpass": cryptpass,
"dovecot_makepass": dovecot_makepass,
"znc_makepass": znc_makepass,
}
|