title: How to increment a python "bytes" object
body: <p>What is the fasted way to increment a bytes object in Python3.7?  Consider the following:</p>
  
  <pre><code>&gt;&gt;&gt; foo = '00000000000000000000000000000000'
  &gt;&gt;&gt; bar = binascii.unhexlify(foo)
  &gt;&gt;&gt; bar
  b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  </code></pre>
  
  <p>I want to increment <code>bar</code> by 1, resulting in <code>b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'</code>.</p>
  
  <p>If I try <code>bar + 1</code>, I get:</p>
  
  <pre><code>&gt;&gt;&gt; bar + 1
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: can't concat int to bytes
  </code></pre>
  
  <p>If I try <code>bar + b'1'</code>, I get:</p>
  
  <pre><code>&gt;&gt;&gt; bar + b'1'
  b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x001'
  </code></pre>
  
  <p>I know there must be a faster way to loop over the hexadecimal values than by going back to <code>foo</code> (a string), converting it to an int, incrementing it, converting back to a string, then doing <code>binascii.unhexlify</code> on it.  But I'm not familiar with these bytes objects.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The ultimate usage I'm aiming for is to loop over many values of <code>plaintext</code> below, while keeping <code>KEY</code> constant.  I want to take <code>plaintext</code> 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.</p>
  
  <pre><code>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')
  </code></pre>