1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-17 06:04:50 +00:00
opus/sys/apps/network/keygen.lua

40 lines
941 B
Lua
Raw Normal View History

2019-06-29 20:35:33 +00:00
local ECC = require('opus.crypto.ecc')
local Event = require('opus.event')
local Util = require('opus.util')
local network = _G.network
local os = _G.os
local keyPairs = { }
local function generateKeyPair()
local key = { }
for _ = 1, 32 do
2020-03-30 06:07:20 +00:00
table.insert(key, math.random(0, 0xFF))
2019-06-29 20:35:33 +00:00
end
2020-03-30 06:07:20 +00:00
local privateKey = setmetatable(key, Util.byteArrayMT)
2019-06-29 20:35:33 +00:00
return privateKey, ECC.publicKey(privateKey)
end
getmetatable(network).__index.getKeyPair = function()
2020-03-30 06:07:20 +00:00
local keys = table.remove(keyPairs)
os.queueEvent('generate_keypair')
if not keys then
return generateKeyPair()
end
return table.unpack(keys)
2019-06-29 20:35:33 +00:00
end
-- Generate key pairs in the background as this is a time-consuming process
Event.on('generate_keypair', function()
2020-03-30 06:07:20 +00:00
while true do
os.sleep(5)
local timer = Util.timer()
table.insert(keyPairs, { generateKeyPair() })
_G._syslog('Generated keypair in ' .. timer())
if #keyPairs >= 3 then
break
end
end
2019-06-29 20:35:33 +00:00
end)