forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mouredev#6701 from iRetr0o/main
Reto mouredev#3 - Kotlin
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Retos/Reto #3 - EL GENERADOR DE CONTRASEÑAS [Media]/kotlin/iRetr0o.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |