2018-09-17 14:41:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall/js"
|
|
|
|
|
2020-06-13 13:44:15 +00:00
|
|
|
openssl "github.com/Luzifer/go-openssl/v4"
|
2018-09-17 14:41:02 +00:00
|
|
|
)
|
|
|
|
|
2020-06-13 13:44:15 +00:00
|
|
|
var defaultCG = openssl.PBKDF2SHA256
|
2019-08-24 15:11:28 +00:00
|
|
|
|
2018-09-17 14:41:02 +00:00
|
|
|
func main() {
|
2019-08-24 15:11:28 +00:00
|
|
|
js.Global().Set("opensslDecrypt", js.FuncOf(decrypt))
|
|
|
|
js.Global().Set("opensslEncrypt", js.FuncOf(encrypt))
|
2018-09-17 14:41:02 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-08-24 15:11:28 +00:00
|
|
|
func decrypt(this js.Value, i []js.Value) interface{} {
|
2018-09-17 14:41:02 +00:00
|
|
|
if len(i) != 3 {
|
|
|
|
println("decrypt requires 3 arguments")
|
2019-08-24 15:11:28 +00:00
|
|
|
return nil
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ciphertext = i[0].String()
|
|
|
|
password = i[1].String()
|
|
|
|
callback = i[2]
|
|
|
|
)
|
|
|
|
|
|
|
|
o := openssl.New()
|
2020-06-13 13:44:15 +00:00
|
|
|
plaintext, err := o.DecryptBytes(password, []byte(ciphertext), defaultCG)
|
2018-09-17 14:41:02 +00:00
|
|
|
if err != nil {
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(nil, fmt.Sprintf("decrypt failed: %s", err))
|
2019-08-24 15:11:28 +00:00
|
|
|
return nil
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(string(plaintext), nil)
|
2019-08-24 15:11:28 +00:00
|
|
|
return nil
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 15:11:28 +00:00
|
|
|
func encrypt(this js.Value, i []js.Value) interface{} {
|
2018-09-17 14:41:02 +00:00
|
|
|
if len(i) != 3 {
|
|
|
|
println("encrypt requires 3 arguments")
|
2019-08-24 15:11:28 +00:00
|
|
|
return nil
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
plaintext = i[0].String()
|
|
|
|
password = i[1].String()
|
|
|
|
callback = i[2]
|
|
|
|
)
|
|
|
|
|
|
|
|
o := openssl.New()
|
2020-06-13 13:44:15 +00:00
|
|
|
ciphertext, err := o.EncryptBytes(password, []byte(plaintext), defaultCG)
|
2018-09-17 14:41:02 +00:00
|
|
|
if err != nil {
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(nil, fmt.Sprintf("encrypt failed: %s", err))
|
2019-08-24 15:11:28 +00:00
|
|
|
return nil
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 12:06:59 +00:00
|
|
|
callback.Invoke(string(ciphertext), nil)
|
2019-08-24 15:11:28 +00:00
|
|
|
return nil
|
2018-09-17 14:41:02 +00:00
|
|
|
}
|