Fix Kotlin encryptDecryptTest to decrypt with swapped private and public keys to follow NIP-44 documentation

This commit is contained in:
Terry Yiu
2024-03-22 23:51:57 -04:00
parent 694642b42b
commit c843e07709

View File

@@ -38,16 +38,16 @@ import java.security.MessageDigest
import java.security.SecureRandom import java.security.SecureRandom
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
public class NIP44v2Test { class NIP44v2Test {
val vectors: VectorFile = private val vectors: VectorFile =
jacksonObjectMapper() jacksonObjectMapper()
.readValue( .readValue(
getInstrumentation().context.assets.open("nip44.vectors.json"), getInstrumentation().context.assets.open("nip44.vectors.json"),
VectorFile::class.java, VectorFile::class.java,
) )
val random = SecureRandom() private val random = SecureRandom()
val nip44v2 = Nip44v2(Secp256k1.get(), random) private val nip44v2 = Nip44v2(Secp256k1.get(), random)
@Test @Test
fun conversationKeyTest() { fun conversationKeyTest() {
@@ -71,21 +71,25 @@ public class NIP44v2Test {
fun encryptDecryptTest() { fun encryptDecryptTest() {
for (v in vectors.v2?.valid?.encryptDecrypt!!) { for (v in vectors.v2?.valid?.encryptDecrypt!!) {
val pub2 = com.vitorpamplona.quartz.crypto.KeyPair(v.sec2!!.hexToByteArray()) val pub2 = com.vitorpamplona.quartz.crypto.KeyPair(v.sec2!!.hexToByteArray())
val conversationKey = nip44v2.getConversationKey(v.sec1!!.hexToByteArray(), pub2.pubKey) val conversationKey1 = nip44v2.getConversationKey(v.sec1!!.hexToByteArray(), pub2.pubKey)
assertEquals(v.conversationKey, conversationKey.toHexKey()) assertEquals(v.conversationKey, conversationKey1.toHexKey())
val ciphertext = val ciphertext =
nip44v2 nip44v2
.encryptWithNonce( .encryptWithNonce(
v.plaintext!!, v.plaintext!!,
conversationKey, conversationKey1,
v.nonce!!.hexToByteArray(), v.nonce!!.hexToByteArray(),
) )
.encodePayload() .encodePayload()
assertEquals(v.payload, ciphertext) assertEquals(v.payload, ciphertext)
val decrypted = nip44v2.decrypt(v.payload!!, conversationKey) val pub1 = com.vitorpamplona.quartz.crypto.KeyPair(v.sec1.hexToByteArray())
val conversationKey2 = nip44v2.getConversationKey(v.sec2.hexToByteArray(), pub1.pubKey)
assertEquals(v.conversationKey, conversationKey2.toHexKey())
val decrypted = nip44v2.decrypt(v.payload!!, conversationKey2)
assertEquals(v.plaintext, decrypted) assertEquals(v.plaintext, decrypted)
} }
} }
@@ -116,7 +120,7 @@ public class NIP44v2Test {
} }
@Test @Test
fun invalidMessageLenghts() { fun invalidMessageLengths() {
for (v in vectors.v2?.invalid?.encryptMsgLengths!!) { for (v in vectors.v2?.invalid?.encryptMsgLengths!!) {
val key = ByteArray(32) val key = ByteArray(32)
random.nextBytes(key) random.nextBytes(key)
@@ -154,7 +158,7 @@ public class NIP44v2Test {
} }
} }
fun sha256Hex(data: ByteArray): String { private fun sha256Hex(data: ByteArray): String {
// Creates a new buffer every time // Creates a new buffer every time
return MessageDigest.getInstance("SHA-256").digest(data).toHexKey() return MessageDigest.getInstance("SHA-256").digest(data).toHexKey()
} }