Normalizes passwords to NFKC in NIP49

This commit is contained in:
Vitor Pamplona 2024-02-15 16:05:53 -05:00
parent 03a9ec8ed2
commit 6553f3bec5
2 changed files with 8 additions and 3 deletions

View File

@ -49,15 +49,17 @@ dependencies {
// Bitcoin secp256k1 bindings to Android
api 'fr.acinq.secp256k1:secp256k1-kmp-jni-android:0.13.0'
// LibSodium for ChaCha encryption
// LibSodium for ChaCha encryption (NIP-44)
implementation "com.goterl:lazysodium-android:5.1.0@aar"
implementation 'net.java.dev.jna:jna:5.14.0@aar'
// Performant Parser of JSONs into Events
api 'com.fasterxml.jackson.module:jackson-module-kotlin:2.16.1'
// immutable collections to avoid recomposition
api('org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.7')
// scrypt for NIP-49
api('com.lambdaworks:scrypt:1.4.0')
// Parses URLs from Text:

View File

@ -31,6 +31,7 @@ import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import fr.acinq.secp256k1.Secp256k1
import java.security.SecureRandom
import java.text.Normalizer
class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
private val libSodium = SodiumAndroid()
@ -50,8 +51,9 @@ class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
check(encryptedInfo != null) { "Couldn't decode key" }
check(encryptedInfo.version == EncryptedInfo.V) { "invalid version" }
val normalizedPassword = Normalizer.normalize(password, Normalizer.Form.NFKC).toByteArray(Charsets.UTF_8)
val n = Math.pow(2.0, encryptedInfo.logn.toDouble()).toInt()
val key = SCrypt.scrypt(password.toByteArray(Charsets.UTF_8), encryptedInfo.salt, n, 8, 1, 32)
val key = SCrypt.scrypt(normalizedPassword, encryptedInfo.salt, n, 8, 1, 32)
val m = ByteArray(32)
lazySodium.cryptoAeadXChaCha20Poly1305IetfDecrypt(
@ -93,8 +95,9 @@ class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
val nonce = ByteArray(24)
random.nextBytes(nonce)
val normalizedPassword = Normalizer.normalize(password, Normalizer.Form.NFKC).toByteArray(Charsets.UTF_8)
val n = Math.pow(2.0, logn.toDouble()).toInt()
val key = SCrypt.scrypt(password.toByteArray(Charsets.UTF_8), salt, n, 8, 1, 32)
val key = SCrypt.scrypt(normalizedPassword, salt, n, 8, 1, 32)
val ciphertext = ByteArray(48)
// byte[] c, long[] cLen,