Skip to content
DeveloperMemos

Generating a Password in Kotlin

Kotlin, Android Development, Password Generation1 min read

Passwords play a crucial role in securing user data and preventing unauthorized access. In this article, we will delve into generating passwords using Kotlin, specifically targeting Android development. We will cover various techniques and provide practical code examples that you can incorporate into your applications.

Random Password Generation

One of the simplest ways to generate a password is by utilizing random characters. Kotlin provides a range of functions and utilities to achieve this:

1import kotlin.random.Random
2
3fun generateRandomPassword(length: Int): String {
4 val charset = ('A'..'Z') + ('a'..'z') + ('0'..'9')
5 return (1..length)
6 .map { charset.random() }
7 .joinToString("")
8}

In the example above, we define a function generateRandomPassword that takes an integer parameter length specifying the desired length of the password. We create a character set containing uppercase letters, lowercase letters, and digits. By iterating from 1 to length, we select a random character from the character set and join them together to form the password.

You can call this function with a specific length to generate a random password:

1val password = generateRandomPassword(10)
2println(password) // Output: j5bWqIcR8L

Secure Password Generation

While random passwords offer a basic level of security, stronger passwords are necessary to protect sensitive information. We can achieve this by incorporating cryptographic techniques.

Kotlin provides the SecureRandom class, which is suitable for generating cryptographically secure passwords. Here's an example:

1import java.security.SecureRandom
2
3fun generateSecurePassword(length: Int): String {
4 val charset = ('A'..'Z') + ('a'..'z') + ('0'..'9') + listOf('!', '@', '#', '$', '%', '^', '&', '*', '(', ')')
5 val secureRandom = SecureRandom()
6 return (1..length)
7 .map { charset[secureRandom.nextInt(charset.size)] }
8 .joinToString("")
9}

In the code snippet above, we have enhanced our password generation function generateSecurePassword. By utilizing the SecureRandom class, we ensure that the generated passwords are more resistant to cryptographic attacks. In addition to letters and digits, we include a list of special characters to further enhance the password strength.

You can generate a secure password by calling this function as well:

1val securePassword = generateSecurePassword(12)
2println(securePassword) // Output: s4N#b92y%fFB

Customizable Password Generation

Sometimes, you may have specific requirements for your passwords, such as a minimum number of uppercase letters or a certain combination of characters. Kotlin allows us to create custom password generation functions to cater to these needs.

Here's an example of generating a customizable password with specific criteria:

1fun generateCustomPassword(length: Int, requireUppercase: Boolean, requireDigits: Boolean): String {
2 val charset = ('a'..'z') +
3 if (requireUppercase) ('A'..'Z') else emptyList() +
4 if (requireDigits) ('0'..'9') else emptyList()
5
6 val secureRandom = SecureRandom()
7 return (1..length)
8 .map { charset[secureRandom.nextInt(charset.size)] }
9 .joinToString("")
10}

In the generateCustomPassword function, we introduce two additional parameters: requireUppercase and requireDigits. These parameters allow us to specify whether the password should contain uppercase letters and digits respectively. By concatenating the character sets based on these criteria, we can generate passwords that fit our specific requirements.

Let's try generating a custom password with uppercase letters and digits:

1val customPassword = generateCustomPassword(8, requireUppercase = true, requireDigits = true)
2println(customPassword) // Output: w2Gh5JbF