1
0
Fork 0
mirror of https://github.com/Luzifer/wasm-openssl.git synced 2024-11-08 14:50:10 +00:00
wasm-openssl/main.go
Knut Ahlers dcb7ab36cf
Migrate to Go1.12 & go-openssl.v3
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-08-24 17:11:28 +02:00

68 lines
1.3 KiB
Go

package main
import (
"fmt"
"syscall/js"
openssl "github.com/Luzifer/go-openssl/v3"
)
var defaultKDF = openssl.DigestSHA256Sum
func main() {
js.Global().Set("opensslDecrypt", js.FuncOf(decrypt))
js.Global().Set("opensslEncrypt", js.FuncOf(encrypt))
// Trigger custom "event"
if js.Global().Get("opensslLoaded").Type() == js.TypeFunction {
js.Global().Call("opensslLoaded")
}
<-make(chan struct{}, 0)
}
func decrypt(this js.Value, i []js.Value) interface{} {
if len(i) != 3 {
println("decrypt requires 3 arguments")
return nil
}
var (
ciphertext = i[0].String()
password = i[1].String()
callback = i[2]
)
o := openssl.New()
plaintext, err := o.DecryptBytes(password, []byte(ciphertext), defaultKDF)
if err != nil {
callback.Invoke(nil, fmt.Sprintf("decrypt failed: %s", err))
return nil
}
callback.Invoke(string(plaintext), nil)
return nil
}
func encrypt(this js.Value, i []js.Value) interface{} {
if len(i) != 3 {
println("encrypt requires 3 arguments")
return nil
}
var (
plaintext = i[0].String()
password = i[1].String()
callback = i[2]
)
o := openssl.New()
ciphertext, err := o.EncryptBytes(password, []byte(plaintext), defaultKDF)
if err != nil {
callback.Invoke(nil, fmt.Sprintf("encrypt failed: %s", err))
return nil
}
callback.Invoke(string(ciphertext), nil)
return nil
}