Refactors SHA-256 methods to its own class
This commit is contained in:
@@ -74,7 +74,7 @@ object CryptoUtils {
|
||||
|
||||
fun sha256(data: ByteArray): ByteArray {
|
||||
// Creates a new buffer every time
|
||||
return nip01.sha256(data)
|
||||
return sha256Hash(data)
|
||||
}
|
||||
|
||||
fun decrypt(
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* 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.MessageDigest
|
||||
|
||||
fun sha256Hash(data: ByteArray): ByteArray {
|
||||
// Creates a new buffer every time
|
||||
return MessageDigest.getInstance("SHA-256").digest(data)
|
||||
}
|
||||
@@ -22,8 +22,8 @@ package com.vitorpamplona.quartz.crypto.nip01
|
||||
|
||||
import com.vitorpamplona.quartz.crypto.CryptoUtils
|
||||
import com.vitorpamplona.quartz.crypto.nextBytes
|
||||
import com.vitorpamplona.quartz.crypto.sha256Hash
|
||||
import fr.acinq.secp256k1.Secp256k1
|
||||
import java.security.MessageDigest
|
||||
import java.security.SecureRandom
|
||||
|
||||
class Nip01(
|
||||
@@ -54,10 +54,7 @@ class Nip01(
|
||||
pubKey: ByteArray,
|
||||
): Boolean = secp256k1.verifySchnorr(signature, hash, pubKey)
|
||||
|
||||
fun sha256(data: ByteArray): ByteArray {
|
||||
// Creates a new buffer every time
|
||||
return MessageDigest.getInstance("SHA-256").digest(data)
|
||||
}
|
||||
fun sha256(data: ByteArray) = sha256Hash(data)
|
||||
|
||||
fun signString(
|
||||
message: String,
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
package com.vitorpamplona.quartz.crypto.nip06
|
||||
|
||||
import com.vitorpamplona.quartz.crypto.nip49.PBKDF
|
||||
import java.security.MessageDigest
|
||||
import com.vitorpamplona.quartz.crypto.sha256Hash
|
||||
|
||||
// CODE FROM: https://github.com/ACINQ/bitcoin-kmp/
|
||||
|
||||
@@ -37,11 +37,6 @@ object Bip39Mnemonics {
|
||||
return zeroes + digits
|
||||
}
|
||||
|
||||
fun sha256(data: ByteArray): ByteArray {
|
||||
// Creates a new buffer every time
|
||||
return MessageDigest.getInstance("SHA-256").digest(data)
|
||||
}
|
||||
|
||||
private fun toBinary(x: ByteArray): List<Boolean> = x.map(Bip39Mnemonics::toBinary).flatten()
|
||||
|
||||
private fun fromBinary(bin: List<Boolean>): Int = bin.fold(0) { acc, flag -> if (flag) 2 * acc + 1 else 2 * acc }
|
||||
@@ -84,7 +79,7 @@ object Bip39Mnemonics {
|
||||
val databits = bits.subList(0, bitlength)
|
||||
val checksumbits = bits.subList(bitlength, bits.size)
|
||||
val data = group(databits, 8).map { fromBinary(it) }.map { it.toByte() }.toByteArray()
|
||||
val check = toBinary(sha256(data)).take(data.size / 4)
|
||||
val check = toBinary(sha256Hash(data)).take(data.size / 4)
|
||||
require(check == checksumbits) { "invalid checksum" }
|
||||
}
|
||||
|
||||
@@ -111,7 +106,7 @@ object Bip39Mnemonics {
|
||||
wordlist: Array<String>,
|
||||
): List<String> {
|
||||
require(wordlist.size == 2048) { "invalid word list (size should be 2048)" }
|
||||
val digits = toBinary(entropy) + toBinary(sha256(entropy)).take(entropy.size / 4)
|
||||
val digits = toBinary(entropy) + toBinary(sha256Hash(entropy)).take(entropy.size / 4)
|
||||
|
||||
return group(digits, 11).map(Bip39Mnemonics::fromBinary).map { wordlist[it] }
|
||||
}
|
||||
|
||||
@@ -24,9 +24,9 @@ import android.util.Log
|
||||
import com.goterl.lazysodium.SodiumAndroid
|
||||
import com.goterl.lazysodium.utils.Key
|
||||
import com.vitorpamplona.quartz.crypto.SharedKeyCache
|
||||
import com.vitorpamplona.quartz.crypto.sha256Hash
|
||||
import com.vitorpamplona.quartz.encoders.Hex
|
||||
import fr.acinq.secp256k1.Secp256k1
|
||||
import java.security.MessageDigest
|
||||
import java.security.SecureRandom
|
||||
import java.util.Base64
|
||||
|
||||
@@ -126,15 +126,10 @@ class Nip44v1(
|
||||
privateKey: ByteArray,
|
||||
pubKey: ByteArray,
|
||||
): ByteArray =
|
||||
sha256(
|
||||
sha256Hash(
|
||||
secp256k1.pubKeyTweakMul(h02 + pubKey, privateKey).copyOfRange(1, 33),
|
||||
)
|
||||
|
||||
fun sha256(data: ByteArray): ByteArray {
|
||||
// Creates a new buffer every time
|
||||
return MessageDigest.getInstance("SHA-256").digest(data)
|
||||
}
|
||||
|
||||
class EncryptedInfo(
|
||||
val ciphertext: ByteArray,
|
||||
val nonce: ByteArray,
|
||||
|
||||
@@ -35,10 +35,10 @@ import com.fasterxml.jackson.databind.module.SimpleModule
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.vitorpamplona.quartz.crypto.CryptoUtils
|
||||
import com.vitorpamplona.quartz.crypto.sha256Hash
|
||||
import com.vitorpamplona.quartz.encoders.ATag
|
||||
import com.vitorpamplona.quartz.encoders.Hex
|
||||
import com.vitorpamplona.quartz.encoders.HexKey
|
||||
import com.vitorpamplona.quartz.encoders.Nip01Serializer
|
||||
import com.vitorpamplona.quartz.encoders.Nip19Bech32
|
||||
import com.vitorpamplona.quartz.encoders.PoWRank
|
||||
import com.vitorpamplona.quartz.encoders.toHexKey
|
||||
@@ -52,7 +52,6 @@ import com.vitorpamplona.quartz.utils.pointerSizeInBytes
|
||||
import com.vitorpamplona.quartz.utils.remove
|
||||
import com.vitorpamplona.quartz.utils.startsWith
|
||||
import java.math.BigDecimal
|
||||
import java.security.MessageDigest
|
||||
|
||||
@Immutable
|
||||
open class Event(
|
||||
@@ -312,11 +311,6 @@ open class Event(
|
||||
return id.equals(generateId())
|
||||
}
|
||||
|
||||
fun hasCorrectIDHash2(): Boolean {
|
||||
if (id.isEmpty()) return false
|
||||
return id.equals(generateId2())
|
||||
}
|
||||
|
||||
fun hasVerifiedSignature(): Boolean {
|
||||
if (id.isEmpty() || sig.isEmpty()) return false
|
||||
return CryptoUtils.verifySignature(Hex.decode(sig), Hex.decode(id), Hex.decode(pubKey))
|
||||
@@ -349,13 +343,7 @@ open class Event(
|
||||
|
||||
fun makeJsonForId(): String = makeJsonForId(pubKey, createdAt, kind, tags, content)
|
||||
|
||||
fun generateId(): String = CryptoUtils.sha256(makeJsonForId().toByteArray()).toHexKey()
|
||||
|
||||
fun generateId2(): String {
|
||||
val sha256 = MessageDigest.getInstance("SHA-256")
|
||||
Nip01Serializer().serializeEventInto(this, Nip01Serializer.BufferedDigestWriter(sha256))
|
||||
return sha256.digest().toHexKey()
|
||||
}
|
||||
fun generateId(): String = sha256Hash(makeJsonForId().toByteArray()).toHexKey()
|
||||
|
||||
private class EventDeserializer : StdDeserializer<Event>(Event::class.java) {
|
||||
override fun deserialize(
|
||||
|
||||
Reference in New Issue
Block a user