Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: hmac.py
"""HMAC (Keyed-Hashing for Message Authentication) Python 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
import hashlib as _hashlib
[7] Fix | Delete
import _hashlib as _hashlibopenssl
[8] Fix | Delete
import _hmacopenssl
[9] Fix | Delete
[10] Fix | Delete
trans_5C = bytes((x ^ 0x5C) for x in range(256))
[11] Fix | Delete
trans_36 = bytes((x ^ 0x36) for x in range(256))
[12] Fix | Delete
[13] Fix | Delete
# The size of the digests returned by HMAC depends on the underlying
[14] Fix | Delete
# hashing module used. Use digest_size from the instance of HMAC instead.
[15] Fix | Delete
digest_size = None
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
class HMAC:
[20] Fix | Delete
"""RFC 2104 HMAC class. Also complies with RFC 4231.
[21] Fix | Delete
[22] Fix | Delete
This supports the API for Cryptographic Hash Functions (PEP 247).
[23] Fix | Delete
"""
[24] Fix | Delete
blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
[25] Fix | Delete
[26] Fix | Delete
def __init__(self, key, msg = None, digestmod = None):
[27] Fix | Delete
"""Create a new HMAC object.
[28] Fix | Delete
[29] Fix | Delete
key: key for the keyed hash object.
[30] Fix | Delete
msg: Initial input for the hash, if provided.
[31] Fix | Delete
digestmod: A module supporting PEP 247. *OR*
[32] Fix | Delete
A hashlib constructor returning a new hash object. *OR*
[33] Fix | Delete
A hash name suitable for hashlib.new().
[34] Fix | Delete
Defaults to hashlib.md5.
[35] Fix | Delete
Implicit default to hashlib.md5 is deprecated and will be
[36] Fix | Delete
removed in Python 3.6.
[37] Fix | Delete
[38] Fix | Delete
Note: key and msg must be a bytes or bytearray objects.
[39] Fix | Delete
"""
[40] Fix | Delete
if _hashlibopenssl.get_fips_mode():
[41] Fix | Delete
raise ValueError(
[42] Fix | Delete
'This class is not available in FIPS mode. '
[43] Fix | Delete
+ 'Use hmac.new().'
[44] Fix | Delete
)
[45] Fix | Delete
[46] Fix | Delete
if not isinstance(key, (bytes, bytearray)):
[47] Fix | Delete
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
[48] Fix | Delete
[49] Fix | Delete
if digestmod is None:
[50] Fix | Delete
_warnings.warn("HMAC() without an explicit digestmod argument "
[51] Fix | Delete
"is deprecated.", PendingDeprecationWarning, 2)
[52] Fix | Delete
digestmod = _hashlib.md5
[53] Fix | Delete
[54] Fix | Delete
if callable(digestmod):
[55] Fix | Delete
self.digest_cons = digestmod
[56] Fix | Delete
elif isinstance(digestmod, str):
[57] Fix | Delete
self.digest_cons = lambda d=b'': _hashlib.new(digestmod, d)
[58] Fix | Delete
else:
[59] Fix | Delete
self.digest_cons = lambda d=b'': digestmod.new(d)
[60] Fix | Delete
[61] Fix | Delete
self.outer = self.digest_cons()
[62] Fix | Delete
self.inner = self.digest_cons()
[63] Fix | Delete
self.digest_size = self.inner.digest_size
[64] Fix | Delete
[65] Fix | Delete
if hasattr(self.inner, 'block_size'):
[66] Fix | Delete
blocksize = self.inner.block_size
[67] Fix | Delete
if blocksize < 16:
[68] Fix | Delete
_warnings.warn('block_size of %d seems too small; using our '
[69] Fix | Delete
'default of %d.' % (blocksize, self.blocksize),
[70] Fix | Delete
RuntimeWarning, 2)
[71] Fix | Delete
blocksize = self.blocksize
[72] Fix | Delete
else:
[73] Fix | Delete
_warnings.warn('No block_size attribute on given digest object; '
[74] Fix | Delete
'Assuming %d.' % (self.blocksize),
[75] Fix | Delete
RuntimeWarning, 2)
[76] Fix | Delete
blocksize = self.blocksize
[77] Fix | Delete
[78] Fix | Delete
# self.blocksize is the default blocksize. self.block_size is
[79] Fix | Delete
# effective block size as well as the public API attribute.
[80] Fix | Delete
self.block_size = blocksize
[81] Fix | Delete
[82] Fix | Delete
if len(key) > blocksize:
[83] Fix | Delete
key = self.digest_cons(key).digest()
[84] Fix | Delete
[85] Fix | Delete
key = key.ljust(blocksize, b'\0')
[86] Fix | Delete
self.outer.update(key.translate(trans_5C))
[87] Fix | Delete
self.inner.update(key.translate(trans_36))
[88] Fix | Delete
if msg is not None:
[89] Fix | Delete
self.update(msg)
[90] Fix | Delete
[91] Fix | Delete
@property
[92] Fix | Delete
def name(self):
[93] Fix | Delete
return "hmac-" + self.inner.name
[94] Fix | Delete
[95] Fix | Delete
def update(self, msg):
[96] Fix | Delete
"""Update this hashing object with the string msg.
[97] Fix | Delete
"""
[98] Fix | Delete
if _hashlibopenssl.get_fips_mode():
[99] Fix | Delete
raise ValueError('hmac.HMAC is not available in FIPS mode')
[100] Fix | Delete
self.inner.update(msg)
[101] Fix | Delete
[102] Fix | Delete
def copy(self):
[103] Fix | Delete
"""Return a separate copy of this hashing object.
[104] Fix | Delete
[105] Fix | Delete
An update to this copy won't affect the original object.
[106] Fix | Delete
"""
[107] Fix | Delete
# Call __new__ directly to avoid the expensive __init__.
[108] Fix | Delete
other = self.__class__.__new__(self.__class__)
[109] Fix | Delete
other.digest_cons = self.digest_cons
[110] Fix | Delete
other.digest_size = self.digest_size
[111] Fix | Delete
other.inner = self.inner.copy()
[112] Fix | Delete
other.outer = self.outer.copy()
[113] Fix | Delete
return other
[114] Fix | Delete
[115] Fix | Delete
def _current(self):
[116] Fix | Delete
"""Return a hash object for the current state.
[117] Fix | Delete
[118] Fix | Delete
To be used only internally with digest() and hexdigest().
[119] Fix | Delete
"""
[120] Fix | Delete
h = self.outer.copy()
[121] Fix | Delete
h.update(self.inner.digest())
[122] Fix | Delete
return h
[123] Fix | Delete
[124] Fix | Delete
def digest(self):
[125] Fix | Delete
"""Return the hash value of this hashing object.
[126] Fix | Delete
[127] Fix | Delete
This returns a string containing 8-bit data. The object is
[128] Fix | Delete
not altered in any way by this function; you can continue
[129] Fix | Delete
updating the object after calling this function.
[130] Fix | Delete
"""
[131] Fix | Delete
h = self._current()
[132] Fix | Delete
return h.digest()
[133] Fix | Delete
[134] Fix | Delete
def hexdigest(self):
[135] Fix | Delete
"""Like digest(), but returns a string of hexadecimal digits instead.
[136] Fix | Delete
"""
[137] Fix | Delete
h = self._current()
[138] Fix | Delete
return h.hexdigest()
[139] Fix | Delete
[140] Fix | Delete
[141] Fix | Delete
def _get_openssl_name(digestmod):
[142] Fix | Delete
if digestmod is None:
[143] Fix | Delete
raise ValueError("'digestmod' argument is mandatory in FIPS mode")
[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
[156] Fix | Delete
class HMAC_openssl(_hmacopenssl.HMAC):
[157] Fix | Delete
def __new__(cls, key, msg = None, digestmod = None):
[158] Fix | Delete
if not isinstance(key, (bytes, bytearray)):
[159] Fix | Delete
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
[160] Fix | Delete
[161] Fix | Delete
name = _get_openssl_name(digestmod)
[162] Fix | Delete
result = _hmacopenssl.HMAC.__new__(cls, key, digestmod=name)
[163] Fix | Delete
if msg:
[164] Fix | Delete
result.update(msg)
[165] Fix | Delete
return result
[166] Fix | Delete
[167] Fix | Delete
[168] Fix | Delete
if _hashlibopenssl.get_fips_mode():
[169] Fix | Delete
HMAC = HMAC_openssl
[170] Fix | Delete
[171] Fix | Delete
[172] Fix | Delete
def new(key, msg = None, digestmod = None):
[173] Fix | Delete
"""Create a new hashing object and return it.
[174] Fix | Delete
[175] Fix | Delete
key: The starting key for the hash.
[176] Fix | Delete
msg: if available, will immediately be hashed into the object's starting
[177] Fix | Delete
state.
[178] Fix | Delete
[179] Fix | Delete
You can now feed arbitrary strings into the object using its update()
[180] Fix | Delete
method, and can ask for the hash value at any time by calling its digest()
[181] Fix | Delete
method.
[182] Fix | Delete
"""
[183] Fix | Delete
return HMAC(key, msg, digestmod)
[184] Fix | Delete
[185] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function