Refactors secure random to extension class

This commit is contained in:
Vitor Pamplona 2025-01-06 12:56:29 -05:00
parent 53458ab484
commit 0971b716e1
3 changed files with 33 additions and 12 deletions

View File

@ -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,

View File

@ -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) }

View File

@ -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)
}