From dcb7ab36cf0139980ff4f861367e0c6301978b5c Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 24 Aug 2019 17:11:28 +0200 Subject: [PATCH] Migrate to Go1.12 & go-openssl.v3 Signed-off-by: Knut Ahlers --- main.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 6773e6b..0c27b28 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,14 @@ import ( "fmt" "syscall/js" - openssl "github.com/Luzifer/go-openssl" + openssl "github.com/Luzifer/go-openssl/v3" ) +var defaultKDF = openssl.DigestSHA256Sum + func main() { - js.Global().Set("opensslDecrypt", js.NewCallback(decrypt)) - js.Global().Set("opensslEncrypt", js.NewCallback(encrypt)) + 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 { @@ -19,10 +21,10 @@ func main() { <-make(chan struct{}, 0) } -func decrypt(i []js.Value) { +func decrypt(this js.Value, i []js.Value) interface{} { if len(i) != 3 { println("decrypt requires 3 arguments") - return + return nil } var ( @@ -32,19 +34,20 @@ func decrypt(i []js.Value) { ) o := openssl.New() - plaintext, err := o.DecryptString(password, ciphertext) + plaintext, err := o.DecryptBytes(password, []byte(ciphertext), defaultKDF) if err != nil { callback.Invoke(nil, fmt.Sprintf("decrypt failed: %s", err)) - return + return nil } callback.Invoke(string(plaintext), nil) + return nil } -func encrypt(i []js.Value) { +func encrypt(this js.Value, i []js.Value) interface{} { if len(i) != 3 { println("encrypt requires 3 arguments") - return + return nil } var ( @@ -54,11 +57,12 @@ func encrypt(i []js.Value) { ) o := openssl.New() - ciphertext, err := o.EncryptString(password, plaintext) + ciphertext, err := o.EncryptBytes(password, []byte(plaintext), defaultKDF) if err != nil { callback.Invoke(nil, fmt.Sprintf("encrypt failed: %s", err)) - return + return nil } callback.Invoke(string(ciphertext), nil) + return nil }