[英]Random bytes to string type conversion
嘗試獲取使用urandom的隨機字符串。
#! 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\x92@d\xfc>\xb1\xccZ)\xff'
<class 'bytes'>
33
16
Salt: LnacoNqkkkBk/D6xzFop/w==
<class 'str'>
49
24
為什么輸出的隨機字節長度是16字節而解碼的字符串是24字節? getsizeof代表什么?
因為b64encode
也b64encode
該值,所以如本問題所討論以及RFC 3548的相應部分中所指定。
輸出值是4 * math.ceil(n/3)
長,因此,在n == 16
情況下等於24
。
getsizeof
表示bytes
和str
對象的內存大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.