2018-09-17 14:41:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall/js"
|
|
|
|
|
2018-09-18 16:22:10 +00:00
|
|
|
openssl "github.com/Luzifer/go-openssl"
|
2018-09-17 14:41:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
js.Global().Set("decrypt", js.NewCallback(decrypt))
|
|
|
|
js.Global().Set("encrypt", js.NewCallback(encrypt))
|
|
|
|
|
|
|
|
// Trigger custom "event"
|
2018-09-18 17:01:52 +00:00
|
|
|
if js.Global().Get("opensslLoaded").Type() == js.TypeFunction {
|
|
|
|
js.Global().Call("opensslLoaded")
|
|
|
|
}
|
|
|
|
|
2018-09-17 14:41:02 +00:00
|
|
|
<-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 {
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(nil, fmt.Sprintf("decrypt failed: %s", err))
|
2018-09-17 14:41:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(string(plaintext), nil)
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(nil, fmt.Sprintf("encrypt failed: %s", err))
|
2018-09-17 14:41:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(string(ciphertext), nil)
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|