2017-09-05 06:09:31 +00:00
|
|
|
local Config = require('config')
|
|
|
|
|
|
|
|
local config = { }
|
|
|
|
|
|
|
|
local Security = { }
|
|
|
|
|
|
|
|
function Security.verifyPassword(password)
|
2018-01-24 22:39:38 +00:00
|
|
|
Config.load('os', config)
|
|
|
|
return config.password and password == config.password
|
2017-09-05 06:09:31 +00:00
|
|
|
end
|
|
|
|
|
2017-11-15 05:08:42 +00:00
|
|
|
function Security.hasPassword()
|
2018-01-24 22:39:38 +00:00
|
|
|
return not not config.password
|
2017-11-15 05:08:42 +00:00
|
|
|
end
|
|
|
|
|
2017-09-05 06:09:31 +00:00
|
|
|
function Security.getSecretKey()
|
2018-01-24 22:39:38 +00:00
|
|
|
Config.load('os', config)
|
|
|
|
if not config.secretKey then
|
|
|
|
config.secretKey = math.random(100000, 999999)
|
|
|
|
Config.update('os', config)
|
|
|
|
end
|
|
|
|
return config.secretKey
|
2017-09-05 06:09:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Security.getPublicKey()
|
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local exchange = {
|
|
|
|
base = 11,
|
|
|
|
primeMod = 625210769
|
|
|
|
}
|
2017-09-05 06:09:31 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local function modexp(base, exponent, modulo)
|
|
|
|
local remainder = base
|
2017-09-05 06:09:31 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
for _ = 1, exponent-1 do
|
|
|
|
remainder = remainder * remainder
|
|
|
|
if remainder >= modulo then
|
|
|
|
remainder = remainder % modulo
|
|
|
|
end
|
|
|
|
end
|
2017-09-05 06:09:31 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
return remainder
|
|
|
|
end
|
2017-09-05 06:09:31 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local secretKey = Security.getSecretKey()
|
|
|
|
return modexp(exchange.base, secretKey, exchange.primeMod)
|
2017-09-05 06:09:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Security.updatePassword(password)
|
2018-01-24 22:39:38 +00:00
|
|
|
Config.load('os', config)
|
|
|
|
config.password = password
|
|
|
|
Config.update('os', config)
|
2017-09-05 06:09:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Security.getPassword()
|
2018-01-24 22:39:38 +00:00
|
|
|
Config.load('os', config)
|
|
|
|
return config.password
|
2017-09-05 06:09:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return Security
|