Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: hashlib.py
#. Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
#
[2] Fix | Delete
[3] Fix | Delete
__doc__ = """hashlib module - A common interface to many hash functions.
[4] Fix | Delete
[5] Fix | Delete
new(name, data=b'', **kwargs) - returns a new hash object implementing the
[6] Fix | Delete
given hash function; initializing the hash
[7] Fix | Delete
using the given binary data.
[8] Fix | Delete
[9] Fix | Delete
Named constructor functions are also available, these are faster
[10] Fix | Delete
than using new(name):
[11] Fix | Delete
[12] Fix | Delete
md5(), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(),
[13] Fix | Delete
sha3_224, sha3_256, sha3_384, sha3_512, shake_128, and shake_256.
[14] Fix | Delete
[15] Fix | Delete
More algorithms may be available on your platform but the above are guaranteed
[16] Fix | Delete
to exist. See the algorithms_guaranteed and algorithms_available attributes
[17] Fix | Delete
to find out what algorithm names can be passed to new().
[18] Fix | Delete
[19] Fix | Delete
NOTE: If you want the adler32 or crc32 hash functions they are available in
[20] Fix | Delete
the zlib module.
[21] Fix | Delete
[22] Fix | Delete
Choose your hash function wisely. Some have known collision weaknesses.
[23] Fix | Delete
sha384 and sha512 will be slow on 32 bit platforms.
[24] Fix | Delete
[25] Fix | Delete
Hash objects have these methods:
[26] Fix | Delete
- update(data): Update the hash object with the bytes in data. Repeated calls
[27] Fix | Delete
are equivalent to a single call with the concatenation of all
[28] Fix | Delete
the arguments.
[29] Fix | Delete
- digest(): Return the digest of the bytes passed to the update() method
[30] Fix | Delete
so far as a bytes object.
[31] Fix | Delete
- hexdigest(): Like digest() except the digest is returned as a string
[32] Fix | Delete
of double length, containing only hexadecimal digits.
[33] Fix | Delete
- copy(): Return a copy (clone) of the hash object. This can be used to
[34] Fix | Delete
efficiently compute the digests of datas that share a common
[35] Fix | Delete
initial substring.
[36] Fix | Delete
[37] Fix | Delete
For example, to obtain the digest of the byte string 'Nobody inspects the
[38] Fix | Delete
spammish repetition':
[39] Fix | Delete
[40] Fix | Delete
>>> import hashlib
[41] Fix | Delete
>>> m = hashlib.md5()
[42] Fix | Delete
>>> m.update(b"Nobody inspects")
[43] Fix | Delete
>>> m.update(b" the spammish repetition")
[44] Fix | Delete
>>> m.digest()
[45] Fix | Delete
b'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'
[46] Fix | Delete
[47] Fix | Delete
More condensed:
[48] Fix | Delete
[49] Fix | Delete
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
[50] Fix | Delete
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
[51] Fix | Delete
[52] Fix | Delete
"""
[53] Fix | Delete
[54] Fix | Delete
# This tuple and __get_builtin_constructor() must be modified if a new
[55] Fix | Delete
# always available algorithm is added.
[56] Fix | Delete
__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
[57] Fix | Delete
'blake2b', 'blake2s',
[58] Fix | Delete
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
[59] Fix | Delete
'shake_128', 'shake_256')
[60] Fix | Delete
[61] Fix | Delete
[62] Fix | Delete
algorithms_guaranteed = set(__always_supported)
[63] Fix | Delete
algorithms_available = set(__always_supported)
[64] Fix | Delete
[65] Fix | Delete
__all__ = __always_supported + ('new', 'algorithms_guaranteed',
[66] Fix | Delete
'algorithms_available', 'pbkdf2_hmac')
[67] Fix | Delete
[68] Fix | Delete
[69] Fix | Delete
__builtin_constructor_cache = {}
[70] Fix | Delete
[71] Fix | Delete
# Prefer our blake2 implementation
[72] Fix | Delete
# OpenSSL 1.1.0 comes with a limited implementation of blake2b/s. The OpenSSL
[73] Fix | Delete
# implementations neither support keyed blake2 (blake2 MAC) nor advanced
[74] Fix | Delete
# features like salt, personalization, or tree hashing. OpenSSL hash-only
[75] Fix | Delete
# variants are available as 'blake2b512' and 'blake2s256', though.
[76] Fix | Delete
__block_openssl_constructor = {
[77] Fix | Delete
'blake2b', 'blake2s',
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
def __get_builtin_constructor(name):
[81] Fix | Delete
cache = __builtin_constructor_cache
[82] Fix | Delete
constructor = cache.get(name)
[83] Fix | Delete
if constructor is not None:
[84] Fix | Delete
return constructor
[85] Fix | Delete
try:
[86] Fix | Delete
if name in {'SHA1', 'sha1'}:
[87] Fix | Delete
import _sha1
[88] Fix | Delete
cache['SHA1'] = cache['sha1'] = _sha1.sha1
[89] Fix | Delete
elif name in {'MD5', 'md5'}:
[90] Fix | Delete
import _md5
[91] Fix | Delete
cache['MD5'] = cache['md5'] = _md5.md5
[92] Fix | Delete
elif name in {'SHA256', 'sha256', 'SHA224', 'sha224'}:
[93] Fix | Delete
import _sha256
[94] Fix | Delete
cache['SHA224'] = cache['sha224'] = _sha256.sha224
[95] Fix | Delete
cache['SHA256'] = cache['sha256'] = _sha256.sha256
[96] Fix | Delete
elif name in {'SHA512', 'sha512', 'SHA384', 'sha384'}:
[97] Fix | Delete
import _sha512
[98] Fix | Delete
cache['SHA384'] = cache['sha384'] = _sha512.sha384
[99] Fix | Delete
cache['SHA512'] = cache['sha512'] = _sha512.sha512
[100] Fix | Delete
elif name in {'blake2b', 'blake2s'}:
[101] Fix | Delete
import _blake2
[102] Fix | Delete
cache['blake2b'] = _blake2.blake2b
[103] Fix | Delete
cache['blake2s'] = _blake2.blake2s
[104] Fix | Delete
elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512'}:
[105] Fix | Delete
import _sha3
[106] Fix | Delete
cache['sha3_224'] = _sha3.sha3_224
[107] Fix | Delete
cache['sha3_256'] = _sha3.sha3_256
[108] Fix | Delete
cache['sha3_384'] = _sha3.sha3_384
[109] Fix | Delete
cache['sha3_512'] = _sha3.sha3_512
[110] Fix | Delete
elif name in {'shake_128', 'shake_256'}:
[111] Fix | Delete
import _sha3
[112] Fix | Delete
cache['shake_128'] = _sha3.shake_128
[113] Fix | Delete
cache['shake_256'] = _sha3.shake_256
[114] Fix | Delete
except ImportError:
[115] Fix | Delete
pass # no extension module, this hash is unsupported.
[116] Fix | Delete
[117] Fix | Delete
constructor = cache.get(name)
[118] Fix | Delete
if constructor is not None:
[119] Fix | Delete
return constructor
[120] Fix | Delete
[121] Fix | Delete
raise ValueError('unsupported hash type ' + name)
[122] Fix | Delete
[123] Fix | Delete
[124] Fix | Delete
def __get_openssl_constructor(name):
[125] Fix | Delete
if name in __block_openssl_constructor:
[126] Fix | Delete
# Prefer our builtin blake2 implementation.
[127] Fix | Delete
return __get_builtin_constructor(name)
[128] Fix | Delete
try:
[129] Fix | Delete
# MD5, SHA1, and SHA2 are in all supported OpenSSL versions
[130] Fix | Delete
# SHA3/shake are available in OpenSSL 1.1.1+
[131] Fix | Delete
f = getattr(_hashlib, 'openssl_' + name)
[132] Fix | Delete
# Allow the C module to raise ValueError. The function will be
[133] Fix | Delete
# defined but the hash not actually available. Don't fall back to
[134] Fix | Delete
# builtin if the current security policy blocks a digest, bpo#40695.
[135] Fix | Delete
f(usedforsecurity=False)
[136] Fix | Delete
# Use the C function directly (very fast)
[137] Fix | Delete
return f
[138] Fix | Delete
except (AttributeError, ValueError):
[139] Fix | Delete
return __get_builtin_constructor(name)
[140] Fix | Delete
[141] Fix | Delete
[142] Fix | Delete
def __py_new(name, data=b'', **kwargs):
[143] Fix | Delete
"""new(name, data=b'', **kwargs) - Return a new hashing object using the
[144] Fix | Delete
named algorithm; optionally initialized with data (which must be
[145] Fix | Delete
a bytes-like object).
[146] Fix | Delete
"""
[147] Fix | Delete
return __get_builtin_constructor(name)(data, **kwargs)
[148] Fix | Delete
[149] Fix | Delete
[150] Fix | Delete
def __hash_new(name, data=b'', **kwargs):
[151] Fix | Delete
"""new(name, data=b'') - Return a new hashing object using the named algorithm;
[152] Fix | Delete
optionally initialized with data (which must be a bytes-like object).
[153] Fix | Delete
"""
[154] Fix | Delete
if name in __block_openssl_constructor:
[155] Fix | Delete
# Prefer our builtin blake2 implementation.
[156] Fix | Delete
return __get_builtin_constructor(name)(data, **kwargs)
[157] Fix | Delete
try:
[158] Fix | Delete
return _hashlib.new(name, data, **kwargs)
[159] Fix | Delete
except ValueError:
[160] Fix | Delete
# If the _hashlib module (OpenSSL) doesn't support the named
[161] Fix | Delete
# hash, try using our builtin implementations.
[162] Fix | Delete
# This allows for SHA224/256 and SHA384/512 support even though
[163] Fix | Delete
# the OpenSSL library prior to 0.9.8 doesn't provide them.
[164] Fix | Delete
return __get_builtin_constructor(name)(data)
[165] Fix | Delete
[166] Fix | Delete
[167] Fix | Delete
try:
[168] Fix | Delete
import _hashlib
[169] Fix | Delete
new = __hash_new
[170] Fix | Delete
__get_hash = __get_openssl_constructor
[171] Fix | Delete
algorithms_available = algorithms_available.union(
[172] Fix | Delete
_hashlib.openssl_md_meth_names)
[173] Fix | Delete
except ImportError:
[174] Fix | Delete
new = __py_new
[175] Fix | Delete
__get_hash = __get_builtin_constructor
[176] Fix | Delete
[177] Fix | Delete
try:
[178] Fix | Delete
# OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA
[179] Fix | Delete
from _hashlib import pbkdf2_hmac
[180] Fix | Delete
except ImportError:
[181] Fix | Delete
_trans_5C = bytes((x ^ 0x5C) for x in range(256))
[182] Fix | Delete
_trans_36 = bytes((x ^ 0x36) for x in range(256))
[183] Fix | Delete
[184] Fix | Delete
def pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None):
[185] Fix | Delete
"""Password based key derivation function 2 (PKCS #5 v2.0)
[186] Fix | Delete
[187] Fix | Delete
This Python implementations based on the hmac module about as fast
[188] Fix | Delete
as OpenSSL's PKCS5_PBKDF2_HMAC for short passwords and much faster
[189] Fix | Delete
for long passwords.
[190] Fix | Delete
"""
[191] Fix | Delete
if not isinstance(hash_name, str):
[192] Fix | Delete
raise TypeError(hash_name)
[193] Fix | Delete
[194] Fix | Delete
if not isinstance(password, (bytes, bytearray)):
[195] Fix | Delete
password = bytes(memoryview(password))
[196] Fix | Delete
if not isinstance(salt, (bytes, bytearray)):
[197] Fix | Delete
salt = bytes(memoryview(salt))
[198] Fix | Delete
[199] Fix | Delete
# Fast inline HMAC implementation
[200] Fix | Delete
inner = new(hash_name)
[201] Fix | Delete
outer = new(hash_name)
[202] Fix | Delete
blocksize = getattr(inner, 'block_size', 64)
[203] Fix | Delete
if len(password) > blocksize:
[204] Fix | Delete
password = new(hash_name, password).digest()
[205] Fix | Delete
password = password + b'\x00' * (blocksize - len(password))
[206] Fix | Delete
inner.update(password.translate(_trans_36))
[207] Fix | Delete
outer.update(password.translate(_trans_5C))
[208] Fix | Delete
[209] Fix | Delete
def prf(msg, inner=inner, outer=outer):
[210] Fix | Delete
# PBKDF2_HMAC uses the password as key. We can re-use the same
[211] Fix | Delete
# digest objects and just update copies to skip initialization.
[212] Fix | Delete
icpy = inner.copy()
[213] Fix | Delete
ocpy = outer.copy()
[214] Fix | Delete
icpy.update(msg)
[215] Fix | Delete
ocpy.update(icpy.digest())
[216] Fix | Delete
return ocpy.digest()
[217] Fix | Delete
[218] Fix | Delete
if iterations < 1:
[219] Fix | Delete
raise ValueError(iterations)
[220] Fix | Delete
if dklen is None:
[221] Fix | Delete
dklen = outer.digest_size
[222] Fix | Delete
if dklen < 1:
[223] Fix | Delete
raise ValueError(dklen)
[224] Fix | Delete
[225] Fix | Delete
dkey = b''
[226] Fix | Delete
loop = 1
[227] Fix | Delete
from_bytes = int.from_bytes
[228] Fix | Delete
while len(dkey) < dklen:
[229] Fix | Delete
prev = prf(salt + loop.to_bytes(4, 'big'))
[230] Fix | Delete
# endianness doesn't matter here as long to / from use the same
[231] Fix | Delete
rkey = int.from_bytes(prev, 'big')
[232] Fix | Delete
for i in range(iterations - 1):
[233] Fix | Delete
prev = prf(prev)
[234] Fix | Delete
# rkey = rkey ^ prev
[235] Fix | Delete
rkey ^= from_bytes(prev, 'big')
[236] Fix | Delete
loop += 1
[237] Fix | Delete
dkey += rkey.to_bytes(inner.digest_size, 'big')
[238] Fix | Delete
[239] Fix | Delete
return dkey[:dklen]
[240] Fix | Delete
[241] Fix | Delete
try:
[242] Fix | Delete
# OpenSSL's scrypt requires OpenSSL 1.1+
[243] Fix | Delete
from _hashlib import scrypt
[244] Fix | Delete
except ImportError:
[245] Fix | Delete
pass
[246] Fix | Delete
[247] Fix | Delete
[248] Fix | Delete
for __func_name in __always_supported:
[249] Fix | Delete
# try them all, some may not work due to the OpenSSL
[250] Fix | Delete
# version not supporting that algorithm.
[251] Fix | Delete
try:
[252] Fix | Delete
globals()[__func_name] = __get_hash(__func_name)
[253] Fix | Delete
except ValueError:
[254] Fix | Delete
import logging
[255] Fix | Delete
logging.exception('code for hash %s was not found.', __func_name)
[256] Fix | Delete
[257] Fix | Delete
[258] Fix | Delete
# Cleanup locals()
[259] Fix | Delete
del __always_supported, __func_name, __get_hash
[260] Fix | Delete
del __py_new, __hash_new, __get_openssl_constructor
[261] Fix | Delete
[262] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function