SHA-256 Module / Tests

A while ago I started a project to build a streamlined GameAnalytics module but due to outside work and the release of the official module I ended up putting the project on the back burner.

I did however finish optimizing the SHA module and now with the release of bit32 I decided to release the module along with the comparison between the old module and using the bit32 functions.

I am sure someone might find it usefull or even make additional improvements. :slight_smile:

Benchmark results (disable Lua debugger in studo setting)

SHA test place_2.rbxl (25.8 KB)

12 Likes

Its really cool, but what could this be useful for in a Roblox game? Any use cases?

You shouldn’t use passwords for remotes.

Many third-party services, namely GameAnalytics, require that you digest and/or encrypt data you send to them.

1 Like

The difference in performance, or lack thereof, between a custom Lua implementation of bit32 and the new one built into Lua is really surprising to me.

Or am I missing something… is the built-in bit32 just for convenience, and not on the C side?

2 Likes

mostly api calls but im sure there are other things this can be used for in a game.

The bit module uses caching of values but that was my same reaction. It might be usefull to create a cache for the bit operations with a weak table. I would need to do some additional testing to find this out.

Im really not sure on how the new bit32 lib is implemented o.o

Not sure what’s happening here, but my (bad) implementation of MD5 went from taking 1700ms for 10 digests to 4ms with the bit32 lib.

Most of the hashing functions I have seen in Roblox originate from here " These are written to be easy to read and easy to use, not for performance!".

I would take a look at what is being ran and remove any redundancy in the code. I did modify how the bit module works so it should be somewhat faster.

I’m not sure why your implementation is so slow, though looking at the code I suspect it’s probably because it’s quite over-engineered. Given you’re porting it from a Lockbox, that’s not surprising though (as you yourself said, it’s designed to be easy to read and not be fast).

To give you an idea of how slow it is comparatively, I ran a few iterations of my implementation along side yours using the same tests:
image
Dekko module is mine, if that wasn’t clear

1 Like

What I meant was that yours should be seeing a much more noticeable improvement by using the native bit32 lib. As @Dekkonot stated, it seems over-engineered and that’s likely what’s taking away from what you gain by using bit32.

1 Like

I found out that LuaUsers implemented a SHA256 algorithm using the bit32 library in pure lua (adapted from the Wikipedia page for SHA2). This ends up being much faster than most of those tested and can literally just be copy pasted into Roblox.
http://lua-users.org/wiki/SecureHashAlgorithm

image

4 Likes

@Hexcede @Dekkonot @Muoshuu Yep it really is. It uses tables to store the data before each round so it can support both strings and byte inputs. It is also not very smart when dealing with chunks as it would still place them in the table just to move them over to the digest.

I am glad to see that there are better solutions available in Lua. Please keep posting them :smiley:

1 Like