[Solved] Simple non-cryptographic numerical hash in Lua

User avatar
texmex
Member
 
Posts: 1588
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

[Solved] Simple non-cryptographic numerical hash in Lua

by texmex » Sun Oct 06, 2019 20:43

I need a way to convert a string to a numerical hash. I don't care how simple. I know there's xxhash for Lua but since Lua libraries can't be used in Minetest I need something that can be embedded. Ideas?
Last edited by texmex on Sun Oct 06, 2019 22:23, edited 1 time in total.
 

User avatar
rubenwardy
Moderator
 
Posts: 5862
Joined: Tue Jun 12, 2012 18:11
Location: United Kingdom
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
 

User avatar
texmex
Member
 
Posts: 1588
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

Re: Simple non-cryptographic numerical hash in Lua

by texmex » Sun Oct 06, 2019 22:22

Yay, didn’t know that. Is it demanding? I won’t be using it in runtime so probably it’s not an issue.

It’s base 16 encoded, yes? From what I understand that can be converted to numbers with tonumber(sha1hash,16)?
 

User avatar
Krock
Developer
 
Posts: 4393
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: [Solved] Simple non-cryptographic numerical hash in Lua

by Krock » Mon Oct 07, 2019 16:33

Usually hash functions return a human-friendly format as hexadecimals printed to a string. Yes, it's base 16 but it won't fit into the numerical type of Lua (double), so only the first few bytes will be of actual use. With the Minetest sha1 function you can also get the raw bytes, if that's more like what you need.
I use that regularly to generate colors which are quite unique, but persistent over server restarts. Quite helpful.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 

User avatar
texmex
Member
 
Posts: 1588
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

Re: [Solved] Simple non-cryptographic numerical hash in Lua

by texmex » Mon Oct 07, 2019 17:44

Even better! It’s not important for me to get the whole sequence, I’ll just use it for predictable uniqueness. Ca you tell me more about your use case, Krock? I’m curious.
 

User avatar
Krock
Developer
 
Posts: 4393
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: [Solved] Simple non-cryptographic numerical hash in Lua

by Krock » Mon Oct 07, 2019 18:30

texmex wrote:I’m curious.


Base image + colourized overlay texture for unique looking items: (unmodified pixels are transparent in the overlay)
Image

Or giving usernames a more or less unique color:
Image
Attachments
usecase.png
(16.18 KiB) Not downloaded yet
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 

User avatar
texmex
Member
 
Posts: 1588
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

Re: [Solved] Simple non-cryptographic numerical hash in Lua

by texmex » Mon Oct 07, 2019 20:59

That looks sweet. I'm looking to do this too eventually. Any chance to see this colorization code?
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 1 guest