Srand com array char em C?
-
21-12-2019 - |
Pergunta
Como posso propagar aleatoriamente com um array char em vez de um int em C?
Quero usar uma senha, não um número, mas srand
leva apenas números inteiros.
Existe uma maneira de fazer isso?
Solução
Basta usar uma função hash.Um clássico é 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;
}
Observe que rand()
não é nada como criptograficamente seguro.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow