- DJB2 String Hashing Algorithm Implemented in Erlang Wed, 29 Jul 2009 15:53:45 -0400
-
Erlang is a very interesting functional programming language that has been receiving a fair amount of attention lately. So I've been playing with it off and on as my little fun project for educating myself. So to give back here is a simple DJB2 hashing algorithm implemented in Erlang.
Something I have had problems with before when implementing DJB2 in those languages (such as Python) is the native infinite precision integers of those languages. However by bitwise AND logic the result of the hash against the maximum value of a C unsigned long negates that by truncating the result buffer so we remain using the proper number of bits for the hashing.
-module(djb2). -author('Brian Smith http://www.modernninja.com/'). -export([hash/1, djb2/2]). -define(INIT_HASH, 5381). hash(String) -> djb2(String, ?INIT_HASH). djb2([], Hash) -> Hash; djb2([Head|Rest], Hash) -> djb2(Rest, (((Hash bsl 5) + Hash) + Head) band 16#FFFFFFFF).
Post Comment