Skip to content

Commit

Permalink
Add new E2 hash functions (#3199)
Browse files Browse the repository at this point in the history
* Add hash functions

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

* Change to hard throw

* Remove return (I forgot to commit this)
  • Loading branch information
Denneisk authored Dec 4, 2024
1 parent 6a12161 commit 79f1429
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
7 changes: 6 additions & 1 deletion data/expression2/tests/runtime/libraries/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ try {
assert(Threw)

assert(format("%s %d", "foo", 232) == "foo 232")
assert(format("%u", 232) == "232")
assert(format("%u", 232) == "232")

assert(hashCRC(Str) == "3957769958")
assert(hashMD5(Str) == "6cd3556deb0da54bca060b4c39479839")
assert(hashSHA1(Str) == "943a702d06f34599aee1f8da8ef9f7296031d699")
assert(hashSHA256(Str) == "315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3")
2 changes: 1 addition & 1 deletion lua/entities/gmod_wire_expression2/core/selfaware.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions lua/entities/gmod_wire_expression2/core/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,36 @@ e2function string decompress(string compressed)
if len > 32768 then return self:throw("Input string is too long!", "") end
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
6 changes: 5 additions & 1 deletion lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 79f1429

Please sign in to comment.