1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package cipher_test
6
7import (
8	"bytes"
9	"crypto/aes"
10	"crypto/cipher"
11	"crypto/rand"
12	"testing"
13)
14
15func TestCFB(t *testing.T) {
16	block, err := aes.NewCipher(commonKey128)
17	if err != nil {
18		t.Error(err)
19		return
20	}
21
22	plaintext := []byte("this is the plaintext")
23	iv := make([]byte, block.BlockSize())
24	rand.Reader.Read(iv)
25	cfb := cipher.NewCFBEncrypter(block, iv)
26	ciphertext := make([]byte, len(plaintext))
27	cfb.XORKeyStream(ciphertext, plaintext)
28
29	cfbdec := cipher.NewCFBDecrypter(block, iv)
30	plaintextCopy := make([]byte, len(plaintext))
31	cfbdec.XORKeyStream(plaintextCopy, ciphertext)
32
33	if !bytes.Equal(plaintextCopy, plaintext) {
34		t.Errorf("got: %x, want: %x", plaintextCopy, plaintext)
35	}
36}
37