Files

30 lines
1.4 KiB
Luau

-- core/Md5 tests: the RFC 1321 test suite, incremental updates at every split, sub-ranges, a long input.
local T = require("@tests/T")
local Md5 = require("@rosetta/core/Md5")
local t = T.suite("md5")
local VECTORS = {
{ "", "d41d8cd98f00b204e9800998ecf8427e" },
{ "a", "0cc175b9c0f1b6a831c399e269772661" },
{ "abc", "900150983cd24fb0d6963f7d28e17f72" },
{ "message digest", "f96b697d7cb7938d525a2f31aaf161d0" },
{ "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b" },
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f" },
{ string.rep("1234567890", 8), "57edf4a22be3c955ac49da2e2107b67a" },
}
for _, v in VECTORS do
t:eq(Md5.of(buffer.fromstring(v[1])), v[2], "md5 of " .. string.format("%q", v[1]))
end
local msg = string.rep("The quick brown fox jumps over the lazy dog. ", 3)
local want = Md5.of(buffer.fromstring(msg))
for cut = 0, #msg, 7 do
local h = Md5.new()
h:update(buffer.fromstring(string.sub(msg, 1, cut)))
h:update(buffer.fromstring(string.sub(msg, cut + 1, cut + 20)))
h:update(buffer.fromstring(string.sub(msg, cut + 21)))
t:eq(Md5.hex(h:digest()), want, "split at " .. cut)
end
t:eq(Md5.of(buffer.fromstring("xxabcyy"), 2, 3), "900150983cd24fb0d6963f7d28e17f72", "sub-range")
t:eq(Md5.of(buffer.fromstring(string.rep("a", 1000000))), "7707d6ae4e027c70eea2a935c2296f21", "a million a's")
t:done()