go_chacha20poly1305_openssh/funcs_test.go

54 lines
1.1 KiB
Go

package cc20p1305ssh
import (
`bytes`
`encoding/hex`
`testing`
)
func TestNewCipher(t *testing.T) {
var err error
var c *ChaCha20Poly1305OpenSSH
var enc []byte
var plain []byte
var tag []byte
if c, err = New(testKdfKey); err != nil {
t.Fatal(err)
}
t.Logf("ChaCha20Poly1305OpenSSH:\n%#v", c)
// Decrypt...
if plain, err = c.Decrypt(testEncBlock, testPoly1305Tag); err != nil {
t.Log("Failed during decrypt!")
t.Fatal(err)
}
t.Logf("plain:\n%v", hex.EncodeToString(plain))
if !bytes.Equal(plain, testPlainBlock) {
t.Fatal("decrypted does not match!")
}
// And encrypt...
if enc, tag, err = c.Encrypt(plain); err != nil {
t.Log("Failed during encrypt!")
t.Fatal(err)
}
t.Logf("crypted:\n%v", hex.EncodeToString(enc))
if !bytes.Equal(enc, testEncBlock) {
t.Fatal("encrypted does not match!")
}
// And check tags.
if !bytes.Equal(tag, testPoly1305Tag) {
t.Logf("real tag: %v", hex.EncodeToString(testPoly1305Tag))
t.Logf("generated tag: %v", hex.EncodeToString(tag))
t.Fatal("tags do not match!")
}
t.Log("The cipher is functioning correctly.")
}