libgo: Merge from revision 18783:00cce3a34d7e of master library.
[official-gcc.git] / libgo / go / crypto / cipher / cfb_test.go
blobec708ab2be29c8fff0ce18a0f584d786a0ca13fc
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.
5 package cipher_test
7 import (
8 "bytes"
9 "crypto/aes"
10 "crypto/cipher"
11 "crypto/rand"
12 "testing"
15 func TestCFB(t *testing.T) {
16 block, err := aes.NewCipher(commonKey128)
17 if err != nil {
18 t.Error(err)
19 return
22 plaintext := []byte("this is the plaintext. 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 copy(ciphertext, plaintext)
28 cfb.XORKeyStream(ciphertext, ciphertext)
30 cfbdec := cipher.NewCFBDecrypter(block, iv)
31 plaintextCopy := make([]byte, len(plaintext))
32 copy(plaintextCopy, ciphertext)
33 cfbdec.XORKeyStream(plaintextCopy, plaintextCopy)
35 if !bytes.Equal(plaintextCopy, plaintext) {
36 t.Errorf("got: %x, want: %x", plaintextCopy, plaintext)