futr/lua-scripts/utils.lua
2025-11-16 20:25:52 +03:00

51 lines
1.6 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--utils.lua- является пользовательской библиотекой вспомогательных функций Lua для работы с СУБД futriix.
-- Выполняет роль:
-- Загрузчика в других Lua скриптах проекта
-- Загрузчика при инициализации Lua интерпретатора
-- Основного инструмента в хранимых процедурах проекта
function json_encode(tbl)
return require("serde").encode(tbl)
end
function json_decode(str)
return require("serde").decode(str)
end
function table_length(tbl)
local count = 0
for _ in pairs(tbl) do
count = count + 1
end
return count
end
-- Функция для MD5 хеширования
function md5_hash(text)
return md5(text)
end
-- Функция для создания пользователя с хешированным паролем
function create_user_with_password(space, username, password, user_data)
user_data = user_data or {}
user_data.password = md5_hash(password)
return insert(space, username, json_encode(user_data))
end
-- Функция для проверки пароля пользователя
function verify_user_password(space, username, password)
local user_json = get(space, username)
if user_json == "null" then
return false, "User not found"
end
local user_data = json_decode(user_json)
if user_data.password == md5_hash(password) then
return true, "Password correct"
else
return false, "Password incorrect"
end
end