aboutsummaryrefslogtreecommitdiff
path: root/glue/libs
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2017-01-12 11:01:28 -0800
committerChristine Dodrill <me@christine.website>2017-01-12 11:01:28 -0800
commit0df20ebe2d6061ca8a0f2be30a2a654f30f46f52 (patch)
tree2d960e94cc84be02813a91ac13c03ec6b67d6942 /glue/libs
parent906a7b686c5ed845c4c0b40c34b94af7651a8d3e (diff)
downloadx-0df20ebe2d6061ca8a0f2be30a2a654f30f46f52.tar.xz
x-0df20ebe2d6061ca8a0f2be30a2a654f30f46f52.zip
glue: add expect
Diffstat (limited to 'glue/libs')
-rw-r--r--glue/libs/gluaexpect/expect.go35
-rw-r--r--glue/libs/gluasimplebox/sb.go100
2 files changed, 135 insertions, 0 deletions
diff --git a/glue/libs/gluaexpect/expect.go b/glue/libs/gluaexpect/expect.go
new file mode 100644
index 0000000..b7e57d4
--- /dev/null
+++ b/glue/libs/gluaexpect/expect.go
@@ -0,0 +1,35 @@
+package gluaexpect
+
+import (
+ "github.com/ThomasRooney/gexpect"
+ luar "github.com/layeh/gopher-luar"
+ lua "github.com/yuin/gopher-lua"
+)
+
+func Preload(L *lua.LState) {
+ L.PreloadModule("expect", Loader)
+}
+
+// Loader is the module loader function.
+func Loader(L *lua.LState) int {
+ mod := L.SetFuncs(L.NewTable(), api)
+ L.Push(mod)
+ return 1
+}
+
+var api = map[string]lua.LGFunction{
+ "spawn": spawn,
+}
+
+func spawn(L *lua.LState) int {
+ cmd := L.CheckString(1)
+ child, err := gexpect.Spawn(cmd)
+ if err != nil {
+ L.Push(lua.LNil)
+ L.Push(lua.LString(err.Error()))
+ return 2
+ }
+
+ L.Push(luar.New(L, child))
+ return 1
+}
diff --git a/glue/libs/gluasimplebox/sb.go b/glue/libs/gluasimplebox/sb.go
new file mode 100644
index 0000000..cbb707e
--- /dev/null
+++ b/glue/libs/gluasimplebox/sb.go
@@ -0,0 +1,100 @@
+package gluasimplebox
+
+import (
+ "crypto/rand"
+ "encoding/base64"
+ "encoding/hex"
+ "errors"
+
+ "github.com/brandur/simplebox"
+ luar "github.com/layeh/gopher-luar"
+ lua "github.com/yuin/gopher-lua"
+)
+
+func Preload(L *lua.LState) {
+ L.PreloadModule("simplebox", Loader)
+}
+
+// Loader is the module loader function.
+func Loader(L *lua.LState) int {
+ mod := L.SetFuncs(L.NewTable(), api)
+ L.Push(mod)
+ return 1
+}
+
+var api = map[string]lua.LGFunction{
+ "new": newSecretBox,
+ "genkey": genKey,
+}
+
+func newSecretBox(L *lua.LState) int {
+ key := L.CheckString(1)
+
+ k, err := parseKey(key)
+ if err != nil {
+ L.Push(lua.LNil)
+ L.Push(lua.LString(err.Error()))
+ return 2
+ }
+
+ sb := simplebox.NewFromSecretKey(k)
+
+ L.Push(luar.New(L, &box{sb: sb}))
+ return 1
+}
+
+func genKey(L *lua.LState) int {
+ key, err := generateKey()
+ if err != nil {
+ L.Push(lua.LNil)
+ L.Push(lua.LString(err.Error()))
+ return 2
+ }
+
+ L.Push(lua.LString(base64.URLEncoding.EncodeToString(key[:])))
+ return 1
+}
+
+func generateKey() (*[32]byte, error) {
+ var k [32]byte
+ _, err := rand.Read(k[:])
+ if err != nil {
+ return nil, err
+ }
+ return &k, nil
+}
+
+func parseKey(s string) (*[32]byte, error) {
+ k := &[32]byte{}
+ raw, err := base64.URLEncoding.DecodeString(s)
+ if err != nil {
+ return nil, err
+ }
+ if n := copy(k[:], raw); n < len(k) {
+ return nil, errors.New("not valid")
+ }
+ return k, nil
+}
+
+type box struct {
+ sb *simplebox.SimpleBox
+}
+
+func (b *box) Encrypt(data string) string {
+ result := b.sb.Encrypt([]byte(data))
+ return hex.EncodeToString(result)
+}
+
+func (b *box) Decrypt(data string) (string, error) {
+ d, err := hex.DecodeString(data)
+ if err != nil {
+ return "", err
+ }
+
+ plain, err := b.sb.Decrypt([]byte(d))
+ if err != nil {
+ return "", err
+ }
+
+ return string(plain), nil
+}