在Python中创建保存会话

2024-04-25 07:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

使用如下创建的会话变量是否安全:

base64.b64encode(os.urandom(256))

urandom据说能带来很好的随机性。base64则只对该字符串进行编码。这种论证正确吗?因此,这种方法是安全的吗?你知道吗


@Qiau指出,os.urandom取决于操作系统的实现。在我的例子中(脚本在Google App Engine上运行),这意味着使用(see Google Groups post)是安全的。你知道吗


Tags: 方法字符串脚本app编码osgoogleurandom
1条回答
网友
1楼 · 发布于 2024-04-25 07:32:08

是的。这是正确的。base64只对字符串进行编码。你知道吗

根据文件:

urandom(...)

   urandom(n) -> str

   Return n random bytes suitable for cryptographic use.

b64encode(s, altchars=None)

   Encode a string using Base64.

根据documentation:,它对于加密应用程序来说应该是不可预测的

This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, though its exact quality depends on the OS implementation. On a UNIX-like system this will query /dev/urandom, and on Windows it will use CryptGenRandom(). If a randomness source is not found, NotImplementedError will be raised.

相关问题 更多 >