diff --git a/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt b/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt index 7b971fd28..c1a203709 100644 --- a/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt +++ b/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt @@ -7,24 +7,22 @@ import org.bitcoinj.crypto.PBKDF2SHA512 * deriving a key from a password using the PBKDF2 algorithm with SHA-512 * hash function. */ -actual class PBKDF2SHA512 { - actual companion object { - /** - * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. - * - * @param p The password used for key derivation. - * @param s The salt value. - * @param c The iteration count. - * @param dkLen The desired length of the derived key in bytes. - * @return The derived key as a ByteArray. - */ - actual fun derive( - p: String, - s: String, - c: Int, - dkLen: Int - ): ByteArray { - return PBKDF2SHA512.derive(p, s, c, dkLen) - } +actual object PBKDF2SHA512 { + /** + * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. + * + * @param p The password used for key derivation. + * @param s The salt value. + * @param c The iteration count. + * @param dkLen The desired length of the derived key in bytes. + * @return The derived key as a ByteArray. + */ + actual fun derive( + p: String, + s: String, + c: Int, + dkLen: Int + ): ByteArray { + return PBKDF2SHA512.derive(p, s, c, dkLen) } } diff --git a/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt b/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt index 2670dfb12..d97c1eebe 100644 --- a/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt +++ b/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt @@ -13,50 +13,48 @@ import platform.CoreCrypto.kCCPRFHmacAlgSHA512 * deriving a key from a password using the PBKDF2 algorithm with SHA-512 * hash function. */ -actual class PBKDF2SHA512 { - actual companion object { - /** - * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. - * - * @param p The password used for key derivation. - * @param s The salt value. - * @param c The iteration count. - * @param dkLen The desired length of the derived key in bytes. - * @return The derived key as a ByteArray. - */ - @OptIn(ExperimentalUnsignedTypes::class, ExperimentalForeignApi::class) - actual fun derive( - p: String, - s: String, - c: Int, - dkLen: Int - ): ByteArray { - val rounds = c.toUInt() - val salt = s.encodeToByteArray().toUByteArray() - val derivedKey = UByteArray(dkLen) // initialize buffer for derived key +actual object PBKDF2SHA512 { + /** + * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. + * + * @param p The password used for key derivation. + * @param s The salt value. + * @param c The iteration count. + * @param dkLen The desired length of the derived key in bytes. + * @return The derived key as a ByteArray. + */ + @OptIn(ExperimentalUnsignedTypes::class, ExperimentalForeignApi::class) + actual fun derive( + p: String, + s: String, + c: Int, + dkLen: Int + ): ByteArray { + val rounds = c.toUInt() + val salt = s.encodeToByteArray().toUByteArray() + val derivedKey = UByteArray(dkLen) // initialize buffer for derived key - derivedKey.usePinned { derivedKeyPin -> - salt.usePinned { saltPin -> + derivedKey.usePinned { derivedKeyPin -> + salt.usePinned { saltPin -> - val result = CCKeyDerivationPBKDF( - algorithm = kCCPBKDF2, - password = p, - passwordLen = p.length.convert(), - salt = saltPin.addressOf(0), - saltLen = saltPin.get().size.convert(), - prf = kCCPRFHmacAlgSHA512, - rounds = rounds, - derivedKey = derivedKeyPin.addressOf(0), - derivedKeyLen = derivedKeyPin.get().size.convert() - ) + val result = CCKeyDerivationPBKDF( + algorithm = kCCPBKDF2, + password = p, + passwordLen = p.length.convert(), + salt = saltPin.addressOf(0), + saltLen = saltPin.get().size.convert(), + prf = kCCPRFHmacAlgSHA512, + rounds = rounds, + derivedKey = derivedKeyPin.addressOf(0), + derivedKeyLen = derivedKeyPin.get().size.convert() + ) - if (result != 0) { - throw Error("Key derivation failed with error: $result") - } + if (result != 0) { + throw Error("Key derivation failed with error: $result") } } - - return derivedKey.toByteArray() } + + return derivedKey.toByteArray() } } diff --git a/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt b/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt index e75d435bf..bee1cec9f 100644 --- a/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt +++ b/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt @@ -5,17 +5,15 @@ package io.iohk.atala.prism.apollo.hashing * deriving a key from a password using the PBKDF2 algorithm with SHA-512 * hash function. */ -expect class PBKDF2SHA512 { - companion object { - /** - * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. - * - * @param p The password used for key derivation. - * @param s The salt value. - * @param c The iteration count. - * @param dkLen The desired length of the derived key in bytes. - * @return The derived key as a ByteArray. - */ - fun derive(p: String, s: String, c: Int, dkLen: Int): ByteArray - } +expect object PBKDF2SHA512 { + /** + * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. + * + * @param p The password used for key derivation. + * @param s The salt value. + * @param c The iteration count. + * @param dkLen The desired length of the derived key in bytes. + * @return The derived key as a ByteArray. + */ + fun derive(p: String, s: String, c: Int, dkLen: Int): ByteArray } diff --git a/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt b/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt index 98530f6f1..c74979b05 100644 --- a/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt +++ b/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt @@ -10,33 +10,31 @@ import org.khronos.webgl.get * deriving a key from a password using the PBKDF2 algorithm with SHA-512 * hash function. */ -actual class PBKDF2SHA512 { - actual companion object { - /** - * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. - * - * @param p The password used for key derivation. - * @param s The salt value. - * @param c The iteration count. - * @param dkLen The desired length of the derived key in bytes. - * @return The derived key as a ByteArray. - */ - actual fun derive( - p: String, - s: String, - c: Int, - dkLen: Int - ): ByteArray { - val opts = js("{}") - opts.c = 2048 - opts.dkLen = 64 - val derived = pbkdf2(sha512, p, s, opts).buffer - val uint8Array = Uint8Array(derived) - val byteArray = ByteArray(uint8Array.length) - for (i in 0 until uint8Array.length) { - byteArray[i] = uint8Array[i].toByte() - } - return byteArray +actual object PBKDF2SHA512 { + /** + * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. + * + * @param p The password used for key derivation. + * @param s The salt value. + * @param c The iteration count. + * @param dkLen The desired length of the derived key in bytes. + * @return The derived key as a ByteArray. + */ + actual fun derive( + p: String, + s: String, + c: Int, + dkLen: Int + ): ByteArray { + val opts = js("{}") + opts.c = 2048 + opts.dkLen = 64 + val derived = pbkdf2(sha512, p, s, opts).buffer + val uint8Array = Uint8Array(derived) + val byteArray = ByteArray(uint8Array.length) + for (i in 0 until uint8Array.length) { + byteArray[i] = uint8Array[i].toByte() } + return byteArray } } diff --git a/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt b/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt index 7b971fd28..c1a203709 100644 --- a/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt +++ b/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt @@ -7,24 +7,22 @@ import org.bitcoinj.crypto.PBKDF2SHA512 * deriving a key from a password using the PBKDF2 algorithm with SHA-512 * hash function. */ -actual class PBKDF2SHA512 { - actual companion object { - /** - * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. - * - * @param p The password used for key derivation. - * @param s The salt value. - * @param c The iteration count. - * @param dkLen The desired length of the derived key in bytes. - * @return The derived key as a ByteArray. - */ - actual fun derive( - p: String, - s: String, - c: Int, - dkLen: Int - ): ByteArray { - return PBKDF2SHA512.derive(p, s, c, dkLen) - } +actual object PBKDF2SHA512 { + /** + * Derives a key from a password using the PBKDF2 algorithm with SHA-512 hash function. + * + * @param p The password used for key derivation. + * @param s The salt value. + * @param c The iteration count. + * @param dkLen The desired length of the derived key in bytes. + * @return The derived key as a ByteArray. + */ + actual fun derive( + p: String, + s: String, + c: Int, + dkLen: Int + ): ByteArray { + return PBKDF2SHA512.derive(p, s, c, dkLen) } }