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