diff --git a/quartz/src/androidTest/java/com/vitorpamplona/quartz/crypto/CryptoUtilsTest.kt b/quartz/src/androidTest/java/com/vitorpamplona/quartz/crypto/CryptoUtilsTest.kt index b109a2d8d..ad5bcff7b 100644 --- a/quartz/src/androidTest/java/com/vitorpamplona/quartz/crypto/CryptoUtilsTest.kt +++ b/quartz/src/androidTest/java/com/vitorpamplona/quartz/crypto/CryptoUtilsTest.kt @@ -120,4 +120,20 @@ class CryptoUtilsTest { assertEquals(msg, decrypted) } + + @Test + fun signString() { + val random = "319cc5596fdd6cd767e5a59d976e8e059c61306af90dff1e6ee1067b3a1fdbc0".hexToByteArray() + val message = "8e58c8251bb406b6ded69e9eb14f55282a9a53bdab16fc49a3218c2ad3abc887".hexToByteArray() + val keyPair = KeyPair("a5ab474552c8f9c46c2eda5a0b68f27430ad81f96cb405e0cb4e34bf0c6494a2".hexToByteArray()) + + val signedMessage = CryptoUtils.sign(message, keyPair.privKey!!, random).toHexKey() + val expectedValue = "0f9be7e01ba53d5ee6874b9180c7956269fda7a5be424634c3d17b5cfcea6da001be89183876415ba08b7dafa6cff4555e393dc228fb8769b384344e9a27b77c" + assertEquals(expectedValue, signedMessage) + + val message2 = "Hello" + val signedMessage2 = CryptoUtils.signString(message2, keyPair.privKey!!, random).toHexKey() + val expectedValue2 = "7ec8194a585bfb513564113b6b7bfeaafa0254c99d24eaf92280657c2291bab908b1b7bc553c83276a0254aef5041bbe6a50e93381edc4de3d859efa1c3a5a1e" + assertEquals(expectedValue2, signedMessage2) + } } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt index 948684a0b..64d0efd82 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt @@ -64,10 +64,19 @@ object CryptoUtils { return secp256k1.secKeyVerify(il) } + fun signString( + message: String, + privKey: ByteArray, + auxrand32: ByteArray = random(32), + ): ByteArray { + return sign(sha256(message.toByteArray()), privKey, auxrand32) + } + fun sign( data: ByteArray, privKey: ByteArray, - ): ByteArray = secp256k1.signSchnorr(data, privKey, null) + auxrand32: ByteArray? = null, + ): ByteArray = secp256k1.signSchnorr(data, privKey, auxrand32) fun verifySignature( signature: ByteArray,