1

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

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • 2
    create an array of bytes, not a string – R Nar Oct 30 '15 at 19:17
  • 1
    sizeof includes the object header which shouldn't matter at all. Now if you're using python3, getting an exact bytecount is still non-trivial (encoding changes depending on the content of the string) but then again, if you're not working on byte arrays for encryption you're doing it wrong. – Voo Oct 30 '15 at 19:18
  • 1
    a string that is 16 characters long is said to be 16 bytes (in python2) ... python3 changes some things – Joran Beasley Oct 30 '15 at 19:27

2 Answers2

4

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 a str is, then you can call len(byteString) or len(string).

I'm assuming that you want to use generate a random IV or key. This can be done in this way:

from Crypto import Random
iv = Random.new().read(16)

This invokes a CSPRNG if the system has enough entropy. You can also use AES.block_size instead of 16.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • See [here](http://stackoverflow.com/a/6273618) for differences between bytes and str between Python 2 and Python 3. – Artjom B. Oct 30 '15 at 20:09
1

getsizeof does not return the actual size of the string but it accounts for additional garbage collector overhead. As shown bellow test starts as an empty string but memory allocated for it is not zero due to reasons listed above.

>>> test = ""
>>> sys.getsizeof(test)
37
>>> test = "" + "h"
>>> sys.getsizeof(test)
38
>>> test = "" + "h" + "h"
>>> sys.getsizeof(test)
39
>>>

To create a 16 bytes string, one way would be:

>>> test = " " * 16
>>> sys.getsizeof(test)
53

53 - 37 = 16 bytes

flamenco
  • 2,702
  • 5
  • 30
  • 46