llHash

From Second Life Wiki
Revision as of 13:39, 5 May 2021 by Rider Linden (talk | contribs) (Created page with "{{LSL Function |func_sleep=0.0|func_energy=10.0 |func=llHash|sort=Hash |func_desc=Returns a 32bit hash for the provided string. |return_type=integer |p1_type=string|p1_name=va...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Summary

Function: integer llHash( string val );

Returns a 32bit hash for the provided string.
Returns an integer

• string val String to hash.

Specification

The SDBM algorithm provides a good general purpose hash function with a fairly even distribution across the 32 bit space in the general case. The characters fed into the hash function are 32bit wide.

U32 SDBMHash(const std::wstring &val)
{
    U32 hash(0);

    for(const wchar_t &c: val)
    {
        hash = c + (hash << 6) + (hash << 16) - hash;
    }
    return hash;
}

In LSL:

integer llSDBMHash(string value)
{
    integer hash = 0;
    integer index = 0;
    for (index = 0; index < llStringLength(value); ++index)
    {
        hash = llOrd(value, index) + (hash << 6) + (hash << 16) - hash;
    }
    
    return hash;
}

Caveats

This hash value is not cryptographically secure and should not be used as part of any security protocol. SDBM provides a good distribution of hash values across its range, however with only 32 bits the chance of a collision is unacceptably high.(with 1000 entries, the odds for a collision are about 1 in 10000.)

All Issues ~ Search JIRA for related Bugs

Examples

See Also

Functions

• llOrd Convert a character into an ordinal
• llChar Convert an ordinal into a character

Articles

•  "Wikipedia logo"List of Hash Functions
•  Hash Collision Probabilities

Deep Notes

Search JIRA for related Issues

Signature

function integer llHash( string val );