mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-12 00:35:56 -04:00
Formatting of lua programs (two space indentation), also feature based check in gpg.lua which is more in line with other components.
This commit is contained in:
parent
f27e0c1d5a
commit
fd96b309a5
@ -12,222 +12,225 @@ local serialization = require("serialization")
|
|||||||
local args, options = shell.parse(...)
|
local args, options = shell.parse(...)
|
||||||
|
|
||||||
local function writeFile(path, data)
|
local function writeFile(path, data)
|
||||||
if filesystem.exists(path) then
|
if filesystem.exists(path) then
|
||||||
print("gpg: failed to write file: " .. path)
|
io.stderr:write("gpg: failed to write file: " .. path .. "\n")
|
||||||
print("gpg: error was: file already exists")
|
io.stderr:write("gpg: error was: file already exists\n")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if type(data) == "table" then
|
if type(data) == "table" then
|
||||||
data = serialization.serialize(data)
|
data = serialization.serialize(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
local h, err = io.open(path, "wb")
|
local h, err = io.open(path, "wb")
|
||||||
|
|
||||||
if not h then
|
if not h then
|
||||||
print("gpg: failed to write file: " .. path)
|
io.stderr:write("gpg: failed to write file: " .. path .. "\n")
|
||||||
print("gpg: error was: " .. err)
|
io.stderr:write("gpg: error was: " .. err .. "\n")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
h:write(data)
|
h:write(data)
|
||||||
h:close()
|
h:close()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function readFile(path, deserialize)
|
local function readFile(path, deserialize)
|
||||||
local h = io.open(path, "rb")
|
local h = io.open(path, "rb")
|
||||||
local r = h:read("*a")
|
local r = h:read("*a")
|
||||||
h:close()
|
h:close()
|
||||||
|
|
||||||
if deserialize then
|
if deserialize then
|
||||||
r = serialization.unserialize(r)
|
r = serialization.unserialize(r)
|
||||||
end
|
end
|
||||||
|
|
||||||
return r
|
return r
|
||||||
end
|
end
|
||||||
|
|
||||||
local function parseKey(path, isPublic)
|
local function parseKey(path, isPublic)
|
||||||
local d = readFile(path, true)
|
local d = readFile(path, true)
|
||||||
local k, err = data.deserializeKey(d.d, d.t)
|
local k, err = data.deserializeKey(d.d, d.t)
|
||||||
|
|
||||||
if not k then
|
if not k then
|
||||||
print("gpg: failed to parse key: " .. err)
|
io.stderr:write("gpg: failed to parse key: " .. err .. "\n")
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
if k.isPublic() ~= isPublic then
|
if k.isPublic() ~= isPublic then
|
||||||
print("gpg: wrong key type")
|
io.stderr:write("gpg: wrong key type\n")
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
return k
|
return k
|
||||||
end
|
end
|
||||||
|
|
||||||
local function deriveName(base, encrypt)
|
local function deriveName(base, encrypt)
|
||||||
if encrypt then
|
if encrypt then
|
||||||
return base .. ".gpg"
|
return base .. ".gpg"
|
||||||
else
|
else
|
||||||
local d = base:gsub(".gpg", "")
|
local d = base:gsub(".gpg", "")
|
||||||
if d == base then
|
if d == base then
|
||||||
d = d .. ".dec"
|
d = d .. ".dec"
|
||||||
print("gpg: decrypting to " .. d)
|
io.write("gpg: decrypting to " .. d .. "\n")
|
||||||
end
|
|
||||||
return d
|
|
||||||
end
|
end
|
||||||
|
return d
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function ensureTier(tier)
|
local function ensureMethods(...)
|
||||||
if not require("component").isAvailable("data") then
|
if not require("component").isAvailable("data") then
|
||||||
print("gpg: you must have data card in order to run this program")
|
io.stderr:write("gpg: you must have data card in order to run this program\n")
|
||||||
error("data card is absent")
|
error("data card is absent")
|
||||||
end
|
end
|
||||||
|
|
||||||
if data.tier() < tier then
|
local names = table.pack(...)
|
||||||
print("gpg: you must have tier " .. tier .. " data card in order to run this program")
|
for i = 1, names.n do
|
||||||
error("data card is too simple")
|
if names[i] and not data[names[i]] then
|
||||||
|
io.stderr:write("gpg: method " .. names[i] .. " required on data card to run this program\n")
|
||||||
|
error("data card tier insufficient")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if options['g'] and (#args == 2) then
|
if options['g'] and (#args == 2) then
|
||||||
ensureTier(3)
|
ensureMethods("generateKeyPair")
|
||||||
local pub, priv = data.generateKeyPair(384)
|
local pub, priv = data.generateKeyPair(384)
|
||||||
|
|
||||||
priv = { t = priv.keyType(), d = priv.serialize() }
|
priv = { t = priv.keyType(), d = priv.serialize() }
|
||||||
pub = { t = pub.keyType(), d = pub.serialize() }
|
pub = { t = pub.keyType(), d = pub.serialize() }
|
||||||
|
|
||||||
if not writeFile(args[1], priv) then
|
if not writeFile(args[1], priv) then
|
||||||
print("gpg: failed to write private key, aborting")
|
io.stderr:write("gpg: failed to write private key, aborting\n")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if not writeFile(args[2], pub) then
|
if not writeFile(args[2], pub) then
|
||||||
print("gpg: failed to write public key, aborting")
|
io.stderr:write("gpg: failed to write public key, aborting\n")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
if options['c'] and (options['e'] or options['d']) and (#args == 1) then
|
if options['c'] and (options['e'] or options['d']) and (#args == 1) then
|
||||||
ensureTier(2)
|
ensureMethods("md5", "sha256", "encrypt", "decrypt", "random")
|
||||||
if options['d'] and options['e'] then
|
if options['d'] and options['e'] then
|
||||||
print("gpg: please specify either -d or -e")
|
io.stderr:write("gpg: please specify either -d or -e\n")
|
||||||
return false
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
io.write("gpg: enter password: ")
|
||||||
|
local aesKey = data.md5(term.read(nil, nil, nil, "*"))
|
||||||
|
local checkValue = data.sha256(aesKey)
|
||||||
|
|
||||||
|
if options['e'] then
|
||||||
|
local iv = data.random(16)
|
||||||
|
local d = data.encrypt(readFile(args[1]), aesKey, iv)
|
||||||
|
|
||||||
|
return writeFile(deriveName(args[1], true), {
|
||||||
|
t = "pwd",
|
||||||
|
kdf = "md5",
|
||||||
|
iv = iv,
|
||||||
|
cv = checkValue,
|
||||||
|
d = d
|
||||||
|
})
|
||||||
|
else
|
||||||
|
local d = readFile(args[1], true)
|
||||||
|
|
||||||
|
if d.t ~= "pwd" then
|
||||||
|
io.stderr:write("gpg: file is not encrypted with a password\n")
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
io.write("gpg: enter password: ")
|
if checkValue ~= d.cv then
|
||||||
local aesKey = data.md5(term.read(nil, nil, nil, "*"))
|
io.stderr:write("gpg: password incorrect\n")
|
||||||
local checkValue = data.sha256(aesKey)
|
return false
|
||||||
|
|
||||||
if options['e'] then
|
|
||||||
local iv = data.random(16)
|
|
||||||
local d = data.encrypt(readFile(args[1]), aesKey, iv)
|
|
||||||
|
|
||||||
return writeFile(deriveName(args[1], true), {
|
|
||||||
t = "pwd",
|
|
||||||
kdf = "md5",
|
|
||||||
iv = iv,
|
|
||||||
cv = checkValue,
|
|
||||||
d = d
|
|
||||||
})
|
|
||||||
else
|
|
||||||
local d = readFile(args[1], true)
|
|
||||||
|
|
||||||
if d.t ~= "pwd" then
|
|
||||||
print("gpg: file is not encrypted with a password")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
if checkValue ~= d.cv then
|
|
||||||
print("gpg: password incorrect")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return writeFile(deriveName(args[1], false), data.decrypt(d.d, aesKey, d.iv))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return writeFile(deriveName(args[1], false), data.decrypt(d.d, aesKey, d.iv))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if (options['d'] or options['e']) and (#args == 2) then
|
if (options['d'] or options['e']) and (#args == 2) then
|
||||||
ensureTier(3)
|
ensureMethods("md5", "sha256", "encrypt", "decrypt", "random", "generateKeyPair", "deserializeKey", "ecdh")
|
||||||
if options['d'] and options['e'] then
|
if options['d'] and options['e'] then
|
||||||
print("gpg: please specify either -d or -e")
|
io.stderr:write("gpg: please specify either -d or -e\n")
|
||||||
return false
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if options['e'] then
|
||||||
|
local userPub = parseKey(args[1], true)
|
||||||
|
local tmpPub, tmpPriv = data.generateKeyPair(384)
|
||||||
|
local aesKey = data.md5(data.ecdh(tmpPriv, userPub))
|
||||||
|
local checkValue = data.sha256(aesKey)
|
||||||
|
local iv = data.random(16)
|
||||||
|
|
||||||
|
local d = data.encrypt(readFile(args[2]), aesKey, iv)
|
||||||
|
return writeFile(deriveName(args[2], true), {
|
||||||
|
t = "ecdh",
|
||||||
|
kdf = "md5",
|
||||||
|
iv = iv,
|
||||||
|
cv = checkValue,
|
||||||
|
k = {
|
||||||
|
t = tmpPub.keyType(),
|
||||||
|
d = tmpPub.serialize()
|
||||||
|
},
|
||||||
|
d = d
|
||||||
|
})
|
||||||
|
else
|
||||||
|
local userPriv = parseKey(args[1], false)
|
||||||
|
local d = readFile(args[2], true)
|
||||||
|
|
||||||
|
if d.t ~= "ecdh" then
|
||||||
|
io.stderr:write("gpg: file is not encrypted with a key\n")
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if options['e'] then
|
local tmpPub = data.deserializeKey(d.k.d, d.k.t)
|
||||||
local userPub = parseKey(args[1], true)
|
local aesKey = data.md5(data.ecdh(userPriv, tmpPub))
|
||||||
local tmpPub, tmpPriv = data.generateKeyPair(384)
|
|
||||||
local aesKey = data.md5(data.ecdh(tmpPriv, userPub))
|
|
||||||
local checkValue = data.sha256(aesKey)
|
|
||||||
local iv = data.random(16)
|
|
||||||
|
|
||||||
local d = data.encrypt(readFile(args[2]), aesKey, iv)
|
if d.cv ~= data.sha256(aesKey) then
|
||||||
return writeFile(deriveName(args[2], true), {
|
io.stderr:write("gpg: invalid key\n")
|
||||||
t = "ecdh",
|
return false
|
||||||
kdf = "md5",
|
|
||||||
iv = iv,
|
|
||||||
cv = checkValue,
|
|
||||||
k = {
|
|
||||||
t = tmpPub.keyType(),
|
|
||||||
d = tmpPub.serialize()
|
|
||||||
},
|
|
||||||
d = d
|
|
||||||
})
|
|
||||||
else
|
|
||||||
local userPriv = parseKey(args[1], false)
|
|
||||||
local d = readFile(args[2], true)
|
|
||||||
|
|
||||||
if d.t ~= "ecdh" then
|
|
||||||
print("gpg: file is not encrypted with a key")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local tmpPub = data.deserializeKey(d.k.d, d.k.t)
|
|
||||||
local aesKey = data.md5(data.ecdh(userPriv, tmpPub))
|
|
||||||
|
|
||||||
if d.cv ~= data.sha256(aesKey) then
|
|
||||||
print("gpg: invalid key")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return writeFile(deriveName(args[2], false), data.decrypt(d.d, aesKey, d.iv))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return writeFile(deriveName(args[2], false), data.decrypt(d.d, aesKey, d.iv))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if (options['s'] or options['v']) and (#args == 2) then
|
if (options['s'] or options['v']) and (#args == 2) then
|
||||||
ensureTier(2)
|
ensureMethods("deserializeKey", "ecdsa")
|
||||||
if options['s'] and options['v'] then
|
if options['s'] and options['v'] then
|
||||||
print("gpg: please specify either -s or -v")
|
io.stderr:write("gpg: please specify either -s or -v\n")
|
||||||
return false
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if options['s'] then
|
||||||
|
local userPriv = parseKey(args[1], false)
|
||||||
|
local sign = data.ecdsa(readFile(args[2]), userPriv)
|
||||||
|
|
||||||
|
return writeFile(args[2] .. ".sig", {
|
||||||
|
t = "ecdsa",
|
||||||
|
s = sign
|
||||||
|
})
|
||||||
|
else
|
||||||
|
local userPub = parseKey(args[1], true)
|
||||||
|
local sign = readFile(args[2] .. ".sig", true)
|
||||||
|
|
||||||
|
if sign.t ~= "ecdsa" then
|
||||||
|
io.stderr:write("gpg: unsupported signature type\n")
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if options['s'] then
|
if not data.ecdsa(readFile(args[2]), userPub, sign.s) then
|
||||||
local userPriv = parseKey(args[1], false)
|
io.stderr:write("gpg: signature verification failed\n")
|
||||||
local sign = data.ecdsa(readFile(args[2]), userPriv)
|
return false
|
||||||
|
|
||||||
return writeFile(args[2] .. ".sig", {
|
|
||||||
t = "ecdsa",
|
|
||||||
s = sign
|
|
||||||
})
|
|
||||||
else
|
|
||||||
local userPub = parseKey(args[1], true)
|
|
||||||
local sign = readFile(args[2] .. ".sig", true)
|
|
||||||
|
|
||||||
if sign.t ~= "ecdsa" then
|
|
||||||
print("gpg: unsupported signature type")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
if not data.ecdsa(readFile(args[2]), userPub, sign.s) then
|
|
||||||
print("gpg: signature verification failed")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
print("gpg: signature is valid")
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
io.write("gpg: signature is valid\n")
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
io.write("Usages:\n")
|
io.write("Usages:\n")
|
||||||
|
@ -18,21 +18,21 @@ function data.fromHex(hex)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if component.isAvailable("data") then
|
if component.isAvailable("data") then
|
||||||
local wrappedFunctions = { 'encode64', 'decode64', 'sha256', 'md5', 'crc32', 'deflate', 'inflate',
|
local wrappedFunctions = { 'encode64', 'decode64', 'sha256', 'md5', 'crc32', 'deflate', 'inflate',
|
||||||
'getLimit', 'tier', 'encrypt', 'decrypt', 'random', 'generateKeyPair',
|
'getLimit', 'tier', 'encrypt', 'decrypt', 'random', 'generateKeyPair',
|
||||||
'deserializeKey', 'ecdh', 'ecdsa' }
|
'deserializeKey', 'ecdh', 'ecdsa' }
|
||||||
|
|
||||||
function data.present()
|
function data.present()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
for i, v in ipairs(wrappedFunctions) do
|
for _, v in ipairs(wrappedFunctions) do
|
||||||
data[v] = component.data[v]
|
data[v] = component.data[v]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
function data.present()
|
function data.present()
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user