Skip to content

Instantly share code, notes, and snippets.

@BiatuAutMiahn
Created April 10, 2025 02:25
Show Gist options
  • Save BiatuAutMiahn/ef5862ea54d3b6ce570b75561a004a4a to your computer and use it in GitHub Desktop.
Save BiatuAutMiahn/ef5862ea54d3b6ce570b75561a004a4a to your computer and use it in GitHub Desktop.
Cryptographically Secure Passphrase/Password Generator
# Cryptographically Secure Passphrase/Password Generator
#
# $Type=(0: Passphrase, 1: Password)
# $PassCount=(Number of passwords to generate)
# $NumWords=(Passphrase Only, Number of words.)
# $NumChars=(Password Only, Number of characters.)
# Note: If dictionary fails to download, it will fallback to password instead of passphrase.
$($Type=0;$PassCount=1;$NumWords=3;$NumChars=16;if($Type-eq0){Write-Host "Fetching Dictionary..." -NoNewline;try{$wr=[System.Text.Encoding]::UTF8.GetString($(iwr "https://automation.corsicatech.com/wl/?id=f1NJ7KCklecRXIRnbVi0clHZKplgzdys").Content);$dict=$wr.Split([Environment]::NewLine,[StringSplitOptions]::RemoveEmptyEntries);Write-Host "Done"}catch{Write-Host "Failed, Error:`n$($_.Exception.Message)";$dict=$null}}else{$dict=$null};$pt=if($dict-eq$null){0}else{1};$wl=$(if($pt){$NumWords}else{$NumChars;$dict='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>/?~'.ToCharArray()});$(1..$PassCount|ForEach{$wi=$(1..$wl|ForEach{$w=$dict[$($r=[System.Security.Cryptography.RandomNumberGenerator]::Create();$b=[byte[]]::new(4);$r.GetBytes($b);$r.Dispose();$([System.BitConverter]::ToUInt32($b,0))%($dict.Count))];if($Type-eq0){$w[0].ToString().ToUpper()+$w.SubString(1)}else{$w}});if($pt){"$($wi-join'-')$((Get-Date).ToString("yy"))"}else{$wi-join''}}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment