Skip to content

Commit

Permalink
refactor: Change PBKDF2SHA512 from class to Object (#139)
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed Moussa <[email protected]>
  • Loading branch information
hamada147 committed May 20, 2024
1 parent fa1ea0c commit da7e6fe
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 117 deletions.
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)
}
}

0 comments on commit da7e6fe

Please sign in to comment.