Created
September 13, 2021 21:51
-
-
Save joegasper/f81c5d2c53ece4dccc20539c3a37b89b to your computer and use it in GitHub Desktop.
Generate a password/passphrase based on a dice password list - only PowerShell Core/7 - added support for parallel foreach
This file contains hidden or 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 | |
Returns a passphrase | |
.DESCRIPTION | |
Returns a passphrase based on a dice password list. | |
.EXAMPLE | |
New-DicePassPhrase | |
miles welt eta pall 41st | |
Returns a single passphrase with a minimum of 23 characters. | |
.EXAMPLE | |
New-DicePassPhrase -MinChars 30 | |
edna 300 kigali 1812 virgil gild | |
Returns a passphrase with a minimum of 30 characters. | |
.EXAMPLE | |
New-DicePassPhrase -Quantity 4 | |
olsen ferric plasm ben hut | |
pang jukes argive risky | |
grew gouda breve spout novo | |
polk iowa golly cape rh | |
Returns 4 passphrases. | |
.EXAMPLE | |
New-DicePassPhrase -Complex | |
July@Iron#Rush-Toni6;Align | |
Returns a complex passphrase - at least one lower, upper, special, number | |
.INPUTS | |
MinChars (integer): Minimum number of characters in the passphrase. | |
Quantity (integer): Number of passphrases to return. | |
Complex (switch): Returns a complex passphrase (lower, upper, special, number) | |
.NOTES | |
Author: [email protected] | |
Inspired to create by: | |
https://theworld.com/~reinhold/diceware.html | |
https://www.eff.org/dice | |
#> | |
function New-DicePassPhrase { | |
[CmdletBinding()] | |
param ( | |
[int]$MinChars = 23, | |
[int]$Quantity = 1, | |
[switch]$Complex | |
) | |
begin { | |
#$DiceLink = 'http://world.std.com/~reinhold/diceware.wordlist.asc' | |
$DiceLink = 'https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt' | |
$DiceFile = $DiceLink.Split('/')[-1] | |
if ( ! (Test-Path "$env:TEMP\$DiceFile")) { | |
Invoke-WebRequest -Uri $DiceLink -OutFile "$env:TEMP\$DiceFile" | |
} | |
$DiceWareWordList = (Get-Content "$env:TEMP\$DiceFile") -split "`n" | Select-String -Pattern '\d{5}\s' | |
$Delim = ' ' | |
$Chars = (32..47) + (58..64) + (91..95) + (123..126) | |
} | |
process { | |
1..$Quantity | ForEach-Object -ThrottleLimit 10 -Parallel { | |
$MyDicePwd = '' | |
do { | |
[string]$Roll = "{0}{1}{2}{3}{4}" -f (Get-Random -Minimum 1 -Maximum 7), | |
(Get-Random -Minimum 1 -Maximum 7), (Get-Random -Minimum 1 -Maximum 7), | |
(Get-Random -Minimum 1 -Maximum 7), (Get-Random -Minimum 1 -Maximum 7), | |
(Get-Random -Minimum 1 -Maximum 7) | |
$MyDicePwd += ($using:DiceWareWordList | Select-String -Pattern $Roll).Line.Split("`t")[1].Trim() + $using:Delim | |
} until ($MyDicePwd.Trim().Length -ge $using:MinChars) | |
$MyDicePwd = $MyDicePwd.Trim() | |
if ($Complex) { | |
$textInfo = (Get-Culture).TextInfo | |
$MyDicePwd = $textInfo.ToTitleCase($MyDicePwd) | |
$NumSpaces = $MyDicePwd.Split(' ').Count - 1 | |
for ($j = 0; $j -lt $NumSpaces; $j++) { | |
$MyDicePwd = ([Regex]' ').Replace($MyDicePwd, ([char]($Chars | Get-Random -Count 1)).ToString(), 1) | |
} | |
$MyPwdLen = $MyDicePwd.Length | |
$Rand = Get-Random -Minimum 1 -Maximum $MyPwdLen | |
$MyDicePwd = $MyDicePwd.Remove($Rand, 1).Insert($Rand, (Get-Random -Minimum 0 -Maximum 10)) | |
} | |
$MyDicePwd | |
} | |
} | |
end { | |
$MyDicePwd = '' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment