Fastest SHA256 Module

Fastest SHA256 Hash

This is over 4x times faster than Hashlib and the input has to be a buffer!

local Message: string = "Hello World"
local MessageBuffer: buffer = buffer.fromstring("Hello World")

local OptionalSalt: string = "Salt"
local OptionalSaltBuffer: buffer = buffer.fromstring("Salt")

assert(SHA256(MessageBuffer) == SHA256(buffer.fromstring(Message)))
assert(SHA256(MessageBuffer, OptionalSaltBuffer) == SHA256(buffer.fromstring(Message .. OptionalSalt)))
assert(SHA256(MessageBuffer, OptionalSaltBuffer) == SHA256(buffer.fromstring(Message), buffer.fromstring(OptionalSalt)))
assert(SHA256(buffer.fromstring("The quick brown fox jumps over the lazy dog.")) == "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")

Benchmark Results
Iterations: 500
Message Length: 29204
HashLib took: 1,492,559 microseconds

The old version took: 332,778 microseconds - 348.5% faster than HashLib
Dekkonot’s SHA256 took: 346,156 microseconds

This version took: 276,341 microseconds - 23.2% faster than Dekkonot’s SHA256 - 428.8% faster than HashLib


Download:
SHA256.rbxm (5.3 KB)

22 Likes

hi! what does --!optimize 2 do on the start of the script. I know --!native and --!strict does but never heard of optimize; I read about it and it seems like it only works in studio for some reason.

1 Like

Think it changes for loops for just if statements when its ran

1 Like

–!optimize 2 is the default option in-game, while 1 is default in studio. Setting it to 2 will just make sure you get the same results when benchmarking in studio as you would in game, but it takes longer to compile.