package nacl_test

import "seekia/internal/cryptography/nacl"

import "seekia/internal/helpers"

import "testing"

func TestEncryptDecryptNacl(t *testing.T) {

	testPublicKey, testPrivateKey, err := nacl.GetNewRandomPublicPrivateNaclKeys()
	if (err != nil){
		t.Fatal("Failed to derive Nacl keys: " + err.Error())
	}

	keyToEncrypt, err := helpers.GetNewRandom32ByteArray()
	if (err != nil){
		t.Fatal("Failed to get new random 32 byte array: " + err.Error())
	}

	encryptedBytes, err := nacl.EncryptKeyWithNacl(testPublicKey, keyToEncrypt)
	if (err != nil){
		t.Fatal("Failed to encrypt Nacl: " + err.Error())
	}

	ableToDecrypt, decryptedKey, err := nacl.DecryptNaclEncryptedKey(encryptedBytes, testPublicKey, testPrivateKey)
	if (err != nil){
		t.Fatal("Failed to decrypt Nacl: " + err.Error())
	}
	if (ableToDecrypt == false){
		t.Fatal("Failed to decrypt Nacl.")
	}

	if (keyToEncrypt != decryptedKey){
		t.Fatal("Nacl decrypted key does not match.") 
	}
}



