mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-03-28 18:51:45 +01:00
Refactors secure random to extension class
This commit is contained in:
parent
53458ab484
commit
0971b716e1
@ -45,19 +45,15 @@ object CryptoUtils {
|
|||||||
nip44.clearCache()
|
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 */
|
/** Provides a 32B "private key" aka random number */
|
||||||
fun privkeyCreate() = nip01.privkeyCreate()
|
fun privkeyCreate() = nip01.privkeyCreate()
|
||||||
|
|
||||||
fun pubkeyCreate(privKey: ByteArray) = nip01.pubkeyCreate(privKey)
|
fun pubkeyCreate(privKey: ByteArray) = nip01.pubkeyCreate(privKey)
|
||||||
|
|
||||||
|
fun randomInt(bound: Int) = random.nextInt(bound)
|
||||||
|
|
||||||
|
fun random(size: Int) = random.nextBytes(size)
|
||||||
|
|
||||||
fun signString(
|
fun signString(
|
||||||
message: String,
|
message: String,
|
||||||
privKey: ByteArray,
|
privKey: ByteArray,
|
||||||
|
@ -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) }
|
@ -21,7 +21,7 @@
|
|||||||
package com.vitorpamplona.quartz.crypto.nip01
|
package com.vitorpamplona.quartz.crypto.nip01
|
||||||
|
|
||||||
import com.vitorpamplona.quartz.crypto.CryptoUtils
|
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 fr.acinq.secp256k1.Secp256k1
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
import java.security.SecureRandom
|
import java.security.SecureRandom
|
||||||
@ -31,7 +31,7 @@ class Nip01(
|
|||||||
val random: SecureRandom,
|
val random: SecureRandom,
|
||||||
) {
|
) {
|
||||||
/** Provides a 32B "private key" aka random number */
|
/** 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))
|
fun compressedPubkeyCreate(privKey: ByteArray) = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(privKey))
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ class Nip01(
|
|||||||
fun sign(
|
fun sign(
|
||||||
data: ByteArray,
|
data: ByteArray,
|
||||||
privKey: ByteArray,
|
privKey: ByteArray,
|
||||||
auxrand32: ByteArray? = random(32),
|
auxrand32: ByteArray? = random.nextBytes(32),
|
||||||
): ByteArray = secp256k1.signSchnorr(data, privKey, auxrand32)
|
): ByteArray = secp256k1.signSchnorr(data, privKey, auxrand32)
|
||||||
|
|
||||||
fun signDeterministic(
|
fun signDeterministic(
|
||||||
@ -62,6 +62,6 @@ class Nip01(
|
|||||||
fun signString(
|
fun signString(
|
||||||
message: String,
|
message: String,
|
||||||
privKey: ByteArray,
|
privKey: ByteArray,
|
||||||
auxrand32: ByteArray = random(32),
|
auxrand32: ByteArray = random.nextBytes(32),
|
||||||
): ByteArray = sign(CryptoUtils.sha256(message.toByteArray()), privKey, auxrand32)
|
): ByteArray = sign(CryptoUtils.sha256(message.toByteArray()), privKey, auxrand32)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user