Skip to content

Commit

Permalink
Fix error (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
putuadityabayu committed Apr 30, 2024
1 parent b5db520 commit 2a2f009
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .idea/git_toolbox_prj.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/go-crypto.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit 2a2f009

Please sign in to comment.