A comprehensive utility library for validations, name formatting, and secure password generation in .NET applications. Features include Turkish ID validation, minimum age validation, file extension validation, Turkish-aware name formatting, and cryptographically secure password generation.
$ dotnet add package SpectraUtilsSpectraUtils is a comprehensive, production-ready utility library for .NET 9 applications, providing robust validation, name formatting, secure password generation, and security services. Designed with performance and security as core priorities, it supports Turkish-specific features and integrates seamlessly with ASP.NET Core dependency injection.
NameCorrection(): Capitalize names correctlySirNameCorrection(): Format surnames in uppercaseNameAndSirNameCorrection(): Combined formattingRemoveSpecialCharacters(): Clean unwanted charactersInstall-Package SpectraUtilsdotnet add package SpectraUtilsusing SpectraUtils.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add SpectraUtils services
builder.Services.AddSpectraUtils();
var app = builder.Build();IPasswordHelper passwordHelper = serviceProvider.GetRequiredService<IPasswordHelper>();
// Standard 8-character password
string password = passwordHelper.CreateStardartPassword;
// Custom password
string customPassword = passwordHelper.CreatePassword(
passwordLength: 16,
includeUppercaseLetter: true,
includeLowercaseLetter: true,
includeNumber: true,
includeSpecialCharacter: true
);IPasswordHasher hasher = serviceProvider.GetRequiredService<IPasswordHasher>();
// Hash a password
string hash = hasher.Hash("UserPassword123!");
// Verify a password
bool isValid = hasher.Verify("UserPassword123!", hash);
// Format: PBKDF2$SHA256$iterations$saltBase64$hashBase64ISecureTokenGenerator tokenGenerator = serviceProvider.GetRequiredService<ISecureTokenGenerator>();
// Generate 32-byte token (default)
string token = tokenGenerator.GenerateToken();
// Generate custom-size token
string customToken = tokenGenerator.GenerateToken(byteLength: 64);IOneTimeCodeGenerator otpGenerator = serviceProvider.GetRequiredService<IOneTimeCodeGenerator>();
// Generate 6-digit OTP (default)
string otp = otpGenerator.GenerateNumericCode();
// Generate 8-digit OTP
string customOtp = otpGenerator.GenerateNumericCode(digits: 8);INameEdit nameEdit = serviceProvider.GetRequiredService<INameEdit>();
// Correct capitalization
string corrected = nameEdit.NameCorrection("mehmet"); // "Mehmet"
// Handle Turkish characters (�, �, etc.)
string turkishName = nameEdit.NameCorrection("istanbul"); // "Istanbul"
// Format multiple names
string fullName = nameEdit.NameCorrection("mehmet", "ali", "kara");
// "Mehmet Ali Kara"
// Format surname
string surname = nameEdit.SirNameCorrection("karaguzel"); // "KARAGUZEL"
// Combined name and surname
string formatted = nameEdit.NameAndSirNameCorrection("mehmet", "karaguzel");
// "Mehmet KARAGUZEL"
// Remove special characters
string cleaned = nameEdit.RemoveSpecialCharacters("Mehmet@123"); // "Mehmet123"using SpectraUtils.Attributes.ValidationAttributes;
public class UserRegistration
{
[MinimumAge(18)]
public DateTime BirthDate { get; set; }
[MinimumAge(21)]
public DateOnly BirthDateOnly { get; set; }
}public class Citizen
{
[TCIDValidation]
public string TcId { get; set; } // Example: "10000000146"
}public class Company
{
[TurkishTaxNumberValidation]
public string TaxId { get; set; } // 10-digit VKN
}public class BankAccount
{
[IbanValidation]
public string IBAN { get; set; }
[IbanValidation(expectedCountryCode: "TR")]
public string TurkishIBAN { get; set; }
}public class DocumentUpload
{
// Max 1MB, jpg and png only
[AllowedExtensions(1024 * 1024, ".jpg", ".png")]
public IFormFile? Avatar { get; set; }
// For file paths
[AllowedExtensions(1024 * 1024, ".pdf", ".doc", ".docx")]
public string? DocumentPath { get; set; }
}using SpectraUtils.Validation;
// Basic validation
bool isValid = IbanValidator.IsValid("TR33 0006 1000 0006 1001 2034 01");
// With country code
bool isTurkish = IbanValidator.IsValid(iban, expectedCountryCode: "TR");using SpectraUtils.Validation;
bool isValidTaxId = TurkishTaxNumberValidator.IsValid("1234567890");builder.Services.AddSingleton<IPasswordHasher>(
new Pbkdf2PasswordHasher(
iterations: 310_000, // OWASP 2024
saltSize: 32, // 256-bit salt
keySize: 64 // 512-bit output
)
);public interface ISpectraUtil
{
INameEdit NameEdit { get; }
IPasswordHelper PasswordHelper { get; }
IPasswordHasher PasswordHasher { get; }
ISecureTokenGenerator TokenGenerator { get; }
IOneTimeCodeGenerator OneTimeCodeGenerator { get; }
}public interface IPasswordHelper
{
string CreateStardartPassword { get; }
string CreatePassword(
int passwordLength,
bool includeUppercaseLetter = true,
bool includeLowercaseLetter = true,
bool includeSpecialCharacter = true,
bool includeNumber = true);
}public interface IPasswordHasher
{
string Hash(string password);
bool Verify(string password, string hash);
}public interface ISecureTokenGenerator
{
string GenerateToken(int byteLength = 32);
}public interface IOneTimeCodeGenerator
{
string GenerateNumericCode(int digits = 6);
}public interface INameEdit
{
string NameCorrection(string? name);
string NameCorrection(params string?[] names);
string SirNameCorrection(string? sirName);
string NameAndSirNameCorrection(string? name, string? sirName);
string RemoveSpecialCharacters(string? text);
}RandomNumberGenerator.GetBytes() - cryptographically secureCultureInfo("tr-TR")StringBuilderstackalloc// ? CORRECT: Hash before storing
string passwordHash = passwordHasher.Hash(userInput);
await database.SaveAsync(user => user.PasswordHash = passwordHash);
// ? WRONG: Never store plain passwords
user.Password = userInput;// ? Use SpectraUtils for secure tokens
string resetToken = tokenGenerator.GenerateToken(byteLength: 64);
// ? WRONG: Weak random sources
var random = new Random();
string badToken = random.Next().ToString();// ? Implement proper OTP handling:
// - Time-based expiration (5-15 minutes)
// - Rate limiting (max 3-5 attempts)
// - Account lockout after failures| Framework | Status |
|---|---|
| .NET 9.0 | ? Full Support |
| .NET 8.0 | ? Compatible |
| .NET 6.0 | ?? May work |
131+ unit tests covering:
Run tests:
dotnet testContributions are welcome! Please feel free to submit a Pull Request.
To contribute:
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)MIT License - See LICENSE file for details.
MIT License
Copyright (c) 2024 O�uzhan KARAGUZEL
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
If you discover a security vulnerability, please responsibly disclose it instead of using the issue tracker.
Made with ?? by O�uzhan KARAGUZEL