When I make a Python string of one character, i.e.
txt = "h"
and I check its size, I get
import sys
sys.getsizeof(txt)
34
If one character is 34 bytes, how do I make a String of 16 byes?
P.S. I'm playing around with PyCrypto AES, in OFB Mode, which required input of 16 bytes
getsizeof
does not return the actual size of the string but it accounts for additional garbage collector overhead. As shown bellowtest
starts as an empty string but memory allocated for it is not zero due to reasons listed above.To create a 16 bytes string, one way would be:
53 - 37 = 16 bytes
sys.getsizeof(obj)
returns the size of the object including internal values. This contains more than the characters or bytes of a string.If you want to know how long a
bytes
object or astr
is, then you can calllen(byteString)
orlen(string)
.I'm assuming that you want to use generate a random IV or key. This can be done in this way:
This invokes a CSPRNG if the system has enough entropy. You can also use
AES.block_size
instead of16
.