Skip to content

Commit

Permalink
Merge pull request mouredev#6701 from iRetr0o/main
Browse files Browse the repository at this point in the history
Reto mouredev#3 - Kotlin
  • Loading branch information
kontroldev authored Dec 9, 2024
2 parents 592c683 + cae992e commit a663162
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
fun main() {
println(passwordGenerator(length = 4))
println(passwordGenerator(length = 8, capital = true))
println(passwordGenerator(length = 16, capital = true, number = true))
println(passwordGenerator(length = 32, capital = true, number = true, symbol = true))
}

fun passwordGenerator(length: Int = 8, capital: Boolean = false, number: Boolean = false, symbol: Boolean = false): String {
var password = ""
val asciiCodes = (97..122).toMutableList()

if (capital) asciiCodes += (65..90)
if (number) asciiCodes += (48..57)
if (symbol) asciiCodes += (33..47)

val finalLength = when {
length < 8 -> 8
length > 16 -> 16
else -> length.toByte()
}

for (i in 0..< finalLength) {
password += asciiCodes.random().toChar()
}

return password
}

0 comments on commit a663162

Please sign in to comment.