Hash functions are awesome! You can read more about them at wikipedia (https://en.wikipedia.org/wiki/Hash_function) but I’ll give a brief and simplified explanation below.
(TL;DR, don’t rely on reverse hashing for your game logic, it’s not available in release mode)
A hash function will take a value (input) of variable length (like a string) and convert it to a value (output) of fixed length. A hash algorithm has certain properties, one of which is uniformity, another one which is Non-invertible.
Uniformity means that the output from a hashing function should be evenly spread over the output space, in a uniform distribution. This could be seen as a more relaxed requirement compared to collision resistance which is an important aspect of cryptographic hash functions, but for Defold is enough to know that the hash value (output) for "/main"
is very unlikely to be the same as the hash value (output) for "/level"
.
Non-invertible means that given h(x) = y
, there should be no known function f(y) = x
. And if such a function exists, it would be very expensive to compute. This is an important aspect when it comes to storing password, but it also allows for hashes to be computed relatively fast, which is important for Defold.
The big benefit of passing hashes between functions in Defold is that they are a lot faster to compare, and will require less memory than the input that created the hash. The drawback is that you can’t retrieve the original input from the hash. The output from Defold’s hash function is a 64 bit integer, which can be compared using a single instruction, whereas comparing the input would require at least N
instructions, and in reality a lot more. Passing the hash value between different functions will require 8 bytes of memory, whereas passing the input would require N
bytes of memory.
When the engine is run in debug/development mode, we will store the input for each hash in a lookup-table so we can reverse the hash, which is required because of the Non-invertible property. This requires extra work, and is something that is optimized away in release mode. Thus you should never rely on reverse hashing for your game logic, since it will be unavailable when you release your game.