Skip to content

Commit

Permalink
Code simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
SAUL committed Jan 10, 2025
1 parent 4c50b6b commit 594702a
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions src/main/kotlin/core/security/MasterPasswordManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,31 @@ import javax.crypto.spec.SecretKeySpec

object MasterPasswordManager {

/**
* Converts a string to a SecureString equivalent in Kotlin (emulated).
* Kotlin doesn't have a `SecureString`, so use char arrays for sensitive data.
*/
fun convertToSecureString(value: String): CharArray {
return value.toCharArray()
}
fun convertToSecureString(value: String): CharArray = value.toCharArray()

/**
* Converts a char array back to a regular string.
*/
fun convertToUnsecureString(secureString: CharArray): String {
return String(secureString)
}
fun convertToUnsecureString(secureString: CharArray): String = String(secureString)

/**
* Encrypts a string using AES (Advanced Encryption Standard) algorithm.
*/
fun encryptString(plainText: String, key: ByteArray): String {
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
val iv = ByteArray(cipher.blockSize)
SecureRandom().nextBytes(iv)
val ivSpec = IvParameterSpec(iv)
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"), ivSpec)

val iv = ByteArray(cipher.blockSize).apply { SecureRandom().nextBytes(this) }
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"), IvParameterSpec(iv))
val encrypted = cipher.doFinal(plainText.toByteArray(StandardCharsets.UTF_8))
val combined = iv + encrypted

return Base64.getEncoder().encodeToString(combined)
return Base64.getEncoder().encodeToString(iv + encrypted)
}

/**
* Decrypts a string that was encrypted using AES (Advanced Encryption Standard) algorithm.
*/
fun decryptString(encryptedText: String, key: ByteArray): String {
val combined = Base64.getDecoder().decode(encryptedText)
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
val iv = combined.copyOf(cipher.blockSize)
val encrypted = combined.copyOfRange(cipher.blockSize, combined.size)
val ivSpec = IvParameterSpec(iv)

cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(key, "AES"), ivSpec)
val decrypted = cipher.doFinal(encrypted)

return String(decrypted, StandardCharsets.UTF_8)
cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(key, "AES"), IvParameterSpec(iv))
return String(cipher.doFinal(encrypted), StandardCharsets.UTF_8)
}

/**
* Derives a cryptographic key from a password and a secret key using PBKDF2 algorithm.
*/
fun getKey(secretKey: String): ByteArray {
val salt = secretKey.toByteArray(StandardCharsets.UTF_8)
val spec: KeySpec = PBEKeySpec(secretKey.toCharArray(), salt, 10000, 256)
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
return factory.generateSecret(spec).encoded
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(spec).encoded
}

}
}

0 comments on commit 594702a

Please sign in to comment.