gpt4 book ai didi

c - 如何在 C 语言中使用 ISAAC

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:47 26 4
gpt4 key购买 nike

我从 here 下载了 isaac64我在使用上遇到了一些问题。我不得不评论 isaac64.c 中的一部分代码,因为它包含主要功能。但我不能使用它...我无法正确初始化它并获取随机数,你能帮我吗?我找不到任何例子。

    randinit(TRUE);     for(i=0;i<10;i++){    printf("%lx\n",rand());    }
每次运行此代码时,我都会得到相同的值。我不知道如何设置种子。

最佳答案

此版本的 ISAAC 是一个“引用实现”,意味着它旨在被引用,但不是特别用户友好或准备好用于生产。有任意数量的 cryptographically secure random number generators在 C 中更易于使用。特别是,简单地从 /dev/random 中读取字节在大多数操作系统上都足够好。


main 函数演示了如何使用该库。它已经用 #ifdef 注释掉了。我找到了 using the Perl wrapper around ISAAC as a guide也有帮助。

int main()
{
/* Initialize the structure to 0 */
randctx ctx;
ctx.randa = ctx.randb = ctx.randc = (ub4)0;

/* Initialize the seed */
for (ub4 i=0; i<256; ++i) {
ctx.randrsl[i] = i;
}

/* Initialize the random numbers from the seed */
randinit(&ctx, TRUE);

/* Print 10 pseudo random numbers */
for(int i=0; i<10; i++) {
printf("%.8lx\n", rand(&ctx));
}
}

您必须提供种子,或"entropy" .如果您提供相同的种子,您将获得相同的结果。这就是为什么 ISAAC 是一个随机数生成器,它只能从现有的随机性中生成更多看似随机的数字。有 a discussion of where to get your seed from in Math::Random::ISAAC .

如果这些对您来说是新概念,并且如果这是用于生产代码,我会强烈建议您直接阅读 /dev/random。这个 ISAAC 实现非常粗糙,密码学很容易出错。 /dev/random 已经为您处理了所有这一切。

如果您必须使用自己的伪随机库,请使用实现良好且有文档记录的库,例如 OpenSSL .

如果您要完成此操作,我建议您调整 the version of the code from the Perl wrapper因为至少作者已经对其进行了清理,使其值得发布。

关于c - 如何在 C 语言中使用 ISAAC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33402981/

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