Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / external / bsd / netpgp / dist / bindings / lua / netpgp.lua
blobe68f172fd06420ccfd7991efee5c9073044782aa
1 #! /usr/bin/env lua
3 --
4 -- Copyright (c) 2009 The NetBSD Foundation, Inc.
5 -- All rights reserved.
6 --
7 -- This code is derived from software contributed to The NetBSD Foundation
8 -- by Alistair Crooks (agc@netbsd.org)
9 --
10 -- Redistribution and use in source and binary forms, with or without
11 -- modification, are permitted provided that the following conditions
12 -- are met:
13 -- 1. Redistributions of source code must retain the above copyright
14 -- notice, this list of conditions and the following disclaimer.
15 -- 2. Redistributions in binary form must reproduce the above copyright
16 -- notice, this list of conditions and the following disclaimer in the
17 -- documentation and/or other materials provided with the distribution.
19 -- THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 -- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 -- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 -- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 -- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 -- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 -- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 -- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 -- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 -- POSSIBILITY OF SUCH DAMAGE.
32 -- command line args
33 dofile "optparse.lua"
35 opt = OptionParser{usage="%prog [options] file", version="20090711"}
37 opt.add_option{"-s", "--sign", action="store_true", dest="sign", help="--sign [--detached] [--armour] file"}
38 opt.add_option{"-v", "--verify", action="store_true", dest="verify", help="--verify [--armour] file"}
39 opt.add_option{"-e", "--encrypt", action="store_true", dest="encrypt", help="--encrypt [--armour] file"}
40 opt.add_option{"-d", "--decrypt", action="store_true", dest="decrypt", help="--decrypt [--armour] file"}
41 opt.add_option{"-h", "--homedir", action="store", dest="homedir", help="--homedir directory"}
42 opt.add_option{"-o", "--output", action="store", dest="output", help="--output file"}
43 opt.add_option{"-a", "--armour", action="store_true", dest="armour", help="--armour"}
44 opt.add_option{"-D", "--detached", action="store_true", dest="detached", help="--detached"}
46 -- caller lua script
47 local extension = ".so"
48 f = io.open("libluanetpgp.dylib", "r")
49 if f then
50 extension = ".dylib"
51 io.close(f)
52 end
53 glupkg = package.loadlib("libluanetpgp" .. extension, "luaopen_netpgp")
54 netpgp = glupkg()
56 -- initialise
57 pgp = netpgp.new()
59 -- parse command line args
60 options,args = opt.parse_args()
62 -- set defaults
63 local output = options.output or ""
64 local armour = "binary"
65 if options.armour then
66 armour = "armour"
67 end
68 local detached = "attached"
69 if options.detached then
70 detached = "detached"
71 end
72 if options.homedir then
73 netpgp.homedir(pgp, options.homedir)
74 end
76 -- initialise everything
77 netpgp.init(pgp)
79 local i
80 for i = 1, #args do
81 if options.encrypt then
82 -- encrypt a file
83 netpgp.encrypt_file(pgp, args[1], output, armour)
84 os.execute("ls -l " .. args[1] .. ".gpg")
85 end
86 if options.decrypt then
87 -- decrypt file
88 netpgp.decrypt_file(pgp, args[1], output, armour)
89 end
90 if options.sign then
91 -- detached signature
92 netpgp.sign_file(pgp, args[1], output, armour, detached)
93 os.execute("ls -l " .. args[1] .. ".sig")
94 end
95 if options.verify then
96 -- verification of detached signature
97 netpgp.verify_file(pgp, args[1], armour)
98 end
99 end