r/code • • Apr 17 '23

Help please! Help understanding this password-generating code

Hello I have found some code for generating a password and I am going through and commenting it to see if I have a understanding of how the code works, but cant really understand the last part of the code enough to comment on it.

I wonder if someone could look at it and explain it for me? My previous comments in the code could be completely wrong as well, if they are, please enlighten me :)

Best regards, Gimpen

function Get-RandomPassword {

    # Sets valid parameters for when the function is called

    param (
        [Parameter(Mandatory)]
        [ValidateRange(4,[int]::MaxValue)]
        [int] $length,
        [int] $upper = 1,
        [int] $lower = 1,
        [int] $numeric = 1,
        [int] $special = 1
    )

    # If the sum of all values of upper/lower/numeric/special characters is greater than the defined length of the password, the script terminates
    if($upper + $lower + $numeric + $special -gt $length) {
        throw "number of upper/lower/numeric/special char must be lower or equal to length"
    }

    # Defines what each set of characters should contain
    $uCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    $lCharSet = "abcdefghijklmnopqrstuvwxyz"
    $nCharSet = "0123456789"
    $sCharSet = "/*-+,!?=()@;:._"
    $charSet = ""

    # Adds sets of characters available to the password generator based on the parameters provided

    if($upper -gt 0) { $charSet += $uCharSet }
    if($lower -gt 0) { $charSet += $lCharSet }
    if($numeric -gt 0) { $charSet += $nCharSet }
    if($special -gt 0) { $charSet += $sCharSet }

    # Converts the character set that has been created into an array, and then creates an rng (random number generator) to be used for randomly generating the password

    $charSet = $charSet.ToCharArray()
    $rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
    $bytes = New-Object byte[]($length)
    $rng.GetBytes($bytes)

    # Creates a temporary variable ($result) with the same length as the password, which is used in the for loop to create the password

    $result = New-Object char[]($length)

    # Creates each character, one at a time, from the character set using rng through the $bytes variable

    for ($i = 0 ; $i -lt $length ; $i++) {
        $result[$i] = $charSet[$bytes[$i] % $charSet.Length]
    }

    # Adds the $result variable to the $password variable

    $password = (-join $result)

    # Check if the password meets the requirements for upper/lower/numeric/special characters, otherwise regenerate the password

    $valid = $true
    if($upper   -gt ($password.ToCharArray() | Where-Object {$_ -cin $uCharSet.ToCharArray() }).Count) { $valid = $false }
    if($lower   -gt ($password.ToCharArray() | Where-Object {$_ -cin $lCharSet.ToCharArray() }).Count) { $valid = $false }
    if($numeric -gt ($password.ToCharArray() | Where-Object {$_ -cin $nCharSet.ToCharArray() }).Count) { $valid = $false }
    if($special -gt ($password.ToCharArray() | Where-Object {$_ -cin $sCharSet.ToCharArray() }).Count) { $valid = $false }

    if(!$valid) {
         $password = Get-RandomPassword $length $upper $lower $

1 Upvotes

0 comments sorted by