gpt4 book ai didi

PostgreSQL/开发/随机数

转载 作者:行者123 更新时间:2023-11-29 12:35:46 27 4
gpt4 key购买 nike

是否有一个 PostgreSQL 函数可以用来通过 /dev/urandom 生成 160 位随机数?

我们想要生成一个访问 token 。

根据OAuth 2.0 Authorization Framework: 10.10. Credentials-Guessing Attacks :

The probability of an attacker guessing generated tokens (and other credentials not intended for handling by end-users) MUST be less than or equal to 2^(-128) and SHOULD be less than or equal to 2^(-160).

最佳答案

如 pozs 所说,您可以使用 pgcrypto 中的 gen_random_bytes(int)贡献模块。

此函数从 src/port/pg_strong_random.c 调用 pg_strong_random如果返回代码为 false,则抛出错误。

评论解释了 pg_strong_random 是如何工作的:

 * Generate requested number of random bytes. The returned bytes are
* cryptographically secure, suitable for use e.g. in authentication.
*
* We rely on system facilities for actually generating the numbers.
* We support a number of sources:
*
* 1. OpenSSL's RAND_bytes()
* 2. Windows' CryptGenRandom() function
* 3. /dev/urandom
*
* The configure script will choose which one to use, and set
* a USE_*_RANDOM flag accordingly.
*
* Returns true on success, and false if none of the sources
* were available. NB: It is important to check the return value!

您可以查看 PostgreSQL 安装中的 include/pg_config.h 并查看使用了哪个随机数源。

如果您使用的是 Linux,您可能会使用 OpenSSL 作为随机源。

manual page for RAND_bytes声明:

RAND_bytes() puts num cryptographically strong pseudo-random bytes into buf.

我没有深入研究 OpenSSL 源代码,因为那真的很伤人,但本质上,如果您信任 OpenSSL,您也可以信任 pgcrypto

关于PostgreSQL/开发/随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44568372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com