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 76ecb05da..1d80512de 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt @@ -45,19 +45,15 @@ object CryptoUtils { nip44.clearCache() } - fun randomInt(bound: Int): Int = random.nextInt(bound) - - fun random(size: Int): ByteArray { - val bytes = ByteArray(size) - random.nextBytes(bytes) - return bytes - } - /** Provides a 32B "private key" aka random number */ fun privkeyCreate() = nip01.privkeyCreate() fun pubkeyCreate(privKey: ByteArray) = nip01.pubkeyCreate(privKey) + fun randomInt(bound: Int) = random.nextInt(bound) + + fun random(size: Int) = random.nextBytes(size) + fun signString( message: String, privKey: ByteArray, diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/RandomExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/RandomExt.kt new file mode 100644 index 000000000..2c999c358 --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/RandomExt.kt @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.crypto + +import java.security.SecureRandom + +fun SecureRandom.nextBytes(size: Int) = ByteArray(size).also { nextBytes(it) } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/nip01/Nip01.kt b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/nip01/Nip01.kt index abd2a0554..f688a2119 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/nip01/Nip01.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/nip01/Nip01.kt @@ -21,7 +21,7 @@ package com.vitorpamplona.quartz.crypto.nip01 import com.vitorpamplona.quartz.crypto.CryptoUtils -import com.vitorpamplona.quartz.crypto.CryptoUtils.random +import com.vitorpamplona.quartz.crypto.nextBytes import fr.acinq.secp256k1.Secp256k1 import java.security.MessageDigest import java.security.SecureRandom @@ -31,7 +31,7 @@ class Nip01( val random: SecureRandom, ) { /** Provides a 32B "private key" aka random number */ - fun privkeyCreate() = random(32) + fun privkeyCreate() = random.nextBytes(32) fun compressedPubkeyCreate(privKey: ByteArray) = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(privKey)) @@ -40,7 +40,7 @@ class Nip01( fun sign( data: ByteArray, privKey: ByteArray, - auxrand32: ByteArray? = random(32), + auxrand32: ByteArray? = random.nextBytes(32), ): ByteArray = secp256k1.signSchnorr(data, privKey, auxrand32) fun signDeterministic( @@ -62,6 +62,6 @@ class Nip01( fun signString( message: String, privKey: ByteArray, - auxrand32: ByteArray = random(32), + auxrand32: ByteArray = random.nextBytes(32), ): ByteArray = sign(CryptoUtils.sha256(message.toByteArray()), privKey, auxrand32) }