42 lines
792 B
Go
42 lines
792 B
Go
package chacha8
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestChaCha8Roundtrip(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
for i := range key {
|
|
key[i] = byte(i + 1)
|
|
}
|
|
nonce := make([]byte, 12)
|
|
for i := range nonce {
|
|
nonce[i] = byte(i + 0x10)
|
|
}
|
|
|
|
c1, err := New(key, nonce)
|
|
if err != nil {
|
|
t.Fatalf("New failed: %v", err)
|
|
}
|
|
|
|
c2, err := New(key, nonce)
|
|
if err != nil {
|
|
t.Fatalf("New failed: %v", err)
|
|
}
|
|
|
|
original := []byte("Hello, Ananta NetEase UXChaCha8 stream cipher verification!")
|
|
data := make([]byte, len(original))
|
|
copy(data, original)
|
|
|
|
c1.XorInPlace(data)
|
|
if bytes.Equal(data, original) {
|
|
t.Errorf("Ciphertext equals plaintext")
|
|
}
|
|
|
|
c2.XorInPlace(data)
|
|
if !bytes.Equal(data, original) {
|
|
t.Errorf("Decrypted data != original: got %q, want %q", data, original)
|
|
}
|
|
}
|