From 2a2f009a920f4b0da730188bc41d38ecae8efba1 Mon Sep 17 00:00:00 2001 From: Putu Aditya Bayu Trana Suta Date: Tue, 30 Apr 2024 16:57:09 +0700 Subject: [PATCH] Fix error (#3) --- .idea/git_toolbox_prj.xml | 15 +++++ .idea/go-crypto.iml | 9 +++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 119 ++++++++++++++++++++++++++++++++++++++ crypto.go | 14 +++-- 6 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 .idea/git_toolbox_prj.xml create mode 100644 .idea/go-crypto.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml new file mode 100644 index 0000000..02b915b --- /dev/null +++ b/.idea/git_toolbox_prj.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/go-crypto.iml b/.idea/go-crypto.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/go-crypto.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8d490d6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..29520ab --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,119 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + \ No newline at end of file diff --git a/crypto.go b/crypto.go index 661e3c8..583ca6d 100644 --- a/crypto.go +++ b/crypto.go @@ -30,7 +30,7 @@ func (crypto CryptoKey) Encrypt(data string) (string, error) { } if len(plainText)%aes.BlockSize != 0 { - err := fmt.Errorf(`plainText: "%s" has the wrong block size`, plainText) + err = fmt.Errorf(`plainText: "%s" has the wrong block size`, plainText) return "", err } @@ -41,7 +41,7 @@ func (crypto CryptoKey) Encrypt(data string) (string, error) { cipherText := make([]byte, aes.BlockSize+len(plainText)) iv := cipherText[:aes.BlockSize] - if _, err := io.ReadFull(rand.Reader, iv); err != nil { + if _, err = io.ReadFull(rand.Reader, iv); err != nil { return "", err } @@ -63,7 +63,10 @@ func (crypto CryptoKey) Decrypt(encrypted string) (string, error) { encrypted = split[0] + split[1] - cipherText, _ := hex.DecodeString(encrypted) + cipherText, err := hex.DecodeString(encrypted) + if err != nil { + return "", err + } block, err := aes.NewCipher(crypto.key) if err != nil { @@ -83,7 +86,10 @@ func (crypto CryptoKey) Decrypt(encrypted string) (string, error) { mode := cipher.NewCBCDecrypter(block, iv) mode.CryptBlocks(cipherText, cipherText) - cipherText, _ = pkcs7.Unpad(cipherText, aes.BlockSize) + cipherText, err = pkcs7.Unpad(cipherText, aes.BlockSize) + if err != nil { + return "", err + } return string(cipherText), nil }