Skip to content
Engine

Data

Hash

Fast non-cryptographic hashes, checksums, MD5, SHA-1, SHA-256 and HMAC signatures, plus hashing of whole tables.

PackageShared

Getting it

local Hash = Engine:GetPackage("Hash")

Overview

Hash turns data (a string or a buffer) into a short fingerprint. Pick the kind you need:

  • Fast hashes (FNV32, Murmur32, Djb2) for hash tables, bucketing and quick comparisons.
  • Checksums (CRC32, Adler32) to detect accidental corruption.
  • Digests (MD5, SHA1, SHA256) for fingerprints that are practically unique.
  • HMAC to sign a message with a secret key so nobody can forge it.
local Hash = Engine:GetPackage("Hash")

print(Hash.SHA256("hello"))
--> 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

local signature = Hash.HMAC(SECRET, "userId=123&reward=500")
assert(Hash.Verify("userId=123&reward=500", signature, SECRET))

Note

MD5 and SHA-1 are fine for fingerprints and checksums, but are not considered secure against deliberate attacks. For passwords use PasswordSystem, and for real cryptography use CryptographyService.

Fast hashes

FunctionReturns
Hash.FNV32(data)32-bit FNV-1a as a number.
Hash.FNV64(data)64-bit FNV-1a as a 16-character hex string.
Hash.Djb2(data)32-bit djb2 as a number.
Hash.Murmur32(data, seed?)32-bit MurmurHash3 as a number. Different seeds give independent hashes.

Checksums

FunctionReturns
Hash.CRC32(data)CRC-32 as a number.
Hash.Adler32(data)Adler-32 as a number.

Digests

FunctionReturns
Hash.MD5(data)32 lowercase hex characters.
Hash.SHA1(data)40 lowercase hex characters.
Hash.SHA256(data)64 lowercase hex characters.

Signatures

HMAC

Hash.HMAC(key: string, msg: string, algorithm: string?): string

Signs msg with key and returns the signature as hex. algorithm is "SHA256" (default), "SHA1" or "MD5".

Verify

Hash.Verify(msg: string, sig: string, key: string, algorithm: string?): boolean

Recomputes the HMAC and compares it with sig in constant time.

Equal

Hash.Equal(a: string, b: string): boolean

Compares two strings in constant time, so an attacker cannot learn how much of a secret matched by measuring how long the comparison took.

Structural hashing

Value

Hash.Value(value: any): number

A 32-bit hash of any value, including nested tables. Tables with the same contents hash the same regardless of key insertion order. Errors on tables that contain themselves or nest deeper than MaxValueDepth.

local key = Hash.Value({ Map = "Desert", Mode = "Duel", Ranked = true })

Mix

Hash.Mix(...: number): number

Combines several numbers into one well-distributed 32-bit hash, for example a grid cell (x, y, z) into a single key.

Hex helpers

Hash.HexEncode(data: buffer | string): string
Hash.HexDecode(hex: string): buffer

Configuration

Defaults from DefaultConfiguration/Packages/Hash3 settings
SettingDefaultWhat it does
DefaultAlgorithm"SHA256"Hash used by HMAC/Verify when no algorithm is passed.
DefaultSeed0Seed Murmur32 uses when none is passed.
MaxValueDepth64Table nesting depth Hash.Value recurses into before failing.