def test_byte_basic(self): """ Test basic put_byte and get_byte behavior. """ bitstream = BitStream() # Put a couple of bytes in the stream: bytes = [ 12, # 00001100 222, # 11011110 145, # 10010001 42, # 00101010 0, # 00000000 255] # 11111111 for byte in bytes: bitstream.put_byte(byte) # Sanity check: self.assertEquals(bitstream.get_length(),len(bytes)*8) self.assertEquals(bitstream.get_current_pos(),len(bytes)*8) # Read the bytes back from the stream bitstream.seek(0) for byte in bytes: self.assertEquals(bitstream.get_byte(), byte) # Read some bits from the stream, interpreting them as bytes, but # without restricting ourselves to 8-bit aligned bytes # e.g. read the "byte" defined by bits #4 to #12 bitstream.seek(4) self.assertEquals(bitstream.get_byte(), 205) # 11001101 bitstream.seek(19) self.assertEquals(bitstream.get_byte(), 137) # 10001001
def test_multiformat_write_multiformat_read(self): """ This test writes numeric, byte and string data to a stream and then reads the whole stream as binary, hex and base64 data, ensuring that the output is the expected one in each case. """ # Write a number, 2 bytes and a string bitstream = BitStream() bitstream.put_num(10438341575639894917, 64) bitstream.put_byte(230) bitstream.put_byte(191) bitstream.put_string("ÄäÜüßTestЯБГДЖЙŁĄŻStringĘĆŃŚŹてす" \ "とアイウエオカキク4234ケコサシスセソタチツテ") # Read in binary, hex and base64 formats expected_bits = \ "1001000011011100011100000101101110111100001101010000101110000101" \ "1110011010111111110000111000010011000011101001001100001110011100" \ "1100001110111100110000111001111101010100011001010111001101110100" \ "1101000010101111110100001001000111010000100100111101000010010100" \ "1101000010010110110100001001100111000101100000011100010010000100" \ "1100010110111011010100110111010001110010011010010110111001100111" \ "1100010010011000110001001000011011000101100000111100010110011010" \ "1100010110111001111000111000000110100110111000111000000110011001" \ "1110001110000001101010001110111110111101101100011110111110111101" \ "1011001011101111101111011011001111101111101111011011010011101111" \ "1011110110110101111011111011110110110110111011111011110110110111" \ "1110111110111101101110000011010000110010001100110011010011101111" \ "1011110110111001111011111011110110111010111011111011110110111011" \ "1110111110111101101111001110111110111101101111011110111110111101" \ "1011111011101111101111011011111111101111101111101000000011101111" \ "1011111010000001111011111011111010000010111011111011111010000011" expected_hex = \ "90dc705bbc350b85e6bfc384c3a4c39cc3bcc39f54657374d0afd091d093d094" \ "d096d099c581c484c5bb537472696e67c498c486c583c59ac5b9e381a6e38199" \ "e381a8efbdb1efbdb2efbdb3efbdb4efbdb5efbdb6efbdb7efbdb834323334ef" \ "bdb9efbdbaefbdbbefbdbcefbdbdefbdbeefbdbfefbe80efbe81efbe82efbe83" expected_base64 = \ "kNxwW7w1C4Xmv8OEw6TDnMO8w59UZXN00K/QkdCT0JTQltCZxYHEhMW7U3RyaW5n" \ "xJjEhsWDxZrFueOBpuOBmeOBqO+9se+9su+9s++9tO+9te+9tu+9t++9uDQyMzTv" \ "vbnvvbrvvbvvvbzvvb3vvb7vvb/vvoDvvoHvvoLvvoM=" bitstream.seek(0) self.assertEquals( \ bitstream.get_bit_dump_string(bitstream.get_length()), expected_bits) bitstream.seek(0) self.assertEquals( bitstream.get_hex(bitstream.get_length()).lower(), expected_hex) bitstream.seek(0) self.assertEquals(bitstream.get_base64(bitstream.get_length()), expected_base64)
def test_multiformat_write_multiformat_read(self): """ This test writes numeric, byte and string data to a stream and then reads the whole stream as binary, hex and base64 data, ensuring that the output is the expected one in each case. """ # Write a number, 2 bytes and a string bitstream = BitStream() bitstream.put_num(10438341575639894917, 64) bitstream.put_byte(230) bitstream.put_byte(191) bitstream.put_string("ÄäÜüßTestЯБГДЖЙŁĄŻStringĘĆŃŚŹてす" \ "とアイウエオカキク4234ケコサシスセソタチツテ") # Read in binary, hex and base64 formats expected_bits = \ "1001000011011100011100000101101110111100001101010000101110000101" \ "1110011010111111110000111000010011000011101001001100001110011100" \ "1100001110111100110000111001111101010100011001010111001101110100" \ "1101000010101111110100001001000111010000100100111101000010010100" \ "1101000010010110110100001001100111000101100000011100010010000100" \ "1100010110111011010100110111010001110010011010010110111001100111" \ "1100010010011000110001001000011011000101100000111100010110011010" \ "1100010110111001111000111000000110100110111000111000000110011001" \ "1110001110000001101010001110111110111101101100011110111110111101" \ "1011001011101111101111011011001111101111101111011011010011101111" \ "1011110110110101111011111011110110110110111011111011110110110111" \ "1110111110111101101110000011010000110010001100110011010011101111" \ "1011110110111001111011111011110110111010111011111011110110111011" \ "1110111110111101101111001110111110111101101111011110111110111101" \ "1011111011101111101111011011111111101111101111101000000011101111" \ "1011111010000001111011111011111010000010111011111011111010000011" expected_hex = \ "90dc705bbc350b85e6bfc384c3a4c39cc3bcc39f54657374d0afd091d093d094" \ "d096d099c581c484c5bb537472696e67c498c486c583c59ac5b9e381a6e38199" \ "e381a8efbdb1efbdb2efbdb3efbdb4efbdb5efbdb6efbdb7efbdb834323334ef" \ "bdb9efbdbaefbdbbefbdbcefbdbdefbdbeefbdbfefbe80efbe81efbe82efbe83" expected_base64 = \ "kNxwW7w1C4Xmv8OEw6TDnMO8w59UZXN00K/QkdCT0JTQltCZxYHEhMW7U3RyaW5n" \ "xJjEhsWDxZrFueOBpuOBmeOBqO+9se+9su+9s++9tO+9te+9tu+9t++9uDQyMzTv" \ "vbnvvbrvvbvvvbzvvb3vvb7vvb/vvoDvvoHvvoLvvoM=" bitstream.seek(0) self.assertEquals( \ bitstream.get_bit_dump_string(bitstream.get_length()), expected_bits) bitstream.seek(0) self.assertEquals(bitstream.get_hex(bitstream.get_length()).lower(), expected_hex) bitstream.seek(0) self.assertEquals(bitstream.get_base64(bitstream.get_length()), expected_base64)
def test_get_byte_beyond_eos(self): """ Test that trying to read beyond the end of the stream raises an exception when calling get_byte(...). """ bitstream = BitStream() self.assertRaises(NotEnoughBitsInStreamError, bitstream.get_byte) # Current position should not have been changed self.assertEquals(bitstream.get_current_pos(), 0) bitstream.put_byte(14) bitstream.seek(1) # Read a single bit beyond EOS self.assertRaises(NotEnoughBitsInStreamError, bitstream.get_byte) # Current position should not have been changed self.assertEquals(bitstream.get_current_pos(), 1)
def test_get_byte_beyond_eos(self): """ Test that trying to read beyond the end of the stream raises an exception when calling get_byte(...). """ bitstream = BitStream() self.assertRaises(NotEnoughBitsInStreamError, bitstream.get_byte) # Current position should not have been changed self.assertEquals(bitstream.get_current_pos(),0) bitstream.put_byte(14) bitstream.seek(1) # Read a single bit beyond EOS self.assertRaises(NotEnoughBitsInStreamError, bitstream.get_byte) # Current position should not have been changed self.assertEquals(bitstream.get_current_pos(),1)
def test_byte_basic(self): """ Test basic put_byte and get_byte behavior. """ bitstream = BitStream() # Put a couple of bytes in the stream: bytes = [ 12, # 00001100 222, # 11011110 145, # 10010001 42, # 00101010 0, # 00000000 255 ] # 11111111 for byte in bytes: bitstream.put_byte(byte) # Sanity check: self.assertEquals(bitstream.get_length(), len(bytes) * 8) self.assertEquals(bitstream.get_current_pos(), len(bytes) * 8) # Read the bytes back from the stream bitstream.seek(0) for byte in bytes: self.assertEquals(bitstream.get_byte(), byte) # Read some bits from the stream, interpreting them as bytes, but # without restricting ourselves to 8-bit aligned bytes # e.g. read the "byte" defined by bits #4 to #12 bitstream.seek(4) self.assertEquals(bitstream.get_byte(), 205) # 11001101 bitstream.seek(19) self.assertEquals(bitstream.get_byte(), 137) # 10001001
# Open the input file print "Reading input file..." try: in_f = open(in_file, 'rb') except Exception, e: print "Problem while opening input file %s: %s" % (in_file, e) # Read the whole file into a bitstream bitstream = BitStream() try: read_quantum = 1024 # KB at a time bytes = in_f.read(read_quantum) while (bytes): for byte in bytes: bitstream.put_byte(ord(byte)) bytes = in_f.read(read_quantum) except Exception, e: print "Problem while reading from input file %s: %s" % (in_file, e) in_f.close() # Define callbacks for the TaskMonitor for monitoring the encryption process if (len(in_file) <= 50): short_in_filename = in_file else: short_in_filename = os.path.split(in_file)[-1] if (len(short_in_filename) > 50): # Do ellipsis shortening short_in_filename = short_in_filename[0,20] + "..." + \ short_in_filename[-20,-1]
def run_tool(key_file, in_file, out_file): """ Runs the plonevote.encrypt tool and encrypts in_file into out_file. """ # Load the public key print("Loading public key...") try: public_key = PublicKey.from_file(key_file) except InvalidPloneVoteCryptoFileError as e: print("Invalid public key file (%s): %s" % (key_file, e.msg)) sys.exit(2) # Open the input file print("Reading input file...") try: in_f = open(in_file, 'rb') except Exception as e: print("Problem while opening input file %s: %s" % (in_file, e)) # Read the whole file into a bitstream bitstream = BitStream() try: read_quantum = 1024 # KB at a time bytes = in_f.read(read_quantum) while (bytes): for byte in bytes: bitstream.put_byte(ord(byte)) bytes = in_f.read(read_quantum) except Exception as e: print("Problem while reading from input file %s: %s" % (in_file, e)) in_f.close() # Define callbacks for the TaskMonitor for monitoring the encryption process if (len(in_file) <= 50): short_in_filename = in_file else: short_in_filename = os.path.split(in_file)[-1] if (len(short_in_filename) > 50): # Do ellipsis shortening short_in_filename = short_in_filename[0,20] + "..." + \ short_in_filename[-20,-1] def cb_task_percent_progress(task): print(" %.2f%% of %s encrypted..." % \ (task.get_percent_completed(), short_in_filename)) # Create new TaskMonitor and register the callbacks taskmon = TaskMonitor() taskmon.add_on_progress_percent_callback(cb_task_percent_progress, \ percent_span = 5) # Encrypt bitstream print("Encrypting...") ciphertext = public_key.encrypt_bitstream(bitstream, task_monitor=taskmon) # Save the ciphertext to the output file try: ciphertext.to_file(out_file) except Exception as e: print("Problem while saving the output file %s: %s" % (in_file, e.msg))
# Open the input file print "Reading input file..." try: in_f = open(in_file, 'rb') except Exception, e: print "Problem while opening input file %s: %s" % (in_file, e) # Read the whole file into a bitstream bitstream = BitStream() try: read_quantum = 1024 # KB at a time bytes = in_f.read(read_quantum) while(bytes): for byte in bytes: bitstream.put_byte(ord(byte)) bytes = in_f.read(read_quantum) except Exception, e: print "Problem while reading from input file %s: %s" % (in_file, e) in_f.close() # Define callbacks for the TaskMonitor for monitoring the encryption process if(len(in_file) <= 50): short_in_filename = in_file else: short_in_filename = os.path.split(in_file)[-1] if(len(short_in_filename) > 50): # Do ellipsis shortening short_in_filename = short_in_filename[0,20] + "..." + \ short_in_filename[-20,-1]