home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2244 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.0 KB  |  105 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __revision__ = '$Id: Chaffing.py,v 1.7 2003/02/28 15:23:21 akuchling Exp $'
  5. from Crypto.Util.number import bytes_to_long
  6.  
  7. class Chaff:
  8.     
  9.     def __init__(self, factor = 1, blocksper = 1):
  10.         if factor <= factor:
  11.             pass
  12.         elif not factor <= 1:
  13.             raise ValueError, "'factor' must be between 0.0 and 1.0"
  14.         
  15.         if blocksper < 0:
  16.             raise ValueError, "'blocksper' must be zero or more"
  17.         blocksper < 0
  18.         self._Chaff__factor = factor
  19.         self._Chaff__blocksper = blocksper
  20.  
  21.     
  22.     def chaff(self, blocks):
  23.         chaffedblocks = []
  24.         count = len(blocks) * self._Chaff__factor
  25.         blocksper = range(self._Chaff__blocksper)
  26.         for i, wheat in map(None, range(len(blocks)), blocks):
  27.             if i < count:
  28.                 (serial, data, mac) = wheat
  29.                 datasize = len(data)
  30.                 macsize = len(mac)
  31.                 addwheat = 1
  32.                 for j in blocksper:
  33.                     import sys
  34.                     chaffdata = self._randnum(datasize)
  35.                     chaffmac = self._randnum(macsize)
  36.                     chaff = (serial, chaffdata, chaffmac)
  37.                     if addwheat and bytes_to_long(self._randnum(16)) & 64:
  38.                         chaffedblocks.append(wheat)
  39.                         addwheat = 0
  40.                     
  41.                     chaffedblocks.append(chaff)
  42.                 
  43.                 if addwheat:
  44.                     chaffedblocks.append(wheat)
  45.                 
  46.             addwheat
  47.             chaffedblocks.append(wheat)
  48.         
  49.         return chaffedblocks
  50.  
  51.     
  52.     def _randnum(self, size):
  53.         randpool = randpool
  54.         import Crypto.Util
  55.         import time
  56.         pool = randpool.RandomPool(size * 2)
  57.         while size > pool.entropy:
  58.             pass
  59.         return pool.get_bytes(size)
  60.  
  61.  
  62. if __name__ == '__main__':
  63.     text = 'We hold these truths to be self-evident, that all men are created equal, that\nthey are endowed by their Creator with certain unalienable Rights, that among\nthese are Life, Liberty, and the pursuit of Happiness. That to secure these\nrights, Governments are instituted among Men, deriving their just powers from\nthe consent of the governed. That whenever any Form of Government becomes\ndestructive of these ends, it is the Right of the People to alter or to\nabolish it, and to institute new Government, laying its foundation on such\nprinciples and organizing its powers in such form, as to them shall seem most\nlikely to effect their Safety and Happiness.\n'
  64.     print 'Original text:\n=========='
  65.     print text
  66.     print '=========='
  67.     blocks = []
  68.     size = 40
  69.     for i in range(0, len(text), size):
  70.         blocks.append(text[i:i + size])
  71.     
  72.     print 'Calculating MACs...'
  73.     from Crypto.Hash import HMAC, SHA
  74.     key = 'Jefferson'
  75.     macs = [ HMAC.new(key, block, digestmod = SHA).digest() for block in blocks ]
  76.     source = []
  77.     m = map(None, range(len(blocks)), blocks, macs)
  78.     print m
  79.     for i, data, mac in m:
  80.         source.append((i, data, mac))
  81.     
  82.     print 'Adding chaff...'
  83.     c = Chaff(factor = 0.5, blocksper = 2)
  84.     chaffed = c.chaff(source)
  85.     from base64 import encodestring
  86.     wheat = []
  87.     print 'chaffed message blocks:'
  88.     for i, data, mac in chaffed:
  89.         h = HMAC.new(key, data, digestmod = SHA)
  90.         pmac = h.digest()
  91.         if pmac == mac:
  92.             tag = '-->'
  93.             wheat.append(data)
  94.         else:
  95.             tag = '   '
  96.         print tag, '%3d' % i, repr(data), encodestring(mac)[:-1]
  97.     
  98.     print 'Undigesting wheat...'
  99.     newtext = ''.join(wheat)
  100.     if newtext == text:
  101.         print 'They match!'
  102.     else:
  103.         print 'They differ!'
  104.  
  105.