use kdf library
This commit is contained in:
@@ -1,26 +1,27 @@
|
||||
package com.infendro.account.util
|
||||
|
||||
import com.infendro.encoding.Hex
|
||||
import javax.crypto.SecretKeyFactory
|
||||
import javax.crypto.spec.PBEKeySpec
|
||||
import com.infendro.hash.SHA256
|
||||
import com.infendro.kdf.PBKDF2
|
||||
import kotlin.random.Random
|
||||
|
||||
// TODO implement PBKDF2
|
||||
object SecureHasher {
|
||||
private val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
|
||||
private val kdf = PBKDF2(SHA256)
|
||||
private val random = Random.Default
|
||||
|
||||
fun hash(
|
||||
value: String,
|
||||
salt: String,
|
||||
): String {
|
||||
val spec = PBEKeySpec(value.toCharArray(), Hex.decode(salt.toByteArray()), 100_000, 256)
|
||||
return factory.generateSecret(spec).encoded
|
||||
.let { Hex.encode(it).toString() }
|
||||
val value = value.encodeToByteArray()
|
||||
val salt = Hex.decode(salt.encodeToByteArray())
|
||||
return kdf.hash(value, salt, 100_000, 32)
|
||||
.let { Hex.encode(it).decodeToString() }
|
||||
}
|
||||
|
||||
fun generateSalt(): String {
|
||||
return ByteArray(16).also { random.nextBytes(it) }
|
||||
.let { Hex.encode(it).toString() }
|
||||
return ByteArray(16)
|
||||
.also { random.nextBytes(it) }
|
||||
.let { Hex.encode(it).decodeToString() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user