git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/schacha20blake3/schacha20blake3_test.go (about) 1 package schacha20blake3_test 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "errors" 7 "testing" 8 9 "git.sr.ht/~pingoo/stdx/crypto/schacha20blake3" 10 ) 11 12 func TestBasic(t *testing.T) { 13 var key [schacha20blake3.KeySize]byte 14 var nonce [schacha20blake3.NonceSize]byte 15 16 originalPlaintext := []byte("Hello World") 17 additionalData := []byte("!") 18 19 rand.Read(key[:]) 20 rand.Read(nonce[:]) 21 22 cipher, _ := schacha20blake3.New(key[:]) 23 ciphertext := cipher.Seal(nil, nonce[:], originalPlaintext, additionalData) 24 25 decryptedPlaintext, err := cipher.Open(nil, nonce[:], ciphertext, additionalData) 26 if err != nil { 27 t.Errorf("decrypting message: %s", err) 28 return 29 } 30 31 if !bytes.Equal(decryptedPlaintext, originalPlaintext) { 32 t.Errorf("original message (%s) != decrypted message (%s)", string(originalPlaintext), string(decryptedPlaintext)) 33 return 34 } 35 } 36 37 func TestAdditionalData(t *testing.T) { 38 var key [schacha20blake3.KeySize]byte 39 var nonce [schacha20blake3.NonceSize]byte 40 41 originalPlaintext := []byte("Hello World") 42 additionalData := []byte("!") 43 44 rand.Read(key[:]) 45 rand.Read(nonce[:]) 46 47 cipher, _ := schacha20blake3.New(key[:]) 48 ciphertext := cipher.Seal(nil, nonce[:], originalPlaintext, additionalData) 49 50 _, err := cipher.Open(nil, nonce[:], ciphertext, []byte{}) 51 if !errors.Is(err, schacha20blake3.ErrOpen) { 52 t.Errorf("expected error (%s) | got (%s)", schacha20blake3.ErrOpen, err) 53 return 54 } 55 }