Moves Sha256 to a pool of digests to double the hashing performance
This commit is contained in:
@@ -20,40 +20,46 @@
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip01Core
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
class EventHasher {
|
||||
companion object {
|
||||
fun makeJsonObjectForId(
|
||||
pubKey: HexKey,
|
||||
createdAt: Long,
|
||||
kind: Int,
|
||||
tags: Array<Array<String>>,
|
||||
content: String,
|
||||
): ArrayNode {
|
||||
val factory = JsonNodeFactory.instance
|
||||
return factory.arrayNode(6).apply {
|
||||
add(0)
|
||||
add(pubKey)
|
||||
add(createdAt)
|
||||
add(kind)
|
||||
add(
|
||||
factory.arrayNode(tags.size).apply {
|
||||
tags.forEach { tag ->
|
||||
add(
|
||||
factory.arrayNode(tag.size).apply { tag.forEach { add(it) } },
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
add(content)
|
||||
}
|
||||
}
|
||||
|
||||
fun makeJsonForId(
|
||||
pubKey: HexKey,
|
||||
createdAt: Long,
|
||||
kind: Int,
|
||||
tags: Array<Array<String>>,
|
||||
content: String,
|
||||
): String {
|
||||
val factory = JsonNodeFactory.instance
|
||||
val rawEvent =
|
||||
factory.arrayNode(6).apply {
|
||||
add(0)
|
||||
add(pubKey)
|
||||
add(createdAt)
|
||||
add(kind)
|
||||
add(
|
||||
factory.arrayNode(tags.size).apply {
|
||||
tags.forEach { tag ->
|
||||
add(
|
||||
factory.arrayNode(tag.size).apply { tag.forEach { add(it) } },
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
add(content)
|
||||
}
|
||||
|
||||
return EventMapper.toJson(rawEvent)
|
||||
}
|
||||
): String = EventMapper.toJson(makeJsonObjectForId(pubKey, createdAt, kind, tags, content))
|
||||
|
||||
fun hashIdBytes(
|
||||
pubKey: HexKey,
|
||||
|
||||
@@ -54,7 +54,6 @@ data class Address(
|
||||
Address(parts[0].toInt(), parts[1], parts[2])
|
||||
} else {
|
||||
Log.w("AddressableId", "Error parsing. Pubkey is not hex: $addressId")
|
||||
throw RuntimeException("It shouldn't get here.")
|
||||
null
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
package com.vitorpamplona.quartz.nip06KeyDerivation
|
||||
|
||||
import com.vitorpamplona.quartz.nip49PrivKeyEnc.PBKDF
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
// CODE FROM: https://github.com/ACINQ/bitcoin-kmp/
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ import android.util.Log
|
||||
import com.vitorpamplona.quartz.utils.LibSodiumInstance
|
||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||
import com.vitorpamplona.quartz.utils.Secp256k1Instance
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import java.util.Base64
|
||||
|
||||
class Nip44v1 {
|
||||
|
||||
@@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip55AndroidSigner
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.Nip01
|
||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
fun signString(
|
||||
message: String,
|
||||
|
||||
@@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip57Zaps
|
||||
|
||||
import com.vitorpamplona.quartz.nip04Dm.crypto.Nip04
|
||||
import com.vitorpamplona.quartz.nip19Bech32.bech32.Bech32
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import java.nio.charset.Charset
|
||||
import java.security.SecureRandom
|
||||
import javax.crypto.BadPaddingException
|
||||
|
||||
@@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip98HttpAuth.tags
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.toHexKey
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
class PayloadHashTag {
|
||||
companion object {
|
||||
|
||||
+3
-6
@@ -18,11 +18,8 @@
|
||||
* 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.utils
|
||||
package com.vitorpamplona.quartz.utils.sha256
|
||||
|
||||
import java.security.MessageDigest
|
||||
val pool = Sha256Pool(5) // max parallel operations
|
||||
|
||||
fun sha256(data: ByteArray): ByteArray {
|
||||
// Creates a new buffer every time
|
||||
return MessageDigest.getInstance("SHA-256").digest(data)
|
||||
}
|
||||
fun sha256(data: ByteArray) = pool.hash(data)
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 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.utils.sha256
|
||||
|
||||
import android.util.Log
|
||||
import java.security.MessageDigest
|
||||
import java.util.concurrent.ArrayBlockingQueue
|
||||
|
||||
class Sha256Pool(
|
||||
size: Int,
|
||||
) {
|
||||
private val pool = ArrayBlockingQueue<MessageDigest>(size)
|
||||
|
||||
private fun digest() = MessageDigest.getInstance("SHA-256")
|
||||
|
||||
init {
|
||||
repeat(size) {
|
||||
pool.add(digest())
|
||||
}
|
||||
}
|
||||
|
||||
private fun acquire(): MessageDigest {
|
||||
if (pool.size < 1) {
|
||||
Log.w("SHA256Pool", "Pool running low in available digests")
|
||||
}
|
||||
return pool.take()
|
||||
}
|
||||
|
||||
private fun release(digest: MessageDigest) {
|
||||
digest.reset()
|
||||
pool.put(digest)
|
||||
}
|
||||
|
||||
fun hash(byteArray: ByteArray): ByteArray {
|
||||
val digest = acquire()
|
||||
try {
|
||||
return digest.digest(byteArray)
|
||||
} finally {
|
||||
release(digest)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user