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 2df57ab352
Prefix functions to prevent collisions
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-09-18 19:24:43 +02:00

64 lines
1.2 KiB
Go

package main
import (
"fmt"
"syscall/js"
openssl "github.com/Luzifer/go-openssl"
)
func main() {
js.Global().Set("opensslDecrypt", js.NewCallback(decrypt))
js.Global().Set("opensslEncrypt", js.NewCallback(encrypt))
// Trigger custom "event"
if js.Global().Get("opensslLoaded").Type() == js.TypeFunction {
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)
}