How to generate a random password?
Everyone has faced this problem in their IT carrier to identify what password they want to generate that is random.
This is one small piece of code I have written which is customizable and serves the following purpose of the requirement.
Length of the password
What characters we want to include in the password generated.
Here you go with the piece of code snippet that can help which has used only the Math.random function additionally.
/**
* This class is used to generate the password based on the user input content and the lenght of password required
*/
public class RandomPasswordGenerator{
/**
* @description getRandomPassword getthe password of the length provided by user.
*/
public String getRandomPassword(Integer strLength, String randomCharSet){
String randomPassword = '';
if(strLength == 0 || randomCharSet.length() == 0){
return 'Error : Password cannot be generated for zero length';
}
Integer rCharLength = randomCharSet.length();
//Use case 1: randomCharSet > strLength
//Use case 2: randomCharSet < strLength
//Use case 3: randomCharSet = strLength
//Generating the random password logic here
for(Integer i = 0; i < strLength; i++){
//Mod and use the character
Integer strPos = Integer.valueOf(Math.random()*rCharLength);
randomPassword += randomCharSet.substring(strPos,strPos+1);
}
return randomPassword;
}
}
Execute the class and get the result in the developer console.
RandomPasswordGenerator rb = new RandomPasswordGenerator();
System.assert(false, rb.getRandomPassword(10, 'a!mz@sug$12'));
New Password Generated
Everytime We will get new password combination
Now if you want to write some unit test then
Here's the test class written to validate few of the test cases.
@isTest(SeeAllData=false)
class RandomPasswordGeneratorTest{
//Negative test cases
@isTest
static void validateGetRandomPasswordWithZeroLength(){
//Given
String expectedError = 'Error : Password cannot be generated for zero length';
RandomPasswordGenerator rpg = new RandomPasswordGenerator();
//When
String randomPassword = rpg.getRandomPassword(0,'strasdfadf');
//Then
System.assertEquals(expectedError, randomPassword);
}
//Negative test cases
@isTest
static void validateGetRandomPasswordWithNoCharacterSet(){
//Given
String expectedError = 'Error : Password cannot be generated for zero length';
RandomPasswordGenerator rpg = new RandomPasswordGenerator();
//When
String randomPassword = rpg.getRandomPassword(10, '');
//Then
System.assertEquals(expectedError, randomPassword);
}
//Positive test cases
@isTest
static void validateGetRandomPasswordWithCharacterSet(){
//Given
String expectedError = 'Error : Password cannot be generated for zero length';
RandomPasswordGenerator rpg = new RandomPasswordGenerator();
//When
String randomPassword = rpg.getRandomPassword(10, 'afdaf');
//Then
System.assert(true, randomPassword.length() > 0);
System.assert(true, randomPassword != expectedError);
}
}
Try out and let me know if we can enhance a bit more
Peace ✌️
Thanks a lot for sharing amazing stuff with coding here with us. Well, I have also seen this platform https://www.techgeekbuzz.com/tools/password-generator where you can generate random password for you.
Thanks