#DartProjects
Learn the basics of Dart by building projects. Learn to build various projects step by step using Dart along the way brush up your Dart programming skills.
back to Dart projectsProject 13
Password security is a critical aspect of modern software development. In this blog post, we’re going to dive into a Dart program that checks the strength of a given password. Dart, a client-optimized language developed by Google, is commonly used in Flutter for building natively compiled applications. Let’s break down this password strength checker step by step.
The program we are discussing is written in Dart, and its primary function is to evaluate the strength of a password entered by the user. The strength is determined based on several criteria, such as length, use of uppercase and lowercase letters, inclusion of digits, and presence of special characters.
Here’s the complete code for our password strength checker:
import 'dart:io';
void main() {
print('Enter a password:');
final password = stdin.readLineSync()!;
final strength = getPasswordStrength(password);
print('Password strength: $strength');
}
String getPasswordStrength(String password) {
int score = 0;
// Add points for password length
if (password.length >= 8) {
score += 1;
}
if (password.length >= 12) {
score += 1;
}
if (password.length >= 16) {
score += 1;
}
// Add points for using a mix of uppercase and lowercase letters
if (RegExp(r'[a-z]').hasMatch(password) && RegExp(r'[A-Z]').hasMatch(password)) {
score += 1;
}
// Add points for using digits and/or symbols
if (RegExp(r'd').hasMatch(password)) {
score += 1;
}
if (RegExp(r'[!@#$%^&*(),.?":{}|<>]').hasMatch(password)) {
score += 1;
}
// Return a rating based on the total score
if (score < 2) {
return 'Weak';
} else if (score < 4) {
return 'Moderate';
} else {
return 'Strong';
}
}
Now, let’s dissect this code step by step.
void main() {
print('Enter a password:');
final password = stdin.readLineSync()!;
final strength = getPasswordStrength(password);
print('Password strength: $strength');
}
void main()
: The entry point of any Dart program.print('Enter a password:')
: Prompts the user to enter a password.final password = stdin.readLineSync()!
: Reads the password input from the user. The exclamation mark (!
) is a null check operator, ensuring that the value read is not null
.final strength = getPasswordStrength(password)
: Calls the function getPasswordStrength
with the entered password.print('Password strength: $strength')
: Displays the evaluated strength of the password.String getPasswordStrength(String password) {
int score = 0;
// Add points for password length
if (password.length >= 8) {
score += 1;
}
if (password.length >= 12) {
score += 1;
}
if (password.length >= 16) {
score += 1;
}
// Add points for using a mix of uppercase and lowercase letters
if (RegExp(r'[a-z]').hasMatch(password) && RegExp(r'[A-Z]').hasMatch(password)) {
score += 1;
}
// Add points for using digits and/or symbols
if (RegExp(r'd').hasMatch(password)) {
score += 1;
}
if (RegExp(r'[!@#$%^&*(),.?":{}|<>]').hasMatch(password)) {
score += 1;
}
// Return a rating based on the total score
if (score < 2) {
return 'Weak';
} else if (score < 4) {
return 'Moderate';
} else {
return 'Strong';
}
}
This function takes a String
(the password) as input and returns a String
representing the password’s strength.
The strength of the password is determined by a scoring system:
Password Length:
Letter Case Mix:
Digits and Symbols:
!@#$%^&*(),.?":{}|<>
.Based on the total score:
The program uses regular expressions to check for the presence of lowercase and uppercase letters, digits, and special characters.
This Dart program offers a basic yet effective way to assess the strength of passwords. It demonstrates fundamental programming concepts in Dart, such as input/output operations, string manipulation, and the use of regular expressions. You can extend this program by adding more criteria or refining the scoring logic to enhance its accuracy.
Remember, strong passwords are crucial for securing user data and should be an integral part of any authentication system.
Enjoying? Tell your friends.
Learn the basics of Dart by building projects. Learn to build various projects step by step using Dart along the way brush up your Dart programming skills.
back to Dart projectsJoin our community on Discord to connect with fellow learners, share your progress, and get help with any questions you may have throughout the #DartProject challenge. Join now and get started on your journey to mastering Dart!