मजबूत पासवर्ड नीति लागू करने के लिए सामान्य मानदंड:
- न्यूनतम 8, अधिकतम 14 कैरेक्टर
- कम से कम एक लोअरकेस अक्षर
- कम से कम एक अपरकेस अक्षर
- कम से कम एक अंक
- कम से कम एक विशेष कैरेक्टर
- कोई व्हाइटस्पेस नहीं
व्यक्तिगत जांच
// लंबाई जांच
if (password.Length < 8 || password.Length > 14) return false;
// कम से कम एक लोअरकेस
if (!password.Any(char.IsLower)) return false;
// कम से कम एक अपरकेस
if (!password.Any(char.IsUpper)) return false;
// कम से कम एक अंक
if (!password.Any(char.IsDigit)) return false;
// कोई व्हाइटस्पेस नहीं
if (password.Contains(" ")) return false;
// कम से कम एक विशेष कैरेक्टर
string specialChars = @"%!@#$%^&*()?/>.<,:;'\|}]{[_~`+=-""";
if (!password.Any(c => specialChars.Contains(c))) return false;
पूर्ण वैलिडेशन मेथड
using System;
using System.Linq;
public static class PasswordValidator {
private static readonly string SpecialChars = @"%!@#$%^&*()?/>.<,:;'\|}]{[_~`+=-""";
public static (bool IsValid, string ErrorMessage) Validate(string password) {
if (string.IsNullOrEmpty(password))
return (false, "Password cannot be empty");
if (password.Length < 8)
return (false, "Password must be at least 8 characters");
if (password.Length > 14)
return (false, "Password must not exceed 14 characters");
if (password.Any(char.IsWhiteSpace))
return (false, "Password cannot contain whitespace");
if (!password.Any(char.IsLower))
return (false, "Password must contain at least one lowercase letter");
if (!password.Any(char.IsUpper))
return (false, "Password must contain at least one uppercase letter");
if (!password.Any(char.IsDigit))
return (false, "Password must contain at least one digit");
if (!password.Any(c => SpecialChars.Contains(c)))
return (false, "Password must contain at least one special character");
return (true, "Password is valid");
}
}
// उपयोग
var (valid, message) = PasswordValidator.Validate("MyPass123!");
Console.WriteLine($"{valid}: {message}");
Regex के साथ (कॉम्पैक्ट)
using System.Text.RegularExpressions;
public static bool IsValidPassword(string password) {
// (?=.*[a-z]) - कम से कम एक लोअरकेस
// (?=.*[A-Z]) - कम से कम एक अपरकेस
// (?=.*\d) - कम से कम एक अंक
// (?=.*[!@#$%^&*]) - कम से कम एक विशेष कैरेक्टर
// .{8,14} - 8-14 कैरेक्टर लंबाई
var pattern = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,14}$";
return Regex.IsMatch(password, pattern);
}
पासवर्ड स्ट्रेंथ स्कोरिंग
public static int GetPasswordStrength(string password) {
int score = 0;
if (password.Length >= 8) score++;
if (password.Length >= 12) score++;
if (password.Any(char.IsLower)) score++;
if (password.Any(char.IsUpper)) score++;
if (password.Any(char.IsDigit)) score++;
if (password.Any(c => SpecialChars.Contains(c))) score++;
// एंट्रॉपी, सामान्य पैटर्न जांच आदि जोड़ सकते हैं
return score; // 0-6
}
सर्वोत्तम अभ्यास
- कभी भी पासवर्ड को प्लेन टेक्स्ट में स्टोर न करें -
BCrypt,Argon2,PBKDF2से हैश करें। - पासवर्ड मैनेजर के साथ संगतता के लिए बहुत सख्त नियमों से बचें।
- NIST दिशानिर्देश: लंबाई जटिलता से अधिक महत्वपूर्ण - न्यूनतम 8, अधिकतम 64+।
- ब्रीच्ड पासवर्ड सूचियों (HaveIBeenPwned API) के विरुद्ध जांचें।