What is the fasted way to increment a bytes object in Python3.7? Consider the following:
>>> foo = '00000000000000000000000000000000'
>>> bar = binascii.unhexlify(foo)
>>> bar
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
I want to increment bar by 1, resulting in b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'.
If I try bar + 1, I get:
>>> bar + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't concat int to bytes
If I try bar + b'1', I get:
>>> bar + b'1'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x001'
I know there must be a faster way to loop over the hexadecimal values than by going back to foo (a string), converting it to an int, incrementing it, converting back to a string, then doing binascii.unhexlify on it. But I'm not familiar with these bytes objects.
EDIT
The ultimate usage I'm aiming for is to loop over many values of plaintext below, while keeping KEY constant. I want to take plaintext from '0000000000000000000000000000000' to 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' and compute each value. I imagine it will take several years to complete, but I'm curious how fast it will go.
from Crypto.Cipher import AES
import binascii
KEY = binascii.unhexlify('AAAABBBBCCCCDDDDEEEEFFFF00001111')
plaintext = binascii.unhexlify('11112222333344445555666677778888')
rijn = AES.new(KEY, AES.MODE_ECB)
ciphertext = rijn.encrypt(plaintext)
binascii.hexlify(ciphertext).decode('utf-8')