Skip to content

Commit

Permalink
pref(cbc): turn panic into friendly and controllable errors
Browse files Browse the repository at this point in the history
pref: gitignore vendor
  • Loading branch information
conero committed Jan 19, 2022
1 parent d0c02c8 commit 82169ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
*.out

.idea

# go-mod cache
vendor
9 changes: 9 additions & 0 deletions cbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openssl

import (
"crypto/cipher"
"errors"
)

// CBCEncrypt
Expand All @@ -11,6 +12,10 @@ func CBCEncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err

encryptData := make([]byte, len(src))

if len(iv) != block.BlockSize() {
return nil, errors.New("CBCEncrypt: IV length must equal block size")
}

mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encryptData, src)

Expand All @@ -22,6 +27,10 @@ func CBCDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err

dst := make([]byte, len(src))

if len(iv) != block.BlockSize() {
return nil, errors.New("CBCDecrypt: IV length must equal block size")
}

mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(dst, src)

Expand Down

0 comments on commit 82169ad

Please sign in to comment.