Is my encrypt function good for unit testing?

Written by
Link to Post by 

Hi all!

I have an encrypt function:

go func EncryptAES(plaintext []byte, key []byte, reader io.Reader) (ciphertext []byte, err error) { block, err := aes.NewCipher(key) if err != nil { return nil, fmt.Errorf("EncryptAES: %s", err) } ciphertext = make([]byte, aes.BlockSize+len(plaintext)) iv := ciphertext[:aes.BlockSize] _, err = io.ReadFull(reader, iv) if err != nil { return nil, fmt.Errorf("EncryptAES: %s", err) } stream := cipher.NewCFBEncrypter(block, iv) stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext) return ciphertext, nil }

Where I pass the io.Reader as a parameter so that I can easily unit test it with a fake reader and not cryptorand.Reader.

However, this forces an external user to pass this parameter cryptorand.Reader to that encrypt API function.

Would there be a better way to implement this such that the io.Reader is hidden from the external user (out of the package) but where the function is unit testable?


submitted by /u/dowitex
[link] [comments]

Article Tags:
· ·
Article Categories:

Leave a Reply