Normalizes passwords to NFKC in NIP49
This commit is contained in:
+3
-1
@@ -49,15 +49,17 @@ dependencies {
|
|||||||
// Bitcoin secp256k1 bindings to Android
|
// Bitcoin secp256k1 bindings to Android
|
||||||
api 'fr.acinq.secp256k1:secp256k1-kmp-jni-android:0.13.0'
|
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 "com.goterl:lazysodium-android:5.1.0@aar"
|
||||||
implementation 'net.java.dev.jna:jna:5.14.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'
|
api 'com.fasterxml.jackson.module:jackson-module-kotlin:2.16.1'
|
||||||
|
|
||||||
// immutable collections to avoid recomposition
|
// immutable collections to avoid recomposition
|
||||||
api('org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.7')
|
api('org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.7')
|
||||||
|
|
||||||
|
// scrypt for NIP-49
|
||||||
api('com.lambdaworks:scrypt:1.4.0')
|
api('com.lambdaworks:scrypt:1.4.0')
|
||||||
|
|
||||||
// Parses URLs from Text:
|
// Parses URLs from Text:
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import com.vitorpamplona.quartz.encoders.hexToByteArray
|
|||||||
import com.vitorpamplona.quartz.encoders.toHexKey
|
import com.vitorpamplona.quartz.encoders.toHexKey
|
||||||
import fr.acinq.secp256k1.Secp256k1
|
import fr.acinq.secp256k1.Secp256k1
|
||||||
import java.security.SecureRandom
|
import java.security.SecureRandom
|
||||||
|
import java.text.Normalizer
|
||||||
|
|
||||||
class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
|
class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
|
||||||
private val libSodium = SodiumAndroid()
|
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 != null) { "Couldn't decode key" }
|
||||||
check(encryptedInfo.version == EncryptedInfo.V) { "invalid version" }
|
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 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)
|
val m = ByteArray(32)
|
||||||
|
|
||||||
lazySodium.cryptoAeadXChaCha20Poly1305IetfDecrypt(
|
lazySodium.cryptoAeadXChaCha20Poly1305IetfDecrypt(
|
||||||
@@ -93,8 +95,9 @@ class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
|
|||||||
val nonce = ByteArray(24)
|
val nonce = ByteArray(24)
|
||||||
random.nextBytes(nonce)
|
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 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)
|
val ciphertext = ByteArray(48)
|
||||||
|
|
||||||
// byte[] c, long[] cLen,
|
// byte[] c, long[] cLen,
|
||||||
|
|||||||
Reference in New Issue
Block a user