Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Change PBKDF2SHA512 from class to Object #139

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}