github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/crypto/cipher/cfb_test.go (about)

     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  
     5  package cipher_test
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/aes"
    10  	"crypto/cipher"
    11  	"crypto/rand"
    12  	"testing"
    13  )
    14  
    15  func 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  }