MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / djb2

Function djb2

hashes/djb2.py:21–35  ·  view source on GitHub ↗

Implementation of djb2 hash algorithm that is popular because of it's magic constants. >>> djb2('Algorithms') 3782405311 >>> djb2('scramble bits') 1609059040

(s: str)

Source from the content-addressed store, hash-verified

19
20
21def djb2(s: str) -> int:
22 """
23 Implementation of djb2 hash algorithm that
24 is popular because of it's magic constants.
25
26 >>> djb2('Algorithms')
27 3782405311
28
29 >>> djb2('scramble bits')
30 1609059040
31 """
32 hash_value = 5381
33 for x in s:
34 hash_value = ((hash_value << 5) + hash_value) + ord(x)
35 return hash_value & 0xFFFFFFFF

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected