diff --git a/cryptobox/crypto_box_seal.go b/cryptobox/crypto_box_seal.go index 1ec1677..09c99cf 100644 --- a/cryptobox/crypto_box_seal.go +++ b/cryptobox/crypto_box_seal.go @@ -8,7 +8,7 @@ import "github.com/GoKillers/libsodium-go/support" func CryptoBoxSeal(m []byte, pk []byte) ([]byte, int) { support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key") - c := make([]byte, len(m)+CryptoBoxMacBytes()) + c := make([]byte, len(m)+CryptoBoxSealBytes()) exit := int(C.crypto_box_seal( (*C.uchar)(&c[0]), (*C.uchar)(&m[0]), @@ -21,7 +21,7 @@ func CryptoBoxSeal(m []byte, pk []byte) ([]byte, int) { func CryptoBoxSealOpen(c []byte, pk []byte, sk []byte) ([]byte, int) { support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key") support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "secret key") - m := make([]byte, len(c)-CryptoBoxMacBytes()) + m := make([]byte, len(c)-CryptoBoxSealBytes()) exit := int(C.crypto_box_seal_open( (*C.uchar)(&m[0]), (*C.uchar)(&c[0]), diff --git a/cryptobox/crypto_box_seal_test.go b/cryptobox/crypto_box_seal_test.go new file mode 100644 index 0000000..5c3a73c --- /dev/null +++ b/cryptobox/crypto_box_seal_test.go @@ -0,0 +1,22 @@ +package cryptobox + +import "testing" + +func TestCryptoBoxSeal(t *testing.T) { + sk, pk, exit := CryptoBoxKeyPair() + if exit != 0 { + t.Fatalf("CryptoBoxKeyPair failed: %v", exit) + } + testStr := "test string 12345678901234567890123456789012345678901234567890" + cipherText, exit := CryptoBoxSeal([]byte(testStr), pk) + if exit != 0 { + t.Fatalf("CryptoBoxSeal failed: %v", exit) + } + plaintext, exit := CryptoBoxSealOpen(cipherText, pk, sk) + if exit != 0 { + t.Fatalf("CryptoBoxSealOpen failed: %v", exit) + } + if string(plaintext) != testStr { + t.Fatalf("Bad plaintext: %#v", plaintext) + } +}