在Linux / POSIX中,什么是Windows rand_s的最佳替代品? [英] What is the best replacement for Windows' rand_s in Linux/POSIX?

查看:143
本文介绍了在Linux / POSIX中,什么是Windows rand_s的最佳替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题不在于随机性本身(我们有兰特),而是在于加密安全的PRNG中。在Linux或理想的POSIX上可以使用什么? NSS有有用的东西吗?

The problem is not about randomness itself (we have rand), but in cryptographically secure PRNG. What can be used on Linux, or ideally POSIX? Does NSS have something useful?

说明:我知道/ dev / random,但是它可能用完了熵池。而且我不确定/ dev / urandom是否保证一定是加密安全的。

Clarification: I know about /dev/random, but it may run out of entropy pool. And I'm not sure whether /dev/urandom is guaranteed to be cryptographically secure.

推荐答案

使用 / dev / random (需要用户输入,例如鼠标移动)或 / dev / urandom 。后者具有一个熵池,除非该池为空,否则不需要任何用户输入。

Use /dev/random (requires user input, eg mouse movements) or /dev/urandom. The latter has an entropy pool and doesn't require any user input unless the pool is empty.

您可以像这样从池中读取内容:

You can read from the pool like this:

char buf[100];
FILE *fp;
if (fp = fopen("/dev/urandom", "r")) {
    fread(&buf, sizeof(char), 100, fp);
    fclose(fp);
}

或类似的东西。

这篇关于在Linux / POSIX中,什么是Windows rand_s的最佳替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆