Skip to content
Snippets Groups Projects
Commit 250c7d62 authored by Taras's avatar Taras
Browse files

Add key length

parent 69865420
No related branches found
No related tags found
No related merge requests found
......@@ -7,21 +7,28 @@ internal object BCryptManager {
private const val iterations = 14
private const val saltLength = 16
private const val privateKeyLength = 32
fun generateBcryptPrivateKeyWithPassword(
userName: String,
password: String
): GeneratePrivateKeyResult {
val salt = userName.sha256().substring(0, saltLength).toByteArray()
val privateKey = BCrypt.withDefaults().hash(iterations, salt, password.toByteArray())
return GeneratePrivateKeyResult(privateKey, salt.toString(), iterations)
val salt = userName.sha256().substring(0, saltLength)
val privateKey = retrievePrivateKeyWithPassword(password, salt, iterations)
return GeneratePrivateKeyResult(privateKey, salt, iterations)
}
fun retrievePrivateKeyWithPassword(
password: String,
salt: String,
iterations: Int
): ByteArray = BCrypt.withDefaults().hash(iterations, salt.toByteArray(), password.toByteArray())
salt: String?,
iterations: Int?
): ByteArray =
BCrypt.withDefaults()
.hash(iterations ?: this.iterations, (salt ?: "").toByteArray(), password.toByteArray())
.takeLast(privateKeyLength)
.reversed()
.toByteArray()
private fun String.sha256(): String = MessageDigest
.getInstance("SHA-256")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment