1
0
Fork 0
mirror of https://github.com/Luzifer/wasm-openssl.git synced 2024-11-09 15:20:06 +00:00
wasm-openssl/main.go
Knut Ahlers 7049fc4299
Convert to be auto-build compliant
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-09-18 18:40:13 +02:00

61 lines
1.1 KiB
Go

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("opensslLoaded")
<-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 {
callback.Invoke(nil, fmt.Sprintf("decrypt failed: %s", err))
return
}
callback.Invoke(string(plaintext), nil)
}
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 {
callback.Invoke(nil, fmt.Sprintf("encrypt failed: %s", err))
return
}
callback.Invoke(string(ciphertext), nil)
}