|
15 | 15 | try: |
16 | 16 | import crypt |
17 | 17 | except ImportError: |
18 | | - import legacycrypt as crypt |
| 18 | + try: |
| 19 | + import legacycrypt as crypt |
| 20 | + except ImportError: |
| 21 | + crypt = None |
19 | 22 |
|
20 | | - METHODS = [x[7:].lower() for x in crypt.__dict__ if x.startswith("METHOD_")] |
21 | | - |
22 | | - def crypt_hash(method): |
23 | | - method = (method or "").lstrip("-_") or "blowfish" |
24 | | - if method not in METHODS: |
25 | | - raise NotImplementedError("method '%s' is not implemented" % method) |
26 | | - def _encode(input, error="strict"): |
27 | | - m = getattr(crypt, "METHOD_" + method.upper()) |
28 | | - return crypt.crypt(ensure_str(input), crypt.mksalt(m)), len(input) |
29 | | - return _encode |
30 | | - |
31 | | - add("crypt", crypt_hash, pattern=r"^crypt(|[-_](?:%s))$" % "|".join(METHODS), guess=None) |
| 23 | + if crypt is not None: |
| 24 | + METHODS = [x[7:].lower() for x in crypt.__dict__ if x.startswith("METHOD_")] |
| 25 | + |
| 26 | + def crypt_hash(method): |
| 27 | + method = (method or "").lstrip("-_") or "blowfish" |
| 28 | + if method not in METHODS: |
| 29 | + raise NotImplementedError("method '%s' is not implemented" % method) |
| 30 | + def _encode(input, error="strict"): |
| 31 | + m = getattr(crypt, "METHOD_" + method.upper()) |
| 32 | + return crypt.crypt(ensure_str(input), crypt.mksalt(m)), len(input) |
| 33 | + return _encode |
| 34 | + |
| 35 | + add("crypt", crypt_hash, pattern=r"^crypt(|[-_](?:%s))$" % "|".join(METHODS), guess=None) |
32 | 36 |
|
0 commit comments