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