-
Notifications
You must be signed in to change notification settings - Fork 11
/
Pbkdf2Factory.kt
35 lines (29 loc) · 928 Bytes
/
Pbkdf2Factory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.redmadrobot.pinkman.internal
import android.os.Build
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec
internal object Pbkdf2Factory {
private const val DEFAULT_ITERATIONS = 10000
private val systemAlgorithm by lazy {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
"PBKDF2withHmacSHA1"
} else {
"PBKDF2withHmacSHA256"
}
}
fun createKey(
passphraseOrPin: CharArray,
salt: ByteArray,
algorithm: String = systemAlgorithm,
iterations: Int = DEFAULT_ITERATIONS
): Pbkdf2Key {
val keySpec = PBEKeySpec(passphraseOrPin, salt, iterations, 256)
val secretKey = SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec)
return Pbkdf2Key(
secretKey.algorithm,
iterations,
salt,
secretKey.encoded
)
}
}