Last active
January 27, 2017 06:37
-
-
Save MVKozlov/5d8accbc7f010e70c0a61cbeef06e4dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Generate set of passwords | |
.DESCRIPTION | |
Generate set of passwords based on character usage rules | |
.OUTPUTS | |
Array of string | |
.PARAMETER Length | |
The password length | |
.PARAMETER Count | |
Count of returned passwords | |
.PARAMETER Charsets | |
Set of characters used for password generation | |
.EXAMPLE | |
#Generate 8-symbols password | |
New-Password | |
.EXAMPLE | |
#Generate 12-symbols password | |
New-Password 12 | |
.EXAMPLE | |
#Generate two 10-symbols passwords | |
New-Password 10 2 | |
.EXAMPLE | |
#Generate two 10-symbols passwords with only small and big letters | |
New-Password -Length 10 -Count 2 -Charsets 'b','s' | |
.LINK | |
https://gist.github.com/MVKozlov/5d8accbc7f010e70c0a61cbeef06e4dd | |
#> | |
param ( | |
[Parameter(Mandatory=$False,Position=0)] | |
[int]$Length = 8, | |
[Parameter(Mandatory=$False,Position=1)] | |
[int]$Count = 1, | |
[Parameter(Mandatory=$False,Position=2)] | |
[ValidateSet('smallLetters','s', 'bigLetters','b', 'Digits','d', 'SpecialChars','p', 'VerySpecialChars','v' )] | |
[string[]]$Charsets = @('smallLetters', 'bigLetters', 'Digits', 'SpecialChars') | |
) | |
BEGIN { | |
$AllChars = @{ | |
smallLetters = @{ | |
Probability = 8 | |
Characters = 'abcdefghijklmnopwrstuvwxyz' | |
} | |
bigLetters = @{ | |
Probability = 8 | |
Characters = 'ABCDEFGHIJKLMNOPWRSTUVWXYZ' | |
} | |
Digits = @{ | |
Probability = 8 | |
Characters = '0123456789' | |
} | |
SpecialChars = @{ | |
Probability = 4 | |
Characters = '!@#$()_+-={[}];''<,>.' | |
} | |
VerySpecialChars = @{ | |
Probability = 3 | |
Characters = '`~%^&*:"\/?|' | |
} | |
} | |
if ($Charsets -contains 's') { $Charsets += 'smallLetters' } | |
if ($Charsets -contains 'b') { $Charsets += 'bigLetters' } | |
if ($Charsets -contains 'd') { $Charsets += 'Digits' } | |
if ($Charsets -contains 'p') { $Charsets += 'SpecialChars' } | |
if ($Charsets -contains 'v') { $Charsets += 'VerySpecialChars' } | |
$AvailableSets = $AllChars[$Charsets] | |
[System.Text.StringBuilder]$CharList = '' | |
foreach ($set in $AvailableSets) { | |
foreach ($i in (1..$set.Probability)) { | |
[void]$CharList.Append($set.Characters) | |
} | |
} | |
$FullCharset = $CharList.ToString(); | |
} | |
PROCESS { | |
foreach ($c in 1..$Count) { | |
$s = '' | |
foreach ($l in 1..$Length) { | |
# Strong random algorithm | |
# $RandomBytes = [Byte[]]@(0,0,0,0) # will need 4 random bytes... | |
# $RNG = [Security.Cryptography.RNGCryptoServiceProvider]::Create() | |
# $RNG.GetBytes($RandomBytes) | |
$s += $FullCharset[(Get-Random -Maximum $FullCharset.Length)] | |
} | |
$s | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment