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:
Florian Nücke 2015-07-25 11:19:17 +02:00
parent f27e0c1d5a
commit fd96b309a5
2 changed files with 182 additions and 179 deletions

View File

@ -13,8 +13,8 @@ local args, options = shell.parse(...)
local function writeFile(path, data)
if filesystem.exists(path) then
print("gpg: failed to write file: " .. path)
print("gpg: error was: file already exists")
io.stderr:write("gpg: failed to write file: " .. path .. "\n")
io.stderr:write("gpg: error was: file already exists\n")
return false
end
@ -25,8 +25,8 @@ local function writeFile(path, data)
local h, err = io.open(path, "wb")
if not h then
print("gpg: failed to write file: " .. path)
print("gpg: error was: " .. err)
io.stderr:write("gpg: failed to write file: " .. path .. "\n")
io.stderr:write("gpg: error was: " .. err .. "\n")
return false
end
@ -52,12 +52,12 @@ local function parseKey(path, isPublic)
local k, err = data.deserializeKey(d.d, d.t)
if not k then
print("gpg: failed to parse key: " .. err)
io.stderr:write("gpg: failed to parse key: " .. err .. "\n")
return nil
end
if k.isPublic() ~= isPublic then
print("gpg: wrong key type")
io.stderr:write("gpg: wrong key type\n")
return nil
end
@ -71,38 +71,41 @@ local function deriveName(base, encrypt)
local d = base:gsub(".gpg", "")
if d == base then
d = d .. ".dec"
print("gpg: decrypting to " .. d)
io.write("gpg: decrypting to " .. d .. "\n")
end
return d
end
end
local function ensureTier(tier)
local function ensureMethods(...)
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")
end
if data.tier() < tier then
print("gpg: you must have tier " .. tier .. " data card in order to run this program")
error("data card is too simple")
local names = table.pack(...)
for i = 1, names.n do
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
if options['g'] and (#args == 2) then
ensureTier(3)
ensureMethods("generateKeyPair")
local pub, priv = data.generateKeyPair(384)
priv = { t = priv.keyType(), d = priv.serialize() }
pub = { t = pub.keyType(), d = pub.serialize() }
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
end
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
end
@ -110,9 +113,9 @@ if options['g'] and (#args == 2) then
end
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
print("gpg: please specify either -d or -e")
io.stderr:write("gpg: please specify either -d or -e\n")
return false
end
@ -135,12 +138,12 @@ if options['c'] and (options['e'] or options['d']) and (#args == 1) then
local d = readFile(args[1], true)
if d.t ~= "pwd" then
print("gpg: file is not encrypted with a password")
io.stderr:write("gpg: file is not encrypted with a password\n")
return false
end
if checkValue ~= d.cv then
print("gpg: password incorrect")
io.stderr:write("gpg: password incorrect\n")
return false
end
@ -149,9 +152,9 @@ if options['c'] and (options['e'] or options['d']) and (#args == 1) then
end
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
print("gpg: please specify either -d or -e")
io.stderr:write("gpg: please specify either -d or -e\n")
return false
end
@ -179,7 +182,7 @@ if (options['d'] or options['e']) and (#args == 2) then
local d = readFile(args[2], true)
if d.t ~= "ecdh" then
print("gpg: file is not encrypted with a key")
io.stderr:write("gpg: file is not encrypted with a key\n")
return false
end
@ -187,7 +190,7 @@ if (options['d'] or options['e']) and (#args == 2) then
local aesKey = data.md5(data.ecdh(userPriv, tmpPub))
if d.cv ~= data.sha256(aesKey) then
print("gpg: invalid key")
io.stderr:write("gpg: invalid key\n")
return false
end
@ -196,9 +199,9 @@ if (options['d'] or options['e']) and (#args == 2) then
end
if (options['s'] or options['v']) and (#args == 2) then
ensureTier(2)
ensureMethods("deserializeKey", "ecdsa")
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
end
@ -215,16 +218,16 @@ if (options['s'] or options['v']) and (#args == 2) then
local sign = readFile(args[2] .. ".sig", true)
if sign.t ~= "ecdsa" then
print("gpg: unsupported signature type")
io.stderr:write("gpg: unsupported signature type\n")
return false
end
if not data.ecdsa(readFile(args[2]), userPub, sign.s) then
print("gpg: signature verification failed")
io.stderr:write("gpg: signature verification failed\n")
return false
end
print("gpg: signature is valid")
io.write("gpg: signature is valid\n")
return true
end

View File

@ -26,7 +26,7 @@ if component.isAvailable("data") then
return true
end
for i, v in ipairs(wrappedFunctions) do
for _, v in ipairs(wrappedFunctions) do
data[v] = component.data[v]
end
else