2016-12-20 4 views
0

ランダムな文字列を取得しようとしています。ランダムバイトからストリング型への変換

#! python3 

from os import urandom 
from base64 import b64encode 
from sys import getsizeof 

random_bytes = urandom(16) 
salt = b64encode(random_bytes).decode('utf-8') 

print("Random Bytes:", random_bytes) 
print(type(random_bytes)) 
print(getsizeof(random_bytes)) 
print(len(random_bytes)) 

print("") 
print("Salt:", salt) 
print(type(salt)) 
print(getsizeof(salt)) 
print(len(salt)) 

以下は、私が理解できない出力の例です。

Random Bytes: b'.v\x9c\xa0\xda\xa4\[email protected]\xfc>\xb1\xccZ)\xff' 
<class 'bytes'> 
33 
16 

Salt: LnacoNqkkkBk/D6xzFop/w== 
<class 'str'> 
49 
24 

デコードされた文字列は24バイトであるのに対し、なぜランダムバイトで出力のな長さは16バイトでありますか? どのgetsizeofが表していますか?

答えて

1

b64encodeもまた、in thisの質問とthe appropriate section of RFC 3548で指定されているように値を埋めます。

出力値は4 * math.ceil(n/3)であるため、n == 16の場合は24になります。

getsizeofはそれぞれ、bytesおよびstrオブジェクトのメモリサイズを表します。

関連する問題