如何向ISAAC随机数发生器提供种子

时间:2016-04-03 05:44:31

标签: c random cryptography

我有以下代码使用ISAAC生成随机数:

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);

    printf("%.8lx\n", rand(&ctx));
}

我从How to use ISAAC in C

获得了上述代码

我还有一个代码可以从/ dev / random读取以获取种子:

int myFile = open("/dev/random", O_RDONLY);            
uint32_t rand;            
uint32_t randomNum = read(myFile, &rand, sizeof(rand)) ;
printf(" %u \n", rand);
close(myFile);

如何向ISAAC供应种子?

由于

1 个答案:

答案 0 :(得分:1)

TRUE传递给randinit的标志时,种子将从ctx.randrsl中获取。但我建议您使用RANDSIZsizeof(ctx.randrsl),而不是将其值256硬编码。

所以:

ssize_t bytes_read = read(myFile, &ctx.randrsl, sizeof(ctx.randrsl));
if(bytes_read != sizeof(ctx.randrsl)){ printf("cannot initialize seed\n"); exit(1); }
相关问题