Refactors SHA-256 methods to its own class

This commit is contained in:
Vitor Pamplona
2025-01-06 12:56:57 -05:00
parent 0971b716e1
commit 5f577df819
10 changed files with 54 additions and 62 deletions
@@ -21,6 +21,7 @@
package com.vitorpamplona.quartz.crypto.nip01
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.crypto.sha256Hash
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import fr.acinq.secp256k1.Secp256k1
@@ -66,7 +67,7 @@ class Nip01Test {
fun testDeterministicSign() {
assertEquals(
"1484d0e0bd62165e822e31f1f4cc8e1ce8e20c30a060e24fb0ecd7baf7c624f661fb7a3e4f0ddb43018e5f0b4892c929af64d8b7a86021aa081ec8231e3dfa37",
nip01.signDeterministic(nip01.sha256("Test".toByteArray()), privateKey).toHexKey(),
nip01.signDeterministic(sha256Hash("Test".toByteArray()), privateKey).toHexKey(),
)
}
@@ -74,7 +75,7 @@ class Nip01Test {
fun testSha256() {
assertEquals(
"532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25",
nip01.sha256("Test".toByteArray()).toHexKey(),
sha256Hash("Test".toByteArray()).toHexKey(),
)
}
@@ -83,7 +84,7 @@ class Nip01Test {
assertTrue(
nip01.verify(
"1484d0e0bd62165e822e31f1f4cc8e1ce8e20c30a060e24fb0ecd7baf7c624f661fb7a3e4f0ddb43018e5f0b4892c929af64d8b7a86021aa081ec8231e3dfa37".hexToByteArray(),
nip01.sha256("Test".toByteArray()),
sha256Hash("Test".toByteArray()),
nip01.pubkeyCreate(privateKey),
),
)
@@ -93,17 +94,17 @@ class Nip01Test {
fun testNonDeterministicSign() {
assertNotEquals(
"1484d0e0bd62165e822e31f1f4cc8e1ce8e20c30a060e24fb0ecd7baf7c624f661fb7a3e4f0ddb43018e5f0b4892c929af64d8b7a86021aa081ec8231e3dfa37",
nip01.sign(nip01.sha256("Test".toByteArray()), privateKey).toHexKey(),
nip01.sign(sha256Hash("Test".toByteArray()), privateKey).toHexKey(),
)
}
@Test
fun testNonDeterministicSignVerify() {
val signature = nip01.sign(nip01.sha256("Test".toByteArray()), privateKey)
val signature = nip01.sign(sha256Hash("Test".toByteArray()), privateKey)
assertTrue(
nip01.verify(
signature,
nip01.sha256("Test".toByteArray()),
sha256Hash("Test".toByteArray()),
nip01.pubkeyCreate(privateKey),
),
)
@@ -25,6 +25,7 @@ import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.crypto.nip01.Nip01
import com.vitorpamplona.quartz.crypto.sha256Hash
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import fr.acinq.secp256k1.Secp256k1
@@ -34,7 +35,6 @@ import junit.framework.TestCase.assertNull
import junit.framework.TestCase.fail
import org.junit.Test
import org.junit.runner.RunWith
import java.security.MessageDigest
import java.security.SecureRandom
@RunWith(AndroidJUnit4::class)
@@ -185,8 +185,5 @@ class NIP44v2Test {
}
}
private fun sha256Hex(data: ByteArray): String {
// Creates a new buffer every time
return MessageDigest.getInstance("SHA-256").digest(data).toHexKey()
}
private fun sha256Hex(data: ByteArray) = sha256Hash(data).toHexKey()
}