0

如何在 C 中使用 char 数组而不是 int 随机种子?
我想使用密码,而不是数字,但srand只需要整数。
有没有办法做到这一点?

4

1 回答 1

0

只需使用哈希函数。一个经典是hash_pjw

unsigned hash_pjw (const void *str)   
{
  const char *s = str;
  unsigned int g, h = 1234567u;
  while (*s != 0) {
    h = (h << 4) + *s++;
    if ((g = h & (unsigned int) 0xf0000000) != 0)
       h = (h ^ (g >> 24)) ^ g;
  }
  return h;
}

请注意,这rand()与加密安全完全不同。

于 2014-04-13T01:24:15.220 回答