home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / hmac.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  2.6 KB  |  78 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import warnings as _warnings
  5. trans_5C = ''.join([ chr(x ^ 92) for x in xrange(256) ])
  6. trans_36 = ''.join([ chr(x ^ 54) for x in xrange(256) ])
  7. digest_size = None
  8. _secret_backdoor_key = []
  9.  
  10. class HMAC:
  11.     blocksize = 64
  12.     
  13.     def __init__(self, key, msg = None, digestmod = None):
  14.         if key is _secret_backdoor_key:
  15.             return None
  16.         if None is None:
  17.             import hashlib
  18.             digestmod = hashlib.md5
  19.         if hasattr(digestmod, '__call__'):
  20.             self.digest_cons = digestmod
  21.         else:
  22.             
  23.             self.digest_cons = lambda d = ('',): digestmod.new(d)
  24.         self.outer = self.digest_cons()
  25.         self.inner = self.digest_cons()
  26.         self.digest_size = self.inner.digest_size
  27.         if hasattr(self.inner, 'block_size'):
  28.             blocksize = self.inner.block_size
  29.             if blocksize < 16:
  30.                 _warnings.warn('block_size of %d seems too small; using our default of %d.' % (blocksize, self.blocksize), RuntimeWarning, 2)
  31.                 blocksize = self.blocksize
  32.             
  33.         else:
  34.             _warnings.warn('No block_size attribute on given digest object; Assuming %d.' % self.blocksize, RuntimeWarning, 2)
  35.             blocksize = self.blocksize
  36.         if len(key) > blocksize:
  37.             key = self.digest_cons(key).digest()
  38.         key = key + chr(0) * (blocksize - len(key))
  39.         self.outer.update(key.translate(trans_5C))
  40.         self.inner.update(key.translate(trans_36))
  41.         if msg is not None:
  42.             self.update(msg)
  43.  
  44.     
  45.     def update(self, msg):
  46.         self.inner.update(msg)
  47.  
  48.     
  49.     def copy(self):
  50.         other = self.__class__(_secret_backdoor_key)
  51.         other.digest_cons = self.digest_cons
  52.         other.digest_size = self.digest_size
  53.         other.inner = self.inner.copy()
  54.         other.outer = self.outer.copy()
  55.         return other
  56.  
  57.     
  58.     def _current(self):
  59.         h = self.outer.copy()
  60.         h.update(self.inner.digest())
  61.         return h
  62.  
  63.     
  64.     def digest(self):
  65.         h = self._current()
  66.         return h.digest()
  67.  
  68.     
  69.     def hexdigest(self):
  70.         h = self._current()
  71.         return h.hexdigest()
  72.  
  73.  
  74.  
  75. def new(key, msg = None, digestmod = None):
  76.     return HMAC(key, msg, digestmod)
  77.  
  78.