mirror of
https://github.com/Luzifer/wasm-openssl.git
synced 2024-11-08 14:50:10 +00:00
Initial test
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
commit
d469b667c7
8 changed files with 120 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
main.wasm*
|
||||
wasm_exec.js
|
8
Makefile
Normal file
8
Makefile
Normal file
|
@ -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"
|
19
README.md
Normal file
19
README.md
Normal file
|
@ -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"]
|
||||
```
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module test
|
||||
|
||||
require github.com/Luzifer/go-openssl v2.0.0+incompatible
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -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=
|
10
index.html
Normal file
10
index.html
Normal file
|
@ -0,0 +1,10 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
<body>
|
||||
<script src="wasm_exec.js"></script>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
15
index.js
Normal file
15
index.js
Normal file
|
@ -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))
|
61
main.go
Normal file
61
main.go
Normal file
|
@ -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))
|
||||
}
|
Loading…
Reference in a new issue