Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: crypt.py
"""Wrapper to the POSIX crypt library call and associated functionality."""
[0] Fix | Delete
[1] Fix | Delete
import sys as _sys
[2] Fix | Delete
[3] Fix | Delete
try:
[4] Fix | Delete
import _crypt
[5] Fix | Delete
except ModuleNotFoundError:
[6] Fix | Delete
if _sys.platform == 'win32':
[7] Fix | Delete
raise ImportError("The crypt module is not supported on Windows")
[8] Fix | Delete
else:
[9] Fix | Delete
raise ImportError("The required _crypt module was not built as part of CPython")
[10] Fix | Delete
[11] Fix | Delete
import errno
[12] Fix | Delete
import string as _string
[13] Fix | Delete
from random import SystemRandom as _SystemRandom
[14] Fix | Delete
from collections import namedtuple as _namedtuple
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
_saltchars = _string.ascii_letters + _string.digits + './'
[18] Fix | Delete
_sr = _SystemRandom()
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')):
[22] Fix | Delete
[23] Fix | Delete
"""Class representing a salt method per the Modular Crypt Format or the
[24] Fix | Delete
legacy 2-character crypt method."""
[25] Fix | Delete
[26] Fix | Delete
def __repr__(self):
[27] Fix | Delete
return '<crypt.METHOD_{}>'.format(self.name)
[28] Fix | Delete
[29] Fix | Delete
[30] Fix | Delete
def mksalt(method=None, *, rounds=None):
[31] Fix | Delete
"""Generate a salt for the specified method.
[32] Fix | Delete
[33] Fix | Delete
If not specified, the strongest available method will be used.
[34] Fix | Delete
[35] Fix | Delete
"""
[36] Fix | Delete
if method is None:
[37] Fix | Delete
method = methods[0]
[38] Fix | Delete
if rounds is not None and not isinstance(rounds, int):
[39] Fix | Delete
raise TypeError(f'{rounds.__class__.__name__} object cannot be '
[40] Fix | Delete
f'interpreted as an integer')
[41] Fix | Delete
if not method.ident: # traditional
[42] Fix | Delete
s = ''
[43] Fix | Delete
else: # modular
[44] Fix | Delete
s = f'${method.ident}$'
[45] Fix | Delete
[46] Fix | Delete
if method.ident and method.ident[0] == '2': # Blowfish variants
[47] Fix | Delete
if rounds is None:
[48] Fix | Delete
log_rounds = 12
[49] Fix | Delete
else:
[50] Fix | Delete
log_rounds = int.bit_length(rounds-1)
[51] Fix | Delete
if rounds != 1 << log_rounds:
[52] Fix | Delete
raise ValueError('rounds must be a power of 2')
[53] Fix | Delete
if not 4 <= log_rounds <= 31:
[54] Fix | Delete
raise ValueError('rounds out of the range 2**4 to 2**31')
[55] Fix | Delete
s += f'{log_rounds:02d}$'
[56] Fix | Delete
elif method.ident in ('5', '6'): # SHA-2
[57] Fix | Delete
if rounds is not None:
[58] Fix | Delete
if not 1000 <= rounds <= 999_999_999:
[59] Fix | Delete
raise ValueError('rounds out of the range 1000 to 999_999_999')
[60] Fix | Delete
s += f'rounds={rounds}$'
[61] Fix | Delete
elif rounds is not None:
[62] Fix | Delete
raise ValueError(f"{method} doesn't support the rounds argument")
[63] Fix | Delete
[64] Fix | Delete
s += ''.join(_sr.choice(_saltchars) for char in range(method.salt_chars))
[65] Fix | Delete
return s
[66] Fix | Delete
[67] Fix | Delete
[68] Fix | Delete
def crypt(word, salt=None):
[69] Fix | Delete
"""Return a string representing the one-way hash of a password, with a salt
[70] Fix | Delete
prepended.
[71] Fix | Delete
[72] Fix | Delete
If ``salt`` is not specified or is ``None``, the strongest
[73] Fix | Delete
available method will be selected and a salt generated. Otherwise,
[74] Fix | Delete
``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
[75] Fix | Delete
returned by ``crypt.mksalt()``.
[76] Fix | Delete
[77] Fix | Delete
"""
[78] Fix | Delete
if salt is None or isinstance(salt, _Method):
[79] Fix | Delete
salt = mksalt(salt)
[80] Fix | Delete
return _crypt.crypt(word, salt)
[81] Fix | Delete
[82] Fix | Delete
[83] Fix | Delete
# available salting/crypto methods
[84] Fix | Delete
methods = []
[85] Fix | Delete
[86] Fix | Delete
def _add_method(name, *args, rounds=None):
[87] Fix | Delete
method = _Method(name, *args)
[88] Fix | Delete
globals()['METHOD_' + name] = method
[89] Fix | Delete
salt = mksalt(method, rounds=rounds)
[90] Fix | Delete
result = None
[91] Fix | Delete
try:
[92] Fix | Delete
result = crypt('', salt)
[93] Fix | Delete
except OSError as e:
[94] Fix | Delete
# Not all libc libraries support all encryption methods.
[95] Fix | Delete
if e.errno == errno.EINVAL:
[96] Fix | Delete
return False
[97] Fix | Delete
raise
[98] Fix | Delete
if result and len(result) == method.total_size:
[99] Fix | Delete
methods.append(method)
[100] Fix | Delete
return True
[101] Fix | Delete
return False
[102] Fix | Delete
[103] Fix | Delete
_add_method('SHA512', '6', 16, 106)
[104] Fix | Delete
_add_method('SHA256', '5', 16, 63)
[105] Fix | Delete
[106] Fix | Delete
# Choose the strongest supported version of Blowfish hashing.
[107] Fix | Delete
# Early versions have flaws. Version 'a' fixes flaws of
[108] Fix | Delete
# the initial implementation, 'b' fixes flaws of 'a'.
[109] Fix | Delete
# 'y' is the same as 'b', for compatibility
[110] Fix | Delete
# with openwall crypt_blowfish.
[111] Fix | Delete
for _v in 'b', 'y', 'a', '':
[112] Fix | Delete
if _add_method('BLOWFISH', '2' + _v, 22, 59 + len(_v), rounds=1<<4):
[113] Fix | Delete
break
[114] Fix | Delete
[115] Fix | Delete
_add_method('MD5', '1', 8, 34)
[116] Fix | Delete
_add_method('CRYPT', None, 2, 13)
[117] Fix | Delete
[118] Fix | Delete
del _v, _add_method
[119] Fix | Delete
[120] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function