覺得我們的內容實用嗎?MyApollo 電子報讀者募集中!歡迎訂閱電子報!
做加密時常常不可或缺的就是 salt ,以下列幾種產生 salt 的方法。
os.urandom(n)#
最簡單的方法就是用 os 的 urandom(n) 產生 salt , n 是 bytes 數。
Return a string of n random bytes suitable for cryptographic use.
而 urandom(n) 的說明就已經提到適合用來做加密用途,所以可以放心使用。
>>> import os
>>> os.urandom(16)
'#^\xf8G>\xc3.\xcam8Ub\xc1\x0f\x96<'
random.choice(seq)#
那如果只是要簡單的 random string 可以用 string 加 random 模組產生:
>>> import random
>>> import string
>>> s = "".join([string.digits, string.letters, string.punctuation])
# s = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> "".join([random.choice(s) for i in range(50)])
'RDMU23\\0WyjycMzvFqjHF?-/PhW*u`WK[ze42(En-4>WCMFz>d'