Skip to content

Commit 41a73b3

Browse files
authored
Merge pull request #10 from tyiu/fix-kotlin-encrypt-decrypt-test
Fix Kotlin encryptDecryptTest to decrypt with swapped private and public keys to follow NIP-44 documentation
2 parents b60eef2 + 7e09349 commit 41a73b3

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

kotlin/nip44-shared/src/androidInstrumentedTest/kotlin/NIP44v2Test.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class NIP44v2Test {
2121

2222
private val nip44v2 = getNip44()
2323

24-
fun sha256Hex(data: ByteArray): String {
24+
private fun sha256Hex(data: ByteArray): String {
2525
// Creates a new buffer every time
2626
return Hex.encode(MessageDigest.getInstance("SHA-256").digest(data))
2727
}
@@ -51,19 +51,23 @@ class NIP44v2Test {
5151
val secp256k1 = Secp256k1.get()
5252
for (v in vectors.v2?.valid?.encryptDecrypt!!) {
5353
val pub2 = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(Hex.decode(v.sec2!!))).copyOfRange(1, 33)
54-
val conversationKey = nip44v2.getConversationKey(Hex.decode(v.sec1!!), pub2)
55-
assertEquals(v.conversationKey, Hex.encode(conversationKey))
54+
val conversationKey1 = nip44v2.getConversationKey(Hex.decode(v.sec1!!), pub2)
55+
assertEquals(v.conversationKey, Hex.encode(conversationKey1))
5656

5757
val ciphertext = nip44v2.encryptWithNonce(
5858
v.plaintext!!,
59-
conversationKey,
59+
conversationKey1,
6060
Hex.decode(v.nonce!!)
6161
).encode()
6262

6363
assertEquals(v.payload, ciphertext)
6464

65-
val decrypted = nip44v2.decrypt(v.payload!!, conversationKey)
66-
assertEquals(v.plaintext, decrypted)
65+
val pub1 = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(Hex.decode(v.sec1))).copyOfRange(1, 33)
66+
val conversationKey2 = nip44v2.getConversationKey(Hex.decode(v.sec2), pub1)
67+
assertEquals(v.conversationKey, Hex.encode(conversationKey2))
68+
69+
val decrypted2 = nip44v2.decrypt(v.payload!!, conversationKey2)
70+
assertEquals(v.plaintext, decrypted2)
6771
}
6872
}
6973

@@ -90,14 +94,14 @@ class NIP44v2Test {
9094
}
9195

9296
@Test
93-
fun invalidMessageLenghts() {
97+
fun invalidMessageLengths() {
9498
val random = SecureRandom()
9599
for (v in vectors.v2?.invalid?.encryptMsgLengths!!) {
96100
val key = ByteArray(32)
97101
random.nextBytes(key)
98102
try {
99103
nip44v2.encrypt("a".repeat(v), key)
100-
fail("Should Throw for ${v}")
104+
fail("Should Throw for $v")
101105
} catch (e: Exception) {
102106
assertNotNull(e)
103107
}

kotlin/nip44-shared/src/jvmTest/kotlin/NIP44v2Test.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class NIP44v2Test {
1919
private val secp256k1 = Secp256k1.get()
2020
private val nip44v2 = getNip44()
2121

22-
fun sha256Hex(data: ByteArray): String {
22+
private fun sha256Hex(data: ByteArray): String {
2323
// Creates a new buffer every time
2424
return Hex.encode(MessageDigest.getInstance("SHA-256").digest(data))
2525
}
@@ -48,19 +48,23 @@ class NIP44v2Test {
4848
fun encryptDecryptTest() {
4949
for (v in vectors.v2?.valid?.encryptDecrypt!!) {
5050
val pub2 = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(Hex.decode(v.sec2!!))).copyOfRange(1, 33)
51-
val conversationKey = nip44v2.getConversationKey(Hex.decode(v.sec1!!), pub2)
52-
assertEquals(v.conversationKey, Hex.encode(conversationKey))
51+
val conversationKey1 = nip44v2.getConversationKey(Hex.decode(v.sec1!!), pub2)
52+
assertEquals(v.conversationKey, Hex.encode(conversationKey1))
5353

5454
val ciphertext = nip44v2.encryptWithNonce(
5555
v.plaintext!!,
56-
conversationKey,
56+
conversationKey1,
5757
Hex.decode(v.nonce!!)
5858
).encode()
5959

6060
assertEquals(v.payload, ciphertext)
6161

62-
val decrypted = nip44v2.decrypt(v.payload!!, conversationKey)
63-
assertEquals(v.plaintext, decrypted)
62+
val pub1 = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(Hex.decode(v.sec1))).copyOfRange(1, 33)
63+
val conversationKey2 = nip44v2.getConversationKey(Hex.decode(v.sec2), pub1)
64+
assertEquals(v.conversationKey, Hex.encode(conversationKey2))
65+
66+
val decrypted2 = nip44v2.decrypt(v.payload!!, conversationKey2)
67+
assertEquals(v.plaintext, decrypted2)
6468
}
6569
}
6670

@@ -87,13 +91,13 @@ class NIP44v2Test {
8791
}
8892

8993
@Test
90-
fun invalidMessageLenghts() {
94+
fun invalidMessageLengths() {
9195
for (v in vectors.v2?.invalid?.encryptMsgLengths!!) {
9296
val key = ByteArray(32)
9397
random.nextBytes(key)
9498
try {
9599
nip44v2.encrypt("a".repeat(v), key)
96-
fail("Should Throw for ${v}")
100+
fail("Should Throw for $v")
97101
} catch (e: Exception) {
98102
assertNotNull(e)
99103
}

0 commit comments

Comments
 (0)