Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
hmac.HMAC Class Reference

List of all members.

Public Member Functions

def __init__
def update
def copy
def digest
def hexdigest

Public Attributes

 digest_cons
 outer
 inner
 digest_size

Static Public Attributes

int blocksize = 64

Private Member Functions

def _current

Detailed Description

RFC 2104 HMAC class.  Also complies with RFC 4231.

This supports the API for Cryptographic Hash Functions (PEP 247).

Definition at line 16 of file hmac.py.


Constructor & Destructor Documentation

def hmac.HMAC.__init__ (   self,
  key,
  msg = None,
  digestmod = None 
)
Create a new HMAC object.

key:       key for the keyed hash object.
msg:       Initial input for the hash, if provided.
digestmod: A module supporting PEP 247.  *OR*
   A hashlib constructor returning a new hash object.
   Defaults to hashlib.md5.

Note: key and msg must be bytes objects.

Definition at line 23 of file hmac.py.

00023 
00024     def __init__(self, key, msg = None, digestmod = None):
00025         """Create a new HMAC object.
00026 
00027         key:       key for the keyed hash object.
00028         msg:       Initial input for the hash, if provided.
00029         digestmod: A module supporting PEP 247.  *OR*
00030                    A hashlib constructor returning a new hash object.
00031                    Defaults to hashlib.md5.
00032 
00033         Note: key and msg must be bytes objects.
00034         """
00035 
00036         if not isinstance(key, bytes):
00037             raise TypeError("expected bytes, but got %r" % type(key).__name__)
00038 
00039         if digestmod is None:
00040             import hashlib
00041             digestmod = hashlib.md5
00042 
00043         if hasattr(digestmod, '__call__'):
00044             self.digest_cons = digestmod
00045         else:
00046             self.digest_cons = lambda d=b'': digestmod.new(d)
00047 
00048         self.outer = self.digest_cons()
00049         self.inner = self.digest_cons()
00050         self.digest_size = self.inner.digest_size
00051 
00052         if hasattr(self.inner, 'block_size'):
00053             blocksize = self.inner.block_size
00054             if blocksize < 16:
00055                 _warnings.warn('block_size of %d seems too small; using our '
00056                                'default of %d.' % (blocksize, self.blocksize),
00057                                RuntimeWarning, 2)
00058                 blocksize = self.blocksize
00059         else:
00060             _warnings.warn('No block_size attribute on given digest object; '
00061                            'Assuming %d.' % (self.blocksize),
00062                            RuntimeWarning, 2)
00063             blocksize = self.blocksize
00064 
00065         if len(key) > blocksize:
00066             key = self.digest_cons(key).digest()
00067 
00068         key = key + bytes(blocksize - len(key))
00069         self.outer.update(key.translate(trans_5C))
00070         self.inner.update(key.translate(trans_36))
00071         if msg is not None:
00072             self.update(msg)

Here is the caller graph for this function:


Member Function Documentation

def hmac.HMAC._current (   self) [private]
Return a hash object for the current state.

To be used only internally with digest() and hexdigest().

Definition at line 93 of file hmac.py.

00093 
00094     def _current(self):
00095         """Return a hash object for the current state.
00096 
00097         To be used only internally with digest() and hexdigest().
00098         """
00099         h = self.outer.copy()
00100         h.update(self.inner.digest())
00101         return h

Here is the caller graph for this function:

def hmac.HMAC.copy (   self)
Return a separate copy of this hashing object.

An update to this copy won't affect the original object.

Definition at line 80 of file hmac.py.

00080 
00081     def copy(self):
00082         """Return a separate copy of this hashing object.
00083 
00084         An update to this copy won't affect the original object.
00085         """
00086         # Call __new__ directly to avoid the expensive __init__.
00087         other = self.__class__.__new__(self.__class__)
00088         other.digest_cons = self.digest_cons
00089         other.digest_size = self.digest_size
00090         other.inner = self.inner.copy()
00091         other.outer = self.outer.copy()
00092         return other

def hmac.HMAC.digest (   self)
Return the hash value of this hashing object.

This returns a string containing 8-bit data.  The object is
not altered in any way by this function; you can continue
updating the object after calling this function.

Definition at line 102 of file hmac.py.

00102 
00103     def digest(self):
00104         """Return the hash value of this hashing object.
00105 
00106         This returns a string containing 8-bit data.  The object is
00107         not altered in any way by this function; you can continue
00108         updating the object after calling this function.
00109         """
00110         h = self._current()
00111         return h.digest()

Here is the call graph for this function:

def hmac.HMAC.hexdigest (   self)
Like digest(), but returns a string of hexadecimal digits instead.

Definition at line 112 of file hmac.py.

00112 
00113     def hexdigest(self):
00114         """Like digest(), but returns a string of hexadecimal digits instead.
00115         """
00116         h = self._current()
00117         return h.hexdigest()

Here is the call graph for this function:

def hmac.HMAC.update (   self,
  msg 
)
Update this hashing object with the string msg.

Definition at line 73 of file hmac.py.

00073 
00074     def update(self, msg):
00075         """Update this hashing object with the string msg.
00076         """
00077         if not isinstance(msg, bytes):
00078             raise TypeError("expected bytes, but got %r" % type(msg).__name__)
00079         self.inner.update(msg)

Here is the caller graph for this function:


Member Data Documentation

int hmac.HMAC.blocksize = 64 [static]

Definition at line 21 of file hmac.py.

Definition at line 43 of file hmac.py.

Definition at line 49 of file hmac.py.

Definition at line 48 of file hmac.py.

Definition at line 47 of file hmac.py.


The documentation for this class was generated from the following file: