import os
import sha
import sys

class EncoderDecoder(object):
    def create_random_block(self,piece,blocksize=16384):
        if len(piece) % blocksize != 0:
            raise Exception('size error')
        self.N = len(piece)/blocksize
        random_block = ['0']*blocksize
        bitlist = [1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1]
        for i in range(len(bitlist)):
            if bitlist[i] == 1:
                block = piece[i*blocksize:i*blocksize+blocksize]
                print '%d-%d   %s' %(i*blocksize,i*blocksize+blocksize,sha.new(block).hexdigest())
                for j in range(blocksize):
                    random_block[j] = chr(ord(random_block[j]) ^ ord(block[j]))
        print sha.new(''.join(random_block)).hexdigest()
        return ''.join(random_block)
   
if __name__ == '__main__':
    data = open('test.img','rb').read()
    x = EncoderDecoder()
    x.create_random_block(data)
    sys.exit(0)
