Skip to content

Commit

Permalink
fixing issue with wasm lib that results in base64 encoded decrypted text
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Jul 1, 2024
1 parent 812a668 commit 738fcd5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
10 changes: 5 additions & 5 deletions internal/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func encryptStr() js.Func {
if len(args) != 2 {
return jsReturn(nil, fmt.Errorf("Supported arguments: public key, secret key or password (required), message (required)"))
}
pk := args[0].String()
keyOrPwd := args[0].String()
message := args[1].String()
ciphertext, err := utils.EncryptData(pk, []byte(message))
ciphertext, err := utils.EncryptData(keyOrPwd, []byte(message))
if err != nil {
return jsReturn(nil, err)
}
Expand All @@ -79,13 +79,13 @@ func decryptStr() js.Func {
if len(args) != 2 {
return jsReturn(nil, fmt.Errorf("Supported arguments: secret key or password (required), ciphertext (required)"))
}
secret := args[0].String()
secretKeyOrPwd := args[0].String()
ciphertext := args[1].String()
message, err := utils.DecryptData(secret, ciphertext)
message, err := utils.DecryptData(secretKeyOrPwd, ciphertext)
if err != nil {
return jsReturn(nil, err)
}
return jsReturn(message, nil)
return jsReturn(string(message), nil)
})
return jsonFunc
}
8 changes: 8 additions & 0 deletions utils/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func DecryptData(secretKeyOrPwd string, ctStr string) ([]byte, error) {
return data, nil
}

func DecryptingReader(secretKeyOrPwd string, src io.Reader) (io.Reader, error) {
secretKey, err := getSecretKey(secretKeyOrPwd)
if err != nil {
return nil, err
}
return secretKey.NewDecryptingReader(src)
}

func DecryptStream(secretKeyOrPwd string, dst io.Writer, src io.Reader) (err error) {
secretKey, err := getSecretKey(secretKeyOrPwd)
if err != nil {
Expand Down
25 changes: 18 additions & 7 deletions utils/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,35 @@ func EncryptData(keyOrPwd string, data []byte) (string, error) {
}
}

func EncryptStream(keyOrPwd string, dst io.Writer, src io.Reader, compress bool) (err error) {
func EncryptingWriter(keyOrPwd string, dst io.Writer, compress bool) (io.WriteCloser, error) {
if IsPubKeyStr(keyOrPwd) {
pubKey, err := pubKeyFromStr(keyOrPwd)
if err != nil {
return err
return nil, err
}
return pubKey.EncryptStream(dst, src, compress)
return pubKey.NewEncryptingWriter(dst, compress)
} else if IsSecretKeyStr(keyOrPwd) {
secretKey, err := secretKeyFromStr(keyOrPwd)
if err != nil {
return err
return nil, err
}
return secretKey.EncryptStream(dst, src, compress)
return secretKey.NewEncryptingWriter(dst, compress)
} else {
secretKey, err := xipher.NewSecretKeyForPassword([]byte(keyOrPwd))
if err != nil {
return err
return nil, err
}
return secretKey.EncryptStream(dst, src, compress)
return secretKey.NewEncryptingWriter(dst, compress)
}
}

func EncryptStream(keyOrPwd string, dst io.Writer, src io.Reader, compress bool) (err error) {
encryptingWriter, err := EncryptingWriter(keyOrPwd, dst, compress)
if err != nil {
return err
}
if _, err = io.Copy(encryptingWriter, src); err != nil {
return err
}
return encryptingWriter.Close()
}

0 comments on commit 738fcd5

Please sign in to comment.