Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
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
[6] Fix | Delete
from operator import _compare_digest as compare_digest
[7] Fix | Delete
[8] Fix | Delete
[9] Fix | Delete
trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
[10] Fix | Delete
trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
[11] Fix | Delete
[12] Fix | Delete
# The size of the digests returned by HMAC depends on the underlying
[13] Fix | Delete
# hashing module used. Use digest_size from the instance of HMAC instead.
[14] Fix | Delete
digest_size = None
[15] Fix | Delete
[16] Fix | Delete
# A unique object passed by HMAC.copy() to the HMAC constructor, in order
[17] Fix | Delete
# that the latter return very quickly. HMAC("") in contrast is quite
[18] Fix | Delete
# expensive.
[19] Fix | Delete
_secret_backdoor_key = []
[20] Fix | Delete
[21] Fix | Delete
class HMAC:
[22] Fix | Delete
"""RFC 2104 HMAC class. Also complies with RFC 4231.
[23] Fix | Delete
[24] Fix | Delete
This supports the API for Cryptographic Hash Functions (PEP 247).
[25] Fix | Delete
"""
[26] Fix | Delete
blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
[27] Fix | Delete
[28] Fix | Delete
def __init__(self, key, msg = None, digestmod = None):
[29] Fix | Delete
"""Create a new HMAC object.
[30] Fix | Delete
[31] Fix | Delete
key: key for the keyed hash object.
[32] Fix | Delete
msg: Initial input for the hash, if provided.
[33] Fix | Delete
digestmod: A module supporting PEP 247. *OR*
[34] Fix | Delete
A hashlib constructor returning a new hash object.
[35] Fix | Delete
Defaults to hashlib.md5.
[36] Fix | Delete
"""
[37] Fix | Delete
[38] Fix | Delete
if key is _secret_backdoor_key: # cheap
[39] Fix | Delete
return
[40] Fix | Delete
[41] Fix | Delete
if digestmod is None:
[42] Fix | Delete
import hashlib
[43] Fix | Delete
digestmod = hashlib.md5
[44] Fix | Delete
[45] Fix | Delete
if hasattr(digestmod, '__call__'):
[46] Fix | Delete
self.digest_cons = digestmod
[47] Fix | Delete
else:
[48] Fix | Delete
self.digest_cons = lambda d='': digestmod.new(d)
[49] Fix | Delete
[50] Fix | Delete
self.outer = self.digest_cons()
[51] Fix | Delete
self.inner = self.digest_cons()
[52] Fix | Delete
self.digest_size = self.inner.digest_size
[53] Fix | Delete
[54] Fix | Delete
if hasattr(self.inner, 'block_size'):
[55] Fix | Delete
blocksize = self.inner.block_size
[56] Fix | Delete
if blocksize < 16:
[57] Fix | Delete
# Very low blocksize, most likely a legacy value like
[58] Fix | Delete
# Lib/sha.py and Lib/md5.py have.
[59] Fix | Delete
_warnings.warn('block_size of %d seems too small; using our '
[60] Fix | Delete
'default of %d.' % (blocksize, self.blocksize),
[61] Fix | Delete
RuntimeWarning, 2)
[62] Fix | Delete
blocksize = self.blocksize
[63] Fix | Delete
else:
[64] Fix | Delete
_warnings.warn('No block_size attribute on given digest object; '
[65] Fix | Delete
'Assuming %d.' % (self.blocksize),
[66] Fix | Delete
RuntimeWarning, 2)
[67] Fix | Delete
blocksize = self.blocksize
[68] Fix | Delete
[69] Fix | Delete
if len(key) > blocksize:
[70] Fix | Delete
key = self.digest_cons(key).digest()
[71] Fix | Delete
[72] Fix | Delete
key = key + chr(0) * (blocksize - len(key))
[73] Fix | Delete
self.outer.update(key.translate(trans_5C))
[74] Fix | Delete
self.inner.update(key.translate(trans_36))
[75] Fix | Delete
if msg is not None:
[76] Fix | Delete
self.update(msg)
[77] Fix | Delete
[78] Fix | Delete
## def clear(self):
[79] Fix | Delete
## raise NotImplementedError, "clear() method not available in HMAC."
[80] Fix | Delete
[81] Fix | Delete
def update(self, msg):
[82] Fix | Delete
"""Update this hashing object with the string msg.
[83] Fix | Delete
"""
[84] Fix | Delete
self.inner.update(msg)
[85] Fix | Delete
[86] Fix | Delete
def copy(self):
[87] Fix | Delete
"""Return a separate copy of this hashing object.
[88] Fix | Delete
[89] Fix | Delete
An update to this copy won't affect the original object.
[90] Fix | Delete
"""
[91] Fix | Delete
other = self.__class__(_secret_backdoor_key)
[92] Fix | Delete
other.digest_cons = self.digest_cons
[93] Fix | Delete
other.digest_size = self.digest_size
[94] Fix | Delete
other.inner = self.inner.copy()
[95] Fix | Delete
other.outer = self.outer.copy()
[96] Fix | Delete
return other
[97] Fix | Delete
[98] Fix | Delete
def _current(self):
[99] Fix | Delete
"""Return a hash object for the current state.
[100] Fix | Delete
[101] Fix | Delete
To be used only internally with digest() and hexdigest().
[102] Fix | Delete
"""
[103] Fix | Delete
h = self.outer.copy()
[104] Fix | Delete
h.update(self.inner.digest())
[105] Fix | Delete
return h
[106] Fix | Delete
[107] Fix | Delete
def digest(self):
[108] Fix | Delete
"""Return the hash value of this hashing object.
[109] Fix | Delete
[110] Fix | Delete
This returns a string containing 8-bit data. The object is
[111] Fix | Delete
not altered in any way by this function; you can continue
[112] Fix | Delete
updating the object after calling this function.
[113] Fix | Delete
"""
[114] Fix | Delete
h = self._current()
[115] Fix | Delete
return h.digest()
[116] Fix | Delete
[117] Fix | Delete
def hexdigest(self):
[118] Fix | Delete
"""Like digest(), but returns a string of hexadecimal digits instead.
[119] Fix | Delete
"""
[120] Fix | Delete
h = self._current()
[121] Fix | Delete
return h.hexdigest()
[122] Fix | Delete
[123] Fix | Delete
def new(key, msg = None, digestmod = None):
[124] Fix | Delete
"""Create a new hashing object and return it.
[125] Fix | Delete
[126] Fix | Delete
key: The starting key for the hash.
[127] Fix | Delete
msg: if available, will immediately be hashed into the object's starting
[128] Fix | Delete
state.
[129] Fix | Delete
[130] Fix | Delete
You can now feed arbitrary strings into the object using its update()
[131] Fix | Delete
method, and can ask for the hash value at any time by calling its digest()
[132] Fix | Delete
method.
[133] Fix | Delete
"""
[134] Fix | Delete
return HMAC(key, msg, digestmod)
[135] Fix | Delete
[136] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function