this algorithm k33 was first reported by dan bernstein many years ago in comp lang c another version of this algorithm now favored by bernstein uses xor hashi hashi 1 33 stri first magic constant 33 it has never been adequately explained it s magic because it works better than many other constants prime or not second magic constant 5381 1 odd number 2 prime number 3 deficient number 4 001010100000101 b source http www cse yorku caozhash html implementation of djb2 hash algorithm that is popular because of it s magic constants djb2 algorithms 3782405311 djb2 scramble bits 1609059040 implementation of djb2 hash algorithm that is popular because of it s magic constants djb2 algorithms 3782405311 djb2 scramble bits 1609059040
def djb2(s: str) -> int:
    
    hash_value = 5381
    for x in s:
        hash_value = ((hash_value << 5) + hash_value) + ord(x)
    return hash_value & 0xFFFFFFFF