< Summary

Information
Class: LOCKnet.Core.Services.PasswordGeneratorService
Assembly: LOCKnet.Core
File(s): /home/runner/work/LOCKnet/LOCKnet/src/LOCKnet.Core/Services/PasswordGeneratorService.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 60
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Generate(...)100%1414100%

File(s)

/home/runner/work/LOCKnet/LOCKnet/src/LOCKnet.Core/Services/PasswordGeneratorService.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3
 4namespace LOCKnet.Core.Services;
 5
 6/// <summary>
 7/// Standardimplementierung fuer <see cref="IPasswordGeneratorService"/>.
 8/// </summary>
 9public sealed class PasswordGeneratorService : IPasswordGeneratorService
 10{
 11  private const string UppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 12  private const string LowercaseChars = "abcdefghijklmnopqrstuvwxyz";
 13  private const string DigitChars = "0123456789";
 14  private const string SpecialChars = "!@#$%^&*()-_=+[]{}|;:,.<>?/";
 15
 16  /// <inheritdoc/>
 17  public string Generate(PasswordGeneratorOptions options)
 1318  {
 1319    ArgumentNullException.ThrowIfNull(options);
 20
 1221    var length = options.Length > 0 ? options.Length : 16;
 1222    var charsetBuilder = new StringBuilder();
 23
 1224    if (options.UseUppercase)
 925    {
 926      charsetBuilder.Append(UppercaseChars);
 927    }
 28
 1229    if (options.UseLowercase)
 930    {
 931      charsetBuilder.Append(LowercaseChars);
 932    }
 33
 1234    if (options.UseDigits)
 1035    {
 1036      charsetBuilder.Append(DigitChars);
 1037    }
 38
 1239    if (options.UseSpecial)
 1040    {
 1041      charsetBuilder.Append(SpecialChars);
 1042    }
 43
 1244    if (charsetBuilder.Length == 0)
 145    {
 146      charsetBuilder.Append(LowercaseChars);
 147    }
 48
 1249    var charset = charsetBuilder.ToString();
 1250    var randomBytes = RandomNumberGenerator.GetBytes(length);
 1251    var password = new char[length];
 52
 84253    for (var i = 0; i < length; i++)
 40954    {
 40955      password[i] = charset[randomBytes[i] % charset.Length];
 40956    }
 57
 1258    return new string(password);
 1259  }
 60}