Merge pull request #2018 from EngineerSmith/hash

Added container type argument for love.data.hash
This commit is contained in:
Sasha Szpakowski
2024-02-13 21:20:07 -04:00
committed by GitHub
3 changed files with 60 additions and 17 deletions
+2
View File
@@ -101,6 +101,7 @@ Released: N/A
* Changed RevoluteJoint:getMotorTorque and WheelJoint:getMotorTorque to take 'dt' as a parameter instead of 'inverse_dt'.
* Changed love.math.perlinNoise and simplexNoise to use higher precision numbers for its internal calculations.
* Changed t.accelerometerjoystick startup flag in love.conf to unset by default.
* Changed love.data.hash to take in a container type.
* Renamed 'display' field to 'displayindex' in love.window.setMode/updateMode/getMode and love.conf.
* Renamed love.graphics Text objects to TextBatch.
@@ -120,6 +121,7 @@ Released: N/A
* Deprecated t.accelerometerjoystick in love.conf (replaced by love.sensor module).
* Deprecated the variants of Mesh:attachAttribute and SpriteBatch:attachAttribute which accept a Mesh (replaced by variants which accept a Buffer).
* Deprecated Texture:newImageData (replaced by love.graphics.readbackTexture).
* Deprecated love.data.hash (replaced by function variant, which takes container type).
* Removed the variant of SpriteBatch:setColor() which turns off all previously set colors.
* Removed the no-argument variant of love.graphics.setColorMask.
+32 -5
View File
@@ -302,25 +302,52 @@ int w_decode(lua_State *L)
int w_hash(lua_State *L)
{
const char *fstr = luaL_checkstring(L, 1);
int narg = 0; // used to change the arg position when using the deprecated function variant
ContainerType ctype = CONTAINER_STRING;
HashFunction::Function function;
const char *str = luaL_checkstring(L, 1);
if (!getConstant(str, ctype))
{
if (HashFunction::getConstant(str, function))
{ // check if the argument at 1 is a hash function; deprecated function variant
luax_markdeprecated(L, 1, "love.data.hash", API_FUNCTION_VARIANT, DEPRECATED_REPLACED, "variant with container return type parameter");
narg = -1;
}
else
luax_enumerror(L, "container type", getConstants(ctype), str);
}
const char *fstr = luaL_checkstring(L, 2 + narg);
if (!HashFunction::getConstant(fstr, function))
return luax_enumerror(L, "hash function", HashFunction::getConstants(function), fstr);
HashFunction::Value hashvalue;
if (lua_isstring(L, 2))
if (lua_isstring(L, 3 + narg))
{
size_t rawsize = 0;
const char *rawbytes = luaL_checklstring(L, 2, &rawsize);
const char *rawbytes = luaL_checklstring(L, 3 + narg, &rawsize);
luax_catchexcept(L, [&](){ love::data::hash(function, rawbytes, rawsize, hashvalue); });
}
else
{
Data *rawdata = luax_checktype<Data>(L, 2);
Data *rawdata = luax_checktype<Data>(L, 3 + narg);
luax_catchexcept(L, [&](){ love::data::hash(function, rawdata, hashvalue); });
}
lua_pushlstring(L, hashvalue.data, hashvalue.size);
if (ctype == CONTAINER_DATA)
{
Data* d = nullptr;
luax_catchexcept(L, [&]() { d = instance()->newByteData(hashvalue.size); });
memcpy(d->getData(), hashvalue.data, hashvalue.size);
luax_pushtype(L, Data::type, d);
d->release();
}
else
lua_pushlstring(L, hashvalue.data, hashvalue.size);
return 1;
}
+26 -12
View File
@@ -196,19 +196,33 @@ end
-- love.data.hash
love.test.data.hash = function(test)
-- setup all the different hashing types
local str1 = love.data.hash('md5', 'helloworld')
local str2 = love.data.hash('sha1', 'helloworld')
local str3 = love.data.hash('sha224', 'helloworld')
local str4 = love.data.hash('sha256', 'helloworld')
local str5 = love.data.hash('sha384', 'helloworld')
local str6 = love.data.hash('sha512', 'helloworld')
local str1 = love.data.hash('string', 'md5', 'helloworld')
local str2 = love.data.hash('string', 'sha1', 'helloworld')
local str3 = love.data.hash('string', 'sha224', 'helloworld')
local str4 = love.data.hash('string', 'sha256', 'helloworld')
local str5 = love.data.hash('string', 'sha384', 'helloworld')
local str6 = love.data.hash('string', 'sha512', 'helloworld')
local data1 = love.data.hash('data', 'md5', 'helloworld')
local data2 = love.data.hash('data', 'sha1', 'helloworld')
local data3 = love.data.hash('data', 'sha224', 'helloworld')
local data4 = love.data.hash('data', 'sha256', 'helloworld')
local data5 = love.data.hash('data', 'sha384', 'helloworld')
local data6 = love.data.hash('data', 'sha512', 'helloworld')
-- check encoded hash value matches what's expected for that algo
test:assertEquals('fc5e038d38a57032085441e7fe7010b0', love.data.encode("string", "hex", str1), 'check md5 encode')
test:assertEquals('6adfb183a4a2c94a2f92dab5ade762a47889a5a1', love.data.encode("string", "hex", str2), 'check sha1 encode')
test:assertEquals('b033d770602994efa135c5248af300d81567ad5b59cec4bccbf15bcc', love.data.encode("string", "hex", str3), 'check sha224 encode')
test:assertEquals('936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af', love.data.encode("string", "hex", str4), 'check sha256 encode')
test:assertEquals('97982a5b1414b9078103a1c008c4e3526c27b41cdbcf80790560a40f2a9bf2ed4427ab1428789915ed4b3dc07c454bd9', love.data.encode("string", "hex", str5), 'check sha384 encode')
test:assertEquals('1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60', love.data.encode("string", "hex", str6), 'check sha512 encode')
-- test container string
test:assertEquals('fc5e038d38a57032085441e7fe7010b0', love.data.encode("string", "hex", str1), 'check string md5 encode')
test:assertEquals('6adfb183a4a2c94a2f92dab5ade762a47889a5a1', love.data.encode("string", "hex", str2), 'check string sha1 encode')
test:assertEquals('b033d770602994efa135c5248af300d81567ad5b59cec4bccbf15bcc', love.data.encode("string", "hex", str3), 'check string sha224 encode')
test:assertEquals('936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af', love.data.encode("string", "hex", str4), 'check string sha256 encode')
test:assertEquals('97982a5b1414b9078103a1c008c4e3526c27b41cdbcf80790560a40f2a9bf2ed4427ab1428789915ed4b3dc07c454bd9', love.data.encode("string", "hex", str5), 'check string sha384 encode')
test:assertEquals('1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60', love.data.encode("string", "hex", str6), 'check string sha512 encode')
-- test container data
test:assertEquals('fc5e038d38a57032085441e7fe7010b0', love.data.encode("string", "hex", data1), 'check data md5 encode')
test:assertEquals('6adfb183a4a2c94a2f92dab5ade762a47889a5a1', love.data.encode("string", "hex", data2), 'check data sha1 encode')
test:assertEquals('b033d770602994efa135c5248af300d81567ad5b59cec4bccbf15bcc', love.data.encode("string", "hex", data3), 'check data sha224 encode')
test:assertEquals('936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af', love.data.encode("string", "hex", data4), 'check data sha256 encode')
test:assertEquals('97982a5b1414b9078103a1c008c4e3526c27b41cdbcf80790560a40f2a9bf2ed4427ab1428789915ed4b3dc07c454bd9', love.data.encode("string", "hex", data5), 'check data sha384 encode')
test:assertEquals('1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60', love.data.encode("string", "hex", data6), 'check data sha512 encode')
end