--[[ test_sha256.lua — Verify sha256 against openssl. Run: lua5.1 tests/test_sha256.lua ]] package.path = package.path .. ";./src/?.lua" local sha = require("balancerlite.sha256") local function assert_eq(a, b, msg) if a ~= b then error(((msg or "") .. "\n expected: " .. tostring(b) .. "\n got: " .. tostring(a)), 2) end end local function check(msg, expected_hex) local h = sha.hex(msg) local ok = (h == expected_hex) print((ok and "PASS" or "FAIL") .. " sha256(" .. (msg == "" and '""' or string.format("%q", msg)) .. ") = " .. (h or "nil")) if not ok then print(" EXPECTED: " .. expected_hex) end return ok end local function check_hmac(key, msg, expected_hex) local h = sha.hmac_hex(key, msg) local ok = (h == expected_hex) print((ok and "PASS" or "FAIL") .. " HMAC(" .. string.format("%q", key) .. ", " .. string.format("%q", msg) .. ") = " .. (h or "nil")) if not ok then print(" EXPECTED: " .. expected_hex) end return ok end print("=== SHA-256 / HMAC test vectors ===") local all_ok = true -- SHA-256 (openssl verified) all_ok = check("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") and all_ok all_ok = check("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") and all_ok -- HMAC-SHA256 (openssl verified) all_ok = check_hmac("key", "The quick brown fox jumps over the lazy dog", "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8") and all_ok print(all_ok and "PASS: sha256" or "FAIL: sha256")