Add new E2 hash functions (#3199)

* Add hash functions

* Add the rest of them
Why not?
Increased size limit

* Change to hard throw

* Remove return (I forgot to commit this)
This commit is contained in:
Denneisk 2024-12-04 19:25:55 +00:00 committed by GitHub
parent 6a1216128b
commit 79f14291fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 3 deletions

View File

@ -26,3 +26,8 @@ assert(Threw)
assert(format("%s %d", "foo", 232) == "foo 232")
assert(format("%u", 232) == "232")
assert(hashCRC(Str) == "3957769958")
assert(hashMD5(Str) == "6cd3556deb0da54bca060b4c39479839")
assert(hashSHA1(Str) == "943a702d06f34599aee1f8da8ef9f7296031d699")
assert(hashSHA256(Str) == "315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3")

View File

@ -362,7 +362,7 @@ e2function number hashNoComments()
return getHash( self, self.entity.buffer )
end
[nodiscard]
[nodiscard, deprecated]
e2function number hash( string str )
return getHash( self, str )
end

View File

@ -520,3 +520,35 @@ e2function string decompress(string compressed)
self.prf = self.prf + len * 0.5
return decompress(compressed) or self:throw("Invalid input for decompression!", "")
end
--[[******************************************************************************]]--
-- Hash functions
local function hash_generic(self, text, func)
local len = #text
if len > 131072 then self:forceThrow("Input string is too long!") end
self.prf = self.prf + len * 0.01
return func(text)
end
__e2setcost(5)
[nodiscard]
e2function string hashCRC(string text)
return hash_generic(self, text, util.CRC)
end
[nodiscard]
e2function string hashMD5(string text)
return hash_generic(self, text, util.MD5)
end
[nodiscard]
e2function string hashSHA1(string text)
return hash_generic(self, text, util.SHA1)
end
[nodiscard]
e2function string hashSHA256(string text)
return hash_generic(self, text, util.SHA256)
end

View File

@ -176,6 +176,10 @@ E2Helper.Descriptions["gmatch(s:s)"] = "runs string.gmatch(S, S2) and returns th
E2Helper.Descriptions["gmatch(s:sn)"] = "runs string.gmatch(S, S2, N) and returns the captures in arrays in a table"
E2Helper.Descriptions["compress(s)"] = "Compresses the input string using LZMA compression. See decompress(string)"
E2Helper.Descriptions["decompress(s)"] = "Decompresses an LZMA-compressed string. See compress(string)"
E2Helper.Descriptions["hashCRC(s)"] = "Returns a the CRC checksum of the input string. This is not a secure hash function"
E2Helper.Descriptions["hashMD5(s)"] = "Returns the MD5 hash of the input string. This is not a secure hash function; see hashSHA256"
E2Helper.Descriptions["hashSHA1(s)"] = "Returns the SHA1 hash of the input string. This is not a secure hash function; see hashSHA256"
E2Helper.Descriptions["hashSHA256(s)"] = "Returns the SHA256 hash of the input string"
-- Entity/Player
E2Helper.Descriptions["entity(n)"] = "Gets the entity associated with the id"
@ -937,7 +941,7 @@ E2Helper.Descriptions["remoteSetCode(e:st)"] = "Sets the E2's code with main fil
E2Helper.Descriptions["remoteUpload(e:s)"] = "Uploads the code from your computer to the server"
E2Helper.Descriptions["getCodeIncludes()"] = "Returns a table where indices (keys) are names of included files and entries are their codes"
E2Helper.Descriptions["hash()"] = "Returns a numerical hash using the code of the E2 itself (Including comments)"
E2Helper.Descriptions["hash(s)"] = "Returns a numerical hash using the string specified"
E2Helper.Descriptions["hash(s)"] = "Returns the CRC-32 of the string specified. This should not be used as a legitimate hash function"
E2Helper.Descriptions["hashNoComments()"] = "Returns a numerical hash using the code of the E2 itself (Excluding comments)"
E2Helper.Descriptions["concmd(s)"] = "Takes a string and executes it in console. Returns 1 if it succeeded and 0 if it failed.The client must enable this in the console with \"wire_expression2_concmd 1\". \"wire_expression2_concmd_whitelist\" allows you to choose which commands can be used.[http://www.wiremod.com/forum/151800-post12.html]"
E2Helper.Descriptions["ioInputEntity(s)"] = "Returns the entity the input S is wired to"