From d469b667c768b6a98fd3e333ec548480db0b1a5f Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 17 Sep 2018 16:41:02 +0200 Subject: [PATCH] Initial test Signed-off-by: Knut Ahlers --- .gitignore | 2 ++ Makefile | 8 +++++++ README.md | 19 +++++++++++++++++ go.mod | 3 +++ go.sum | 2 ++ index.html | 10 +++++++++ index.js | 15 ++++++++++++++ main.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 index.html create mode 100644 index.js create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf791a3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main.wasm* +wasm_exec.js diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..35fce71 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +default: build wasm_exec.js + +build: + GOOS=js GOARCH=wasm go build -o main.wasm main.go + gzip -c main.wasm > main.wasm.gz + +wasm_exec.js: + curl -sSfLo wasm_exec.js "https://raw.githubusercontent.com/golang/go/go1.11/misc/wasm/wasm_exec.js" diff --git a/README.md b/README.md new file mode 100644 index 0000000..9b0374f --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# go-openssl Wrapper for WASM support + +```console +$ make +GOOS=js GOARCH=wasm go build -o main.wasm main.go +gzip -c main.wasm > main.wasm.gz +curl -sSfLo wasm_exec.js "https://raw.githubusercontent.com/golang/go/go1.11/misc/wasm/wasm_exec.js" + +$ ls -lh main.wasm* +-rwxr-xr-x 1 luzifer luzifer 2.8M Sep 17 16:43 main.wasm +-rw-r--r-- 1 luzifer luzifer 611K Sep 17 16:43 main.wasm.gz +``` + +Chrome dev console output: + +``` +(index):14 (2) ["encryptResponse", "U2FsdGVkX1/baq9kUCX1EmUY/XZfnz7CwqGr70vqo6g="] +(index):10 (2) ["decryptResponse", "Knut"] +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..eb0d33f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module test + +require github.com/Luzifer/go-openssl v2.0.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..30e01b6 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/Luzifer/go-openssl v2.0.0+incompatible h1:EpNNxrPDji4rRzE0KeOeIeV7pHyKe8zF9oNnAXy4mBY= +github.com/Luzifer/go-openssl v2.0.0+incompatible/go.mod h1:t2qnLjT8WQ3usGU1R8uAqjY4T7CK7eMg9vhQ3l9Ue/Y= diff --git a/index.html b/index.html new file mode 100644 index 0000000..5e7ff39 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..b11b622 --- /dev/null +++ b/index.js @@ -0,0 +1,15 @@ +function decryptResponse(plaintext) { + console.log(["decryptResponse", plaintext]) +} + +function encryptResponse(ciphertext) { + console.log(["encryptResponse", ciphertext]) + decrypt(ciphertext, "password", decryptResponse) +} + +function wasmStartSuccess() { + encrypt("Knut", "password", encryptResponse) +} + +const go = new Go() +WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then(async obj => await go.run(obj.instance)) diff --git a/main.go b/main.go new file mode 100644 index 0000000..c2eab4e --- /dev/null +++ b/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "syscall/js" + + openssl "github.com/Luzifer/go-openssl" +) + +func main() { + js.Global().Set("decrypt", js.NewCallback(decrypt)) + js.Global().Set("encrypt", js.NewCallback(encrypt)) + + // Trigger custom "event" + js.Global().Call("wasmStartSuccess") + <-make(chan struct{}, 0) +} + +func decrypt(i []js.Value) { + if len(i) != 3 { + println("decrypt requires 3 arguments") + return + } + + var ( + ciphertext = i[0].String() + password = i[1].String() + callback = i[2] + ) + + o := openssl.New() + plaintext, err := o.DecryptString(password, ciphertext) + if err != nil { + println(fmt.Sprintf("decrypt failed: %s", err)) + return + } + + callback.Invoke(string(plaintext)) +} + +func encrypt(i []js.Value) { + if len(i) != 3 { + println("encrypt requires 3 arguments") + return + } + + var ( + plaintext = i[0].String() + password = i[1].String() + callback = i[2] + ) + + o := openssl.New() + ciphertext, err := o.EncryptString(password, plaintext) + if err != nil { + println(fmt.Sprintf("encrypt failed: %s", err)) + return + } + + callback.Invoke(string(ciphertext)) +}