使用 Kotlin 验证密码
password validation with Kotlin
我是 Kotlin 的新手,正在尝试找到最优雅的密码验证解决方案,条件是:
- 密码必须至少包含 8 个字符。
- 它必须至少有 1 个小写字母和至少 1 个大写字母。
- 它必须有一个特殊字符,比如!或 + 或 - 或类似的
- 必须至少有一位数字
“优雅”是主观的!
这是一个实用的方法:
// you can define each rule as a separate checking function,
// adding more doesn't change the complexity
fun String.isLongEnough() = length >= 8
fun String.hasEnoughDigits() = count(Char::isDigit) > 0
fun String.isMixedCase() = any(Char::isLowerCase) && any(Char::isUpperCase)
fun String.hasSpecialChar() = any { it in "!,+^" }
// you can decide which requirements need to be included (or make separate lists
// of different priority requirements, and check that enough of each have been met)
val requirements = listOf(String::isLongEnough, String::hasEnoughDigits)
val String.meetsRequirements get() = requirements.all { check -> check(this) }
fun main() {
val password = "hiThere2!+"
println(password.meetsRequirements)
}
我认为好处是添加新规则很容易,它们可以非常简单和可读,并且您可以在单独的步骤中处理验证逻辑(例如,如果您正在实施“密码强度”指标,满足某些要求比其他要求更重要)。
我在其中使用了一些更高级的语言功能,但实际上是为了保持简洁。 String.whatever()
扩展函数只是意味着您不需要在函数中引用字符串参数(它是 this
),而函数引用 (String::hasEnoughDigits
) 让您可以这样做 requirements.all
调用而不是去 if (isLongEnough(password) && hasEnoughDigits(password) && ...)
等等。如果你愿意,你可以那样做!
有很多选择和方法来实现它。正则表达式绝对可以很优雅,但也很难使用
你可以这样做...
internal fun isValidPassword(password: String): Boolean {
if (password.length < 8) return false
if (password.filter { it.isDigit() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isUpperCase() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isLowerCase() }.firstOrNull() == null) return false
if (password.filter { !it.isLetterOrDigit() }.firstOrNull() == null) return false
return true
}
我是 Kotlin 的新手,正在尝试找到最优雅的密码验证解决方案,条件是:
- 密码必须至少包含 8 个字符。
- 它必须至少有 1 个小写字母和至少 1 个大写字母。
- 它必须有一个特殊字符,比如!或 + 或 - 或类似的
- 必须至少有一位数字
“优雅”是主观的!
这是一个实用的方法:
// you can define each rule as a separate checking function,
// adding more doesn't change the complexity
fun String.isLongEnough() = length >= 8
fun String.hasEnoughDigits() = count(Char::isDigit) > 0
fun String.isMixedCase() = any(Char::isLowerCase) && any(Char::isUpperCase)
fun String.hasSpecialChar() = any { it in "!,+^" }
// you can decide which requirements need to be included (or make separate lists
// of different priority requirements, and check that enough of each have been met)
val requirements = listOf(String::isLongEnough, String::hasEnoughDigits)
val String.meetsRequirements get() = requirements.all { check -> check(this) }
fun main() {
val password = "hiThere2!+"
println(password.meetsRequirements)
}
我认为好处是添加新规则很容易,它们可以非常简单和可读,并且您可以在单独的步骤中处理验证逻辑(例如,如果您正在实施“密码强度”指标,满足某些要求比其他要求更重要)。
我在其中使用了一些更高级的语言功能,但实际上是为了保持简洁。 String.whatever()
扩展函数只是意味着您不需要在函数中引用字符串参数(它是 this
),而函数引用 (String::hasEnoughDigits
) 让您可以这样做 requirements.all
调用而不是去 if (isLongEnough(password) && hasEnoughDigits(password) && ...)
等等。如果你愿意,你可以那样做!
有很多选择和方法来实现它。正则表达式绝对可以很优雅,但也很难使用
你可以这样做...
internal fun isValidPassword(password: String): Boolean {
if (password.length < 8) return false
if (password.filter { it.isDigit() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isUpperCase() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isLowerCase() }.firstOrNull() == null) return false
if (password.filter { !it.isLetterOrDigit() }.firstOrNull() == null) return false
return true
}