forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.py
More file actions
29 lines (21 loc) · 979 Bytes
/
Copy pathcrypt.py
File metadata and controls
29 lines (21 loc) · 979 Bytes
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
# -*- coding: UTF-8 -*-
"""Case Codecs - string hashing with Unix's Crypt.
These are codecs for hashing strings, for use with other codecs in encoding chains.
These codecs:
- transform strings from str to str
- transform strings from bytes to bytes
- transform file content from str to bytes (write)
"""
from ..__common__ import add, ensure_str, UNIX
if UNIX:
import crypt
METHODS = [x[7:].lower() for x in crypt.__dict__ if x.startswith("METHOD_")]
def crypt_hash(method):
method = (method or "").lstrip("-_") or "blowfish"
if method not in METHODS:
raise NotImplementedError("method '%s' is not implemented" % method)
def _encode(input, error="strict"):
m = getattr(crypt, "METHOD_" + method.upper())
return crypt.crypt(ensure_str(input), crypt.mksalt(m)), len(input)
return _encode
add("crypt", crypt_hash, pattern=r"^crypt(|[-_](?:%s))$" % "|".join(METHODS), guess=None)