1
0
mirror of https://github.com/kepler155c/opus synced 2024-06-18 11:20:01 +00:00
opus/sys/modules/opus/security.lua

57 lines
1.2 KiB
Lua
Raw Normal View History

local Config = require('opus.config')
local ECC = require('opus.crypto.ecc')
2019-06-29 20:35:33 +00:00
local Util = require('opus.util')
local Security = { }
function Security.verifyPassword(password)
local current = Security.getPassword()
return current and password == current
end
2017-11-15 05:08:42 +00:00
function Security.hasPassword()
return not not Security.getPassword()
2017-11-15 05:08:42 +00:00
end
2019-06-29 06:44:30 +00:00
local function genKey()
local key = { }
for _ = 1, 32 do
table.insert(key, ("%02x"):format(math.random(0, 0xFF)))
end
return table.concat(key)
end
function Security.getSecretKey()
local config = Config.load('os')
2018-01-24 22:39:38 +00:00
if not config.secretKey then
2019-06-29 06:44:30 +00:00
config.secretKey = genKey()
2018-01-24 22:39:38 +00:00
Config.update('os', config)
end
return Util.hexToByteArray(config.secretKey)
end
2019-06-29 20:35:33 +00:00
function Security.getIdentifier()
local config = Config.load('os')
if config.identifier then
return config.identifier
end
-- preserve the hash the user generated
local identifier = ECC.publicKey(Security.getSecretKey())
config.identifier = Util.byteArrayToHex(identifier)
Config.update('os', config)
return config.identifier
end
function Security.updatePassword(password)
local config = Config.load('os')
2018-01-24 22:39:38 +00:00
config.password = password
Config.update('os', config)
end
function Security.getPassword()
return Config.load('os').password
end
return Security