Skip to content

Commit

Permalink
Merge pull request #16 from conero/dev/pull
Browse files Browse the repository at this point in the history
pref(cbc): turn panic into friendly and controllable errors
  • Loading branch information
leeqvip authored Jan 21, 2022
2 parents 7edc4dd + 82169ad commit dbfc31a
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 dbfc31a

Please sign in to comment.