A continuación se muestra la implementación de la función hash de la biblioteca roguewave. Devuelve un hash de 32 bits. La operación principal es A = B ^ ((C < < 5) | (C > > 27)). ¿Es seguro usar este hash como verificación de contraseña o es posible recuperar todas las B usadas invirtiéndolas?
const unsigned RW_HASH_SHIFT = 5;
inline static void mash(unsigned& hash, unsigned chars)
{
hash = (chars ^
((hash << RW_HASH_SHIFT) |
(hash >> (RWBITSPERBYTE*sizeof(unsigned) - RW_HASH_SHIFT))));
}
unsigned
RWCStringRef::hash() const
{
unsigned hv = (unsigned)length(); // Mix in the string length.
unsigned i = length()*sizeof(char)/sizeof(unsigned);
const unsigned* p = (const unsigned*)data();
{
while (i--)
mash(hv, *p++); // XOR in the characters.
}
// XOR in any remaining characters:
if ((i = length()*sizeof(char)%sizeof(unsigned)) != 0) {
unsigned h = 0;
const char* c = (const char*)p;
while (i--)
h = ((h << RWBITSPERBYTE*sizeof(char)) | *c++);
mash(hv, h);
}
return hv;
}