Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3....
File: hmac.py
"""HMAC (Keyed-Hashing for Message Authentication) module.
[0] Fix | Delete
[1] Fix | Delete
Implements the HMAC algorithm as described by RFC 2104.
[2] Fix | Delete
"""
[3] Fix | Delete
[4] Fix | Delete
import warnings as _warnings
[5] Fix | Delete
from _operator import _compare_digest as compare_digest
[6] Fix | Delete
try:
[7] Fix | Delete
import _hashlib as _hashopenssl
[8] Fix | Delete
except ImportError:
[9] Fix | Delete
_hashopenssl = None
[10] Fix | Delete
_openssl_md_meths = None
[11] Fix | Delete
else:
[12] Fix | Delete
_openssl_md_meths = frozenset(_hashopenssl.openssl_md_meth_names)
[13] Fix | Delete
import hashlib as _hashlib
[14] Fix | Delete
import _hashlib as _hashlibopenssl
[15] Fix | Delete
import _hmacopenssl
[16] Fix | Delete
[17] Fix | Delete
trans_5C = bytes((x ^ 0x5C) for x in range(256))
[18] Fix | Delete
trans_36 = bytes((x ^ 0x36) for x in range(256))
[19] Fix | Delete
[20] Fix | Delete
# The size of the digests returned by HMAC depends on the underlying
[21] Fix | Delete
# hashing module used. Use digest_size from the instance of HMAC instead.
[22] Fix | Delete
digest_size = None
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
class HMAC:
[27] Fix | Delete
"""RFC 2104 HMAC class. Also complies with RFC 4231.
[28] Fix | Delete
[29] Fix | Delete
This supports the API for Cryptographic Hash Functions (PEP 247).
[30] Fix | Delete
"""
[31] Fix | Delete
blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
[32] Fix | Delete
[33] Fix | Delete
def __init__(self, key, msg=None, digestmod=''):
[34] Fix | Delete
"""Create a new HMAC object.
[35] Fix | Delete
[36] Fix | Delete
key: bytes or buffer, key for the keyed hash object.
[37] Fix | Delete
msg: bytes or buffer, Initial input for the hash or None.
[38] Fix | Delete
digestmod: A hash name suitable for hashlib.new(). *OR*
[39] Fix | Delete
A hashlib constructor returning a new hash object. *OR*
[40] Fix | Delete
A module supporting PEP 247.
[41] Fix | Delete
[42] Fix | Delete
Required as of 3.8, despite its position after the optional
[43] Fix | Delete
msg argument. Passing it as a keyword argument is
[44] Fix | Delete
recommended, though not required for legacy API reasons.
[45] Fix | Delete
"""
[46] Fix | Delete
if _hashlibopenssl.get_fips_mode():
[47] Fix | Delete
raise ValueError(
[48] Fix | Delete
'This class is not available in FIPS mode. '
[49] Fix | Delete
+ 'Use hmac.new().'
[50] Fix | Delete
)
[51] Fix | Delete
[52] Fix | Delete
if not isinstance(key, (bytes, bytearray)):
[53] Fix | Delete
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
[54] Fix | Delete
[55] Fix | Delete
if not digestmod:
[56] Fix | Delete
raise TypeError("Missing required parameter 'digestmod'.")
[57] Fix | Delete
[58] Fix | Delete
if callable(digestmod):
[59] Fix | Delete
self.digest_cons = digestmod
[60] Fix | Delete
elif isinstance(digestmod, str):
[61] Fix | Delete
self.digest_cons = lambda d=b'': _hashlib.new(digestmod, d)
[62] Fix | Delete
else:
[63] Fix | Delete
self.digest_cons = lambda d=b'': digestmod.new(d)
[64] Fix | Delete
[65] Fix | Delete
self.outer = self.digest_cons()
[66] Fix | Delete
self.inner = self.digest_cons()
[67] Fix | Delete
self.digest_size = self.inner.digest_size
[68] Fix | Delete
[69] Fix | Delete
if hasattr(self.inner, 'block_size'):
[70] Fix | Delete
blocksize = self.inner.block_size
[71] Fix | Delete
if blocksize < 16:
[72] Fix | Delete
_warnings.warn('block_size of %d seems too small; using our '
[73] Fix | Delete
'default of %d.' % (blocksize, self.blocksize),
[74] Fix | Delete
RuntimeWarning, 2)
[75] Fix | Delete
blocksize = self.blocksize
[76] Fix | Delete
else:
[77] Fix | Delete
_warnings.warn('No block_size attribute on given digest object; '
[78] Fix | Delete
'Assuming %d.' % (self.blocksize),
[79] Fix | Delete
RuntimeWarning, 2)
[80] Fix | Delete
blocksize = self.blocksize
[81] Fix | Delete
[82] Fix | Delete
# self.blocksize is the default blocksize. self.block_size is
[83] Fix | Delete
# effective block size as well as the public API attribute.
[84] Fix | Delete
self.block_size = blocksize
[85] Fix | Delete
[86] Fix | Delete
if len(key) > blocksize:
[87] Fix | Delete
key = self.digest_cons(key).digest()
[88] Fix | Delete
[89] Fix | Delete
key = key.ljust(blocksize, b'\0')
[90] Fix | Delete
self.outer.update(key.translate(trans_5C))
[91] Fix | Delete
self.inner.update(key.translate(trans_36))
[92] Fix | Delete
if msg is not None:
[93] Fix | Delete
self.update(msg)
[94] Fix | Delete
[95] Fix | Delete
@property
[96] Fix | Delete
def name(self):
[97] Fix | Delete
return "hmac-" + self.inner.name
[98] Fix | Delete
[99] Fix | Delete
def update(self, msg):
[100] Fix | Delete
"""Feed data from msg into this hashing object."""
[101] Fix | Delete
if _hashlibopenssl.get_fips_mode():
[102] Fix | Delete
raise ValueError('hmac.HMAC is not available in FIPS mode')
[103] Fix | Delete
self.inner.update(msg)
[104] Fix | Delete
[105] Fix | Delete
def copy(self):
[106] Fix | Delete
"""Return a separate copy of this hashing object.
[107] Fix | Delete
[108] Fix | Delete
An update to this copy won't affect the original object.
[109] Fix | Delete
"""
[110] Fix | Delete
# Call __new__ directly to avoid the expensive __init__.
[111] Fix | Delete
other = self.__class__.__new__(self.__class__)
[112] Fix | Delete
other.digest_cons = self.digest_cons
[113] Fix | Delete
other.digest_size = self.digest_size
[114] Fix | Delete
other.inner = self.inner.copy()
[115] Fix | Delete
other.outer = self.outer.copy()
[116] Fix | Delete
return other
[117] Fix | Delete
[118] Fix | Delete
def _current(self):
[119] Fix | Delete
"""Return a hash object for the current state.
[120] Fix | Delete
[121] Fix | Delete
To be used only internally with digest() and hexdigest().
[122] Fix | Delete
"""
[123] Fix | Delete
h = self.outer.copy()
[124] Fix | Delete
h.update(self.inner.digest())
[125] Fix | Delete
return h
[126] Fix | Delete
[127] Fix | Delete
def digest(self):
[128] Fix | Delete
"""Return the hash value of this hashing object.
[129] Fix | Delete
[130] Fix | Delete
This returns the hmac value as bytes. The object is
[131] Fix | Delete
not altered in any way by this function; you can continue
[132] Fix | Delete
updating the object after calling this function.
[133] Fix | Delete
"""
[134] Fix | Delete
h = self._current()
[135] Fix | Delete
return h.digest()
[136] Fix | Delete
[137] Fix | Delete
def hexdigest(self):
[138] Fix | Delete
"""Like digest(), but returns a string of hexadecimal digits instead.
[139] Fix | Delete
"""
[140] Fix | Delete
h = self._current()
[141] Fix | Delete
return h.hexdigest()
[142] Fix | Delete
[143] Fix | Delete
def _get_openssl_name(digestmod):
[144] Fix | Delete
if isinstance(digestmod, str):
[145] Fix | Delete
return digestmod.lower()
[146] Fix | Delete
elif callable(digestmod):
[147] Fix | Delete
digestmod = digestmod(b'')
[148] Fix | Delete
[149] Fix | Delete
if not isinstance(digestmod, _hashlibopenssl.HASH):
[150] Fix | Delete
raise TypeError(
[151] Fix | Delete
'Only OpenSSL hashlib hashes are accepted in FIPS mode.')
[152] Fix | Delete
[153] Fix | Delete
return digestmod.name.lower().replace('_', '-')
[154] Fix | Delete
[155] Fix | Delete
class HMAC_openssl(_hmacopenssl.HMAC):
[156] Fix | Delete
def __new__(cls, key, msg = None, digestmod = None):
[157] Fix | Delete
if not isinstance(key, (bytes, bytearray)):
[158] Fix | Delete
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
[159] Fix | Delete
[160] Fix | Delete
name = _get_openssl_name(digestmod)
[161] Fix | Delete
result = _hmacopenssl.HMAC.__new__(cls, key, digestmod=name)
[162] Fix | Delete
if msg:
[163] Fix | Delete
result.update(msg)
[164] Fix | Delete
return result
[165] Fix | Delete
[166] Fix | Delete
[167] Fix | Delete
if _hashlibopenssl.get_fips_mode():
[168] Fix | Delete
HMAC = HMAC_openssl
[169] Fix | Delete
[170] Fix | Delete
[171] Fix | Delete
def new(key, msg=None, digestmod=''):
[172] Fix | Delete
"""Create a new hashing object and return it.
[173] Fix | Delete
[174] Fix | Delete
key: bytes or buffer, The starting key for the hash.
[175] Fix | Delete
msg: bytes or buffer, Initial input for the hash, or None.
[176] Fix | Delete
digestmod: A hash name suitable for hashlib.new(). *OR*
[177] Fix | Delete
A hashlib constructor returning a new hash object. *OR*
[178] Fix | Delete
A module supporting PEP 247.
[179] Fix | Delete
[180] Fix | Delete
Required as of 3.8, despite its position after the optional
[181] Fix | Delete
msg argument. Passing it as a keyword argument is
[182] Fix | Delete
recommended, though not required for legacy API reasons.
[183] Fix | Delete
[184] Fix | Delete
You can now feed arbitrary bytes into the object using its update()
[185] Fix | Delete
method, and can ask for the hash value at any time by calling its digest()
[186] Fix | Delete
or hexdigest() methods.
[187] Fix | Delete
"""
[188] Fix | Delete
return HMAC(key, msg, digestmod)
[189] Fix | Delete
[190] Fix | Delete
[191] Fix | Delete
def digest(key, msg, digest):
[192] Fix | Delete
"""Fast inline implementation of HMAC.
[193] Fix | Delete
[194] Fix | Delete
key: bytes or buffer, The key for the keyed hash object.
[195] Fix | Delete
msg: bytes or buffer, Input message.
[196] Fix | Delete
digest: A hash name suitable for hashlib.new() for best performance. *OR*
[197] Fix | Delete
A hashlib constructor returning a new hash object. *OR*
[198] Fix | Delete
A module supporting PEP 247.
[199] Fix | Delete
"""
[200] Fix | Delete
if (_hashopenssl is not None and
[201] Fix | Delete
isinstance(digest, str) and digest in _openssl_md_meths):
[202] Fix | Delete
return _hashopenssl.hmac_digest(key, msg, digest)
[203] Fix | Delete
[204] Fix | Delete
if callable(digest):
[205] Fix | Delete
digest_cons = digest
[206] Fix | Delete
elif isinstance(digest, str):
[207] Fix | Delete
digest_cons = lambda d=b'': _hashlib.new(digest, d)
[208] Fix | Delete
else:
[209] Fix | Delete
digest_cons = lambda d=b'': digest.new(d)
[210] Fix | Delete
[211] Fix | Delete
inner = digest_cons()
[212] Fix | Delete
outer = digest_cons()
[213] Fix | Delete
blocksize = getattr(inner, 'block_size', 64)
[214] Fix | Delete
if len(key) > blocksize:
[215] Fix | Delete
key = digest_cons(key).digest()
[216] Fix | Delete
key = key + b'\x00' * (blocksize - len(key))
[217] Fix | Delete
inner.update(key.translate(trans_36))
[218] Fix | Delete
outer.update(key.translate(trans_5C))
[219] Fix | Delete
inner.update(msg)
[220] Fix | Delete
outer.update(inner.digest())
[221] Fix | Delete
return outer.digest()
[222] Fix | Delete
[223] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function