Last active
September 17, 2019 19:22
-
-
Save root9b-zz/aa82376a327231da8b30f5057ec583e3 to your computer and use it in GitHub Desktop.
Passphrase/password generator
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
# passphrase/password generator | |
# Generates random passwords of at least 80-bit entropy using randomly-selected words | |
# selected using the system cryptographic RNG. More reason to trust than other methods because: | |
# - Verifiable good entropy source; your underlying system cryptographically secure RNG. | |
# - Verifiable level of entropy; instead of trying to be clever, which fails at a high rate, we mathematically calculate entropy. | |
# - Memorable, as it uses the most common English words | |
# - Does not give anybody else, like a website, an opportunity to record your passwords | |
if( -not (Test-Path .\randomwords.txt) ){ | |
echo "Downloading wordlist..." | |
(New-Object System.Net.WebClient).DownloadFile("http://www.mieliestronk.com/corncob_lowercase.txt","$(pwd)\randomwords.txt") | |
} | |
$targetbits = 75 # how many bits of entropy, minimum, do you need? | |
#read wordlist | |
$rl = [System.IO.File]::ReadLines("$(pwd)\randomwords.txt") | |
#put into a set to ensure no duplicates | |
$hs = New-Object System.Collections.Generic.HashSet[String] ($rl) | |
#then convert to a list so we can index it | |
$lines = New-Object System.Collections.Generic.List[String] $hs | |
$numlines = $lines.Count | |
$bitsPerWord = ([math]::Log($numlines) / [math]::Log(2)) | |
$wordsFloat = ($targetbits / $bitsPerWord) | |
$numWords = [math]::Ceiling($wordsFloat) | |
$totalbits = $numWords * $bitsPerWord | |
$rounded = [math]::Round($totalbits,2) | |
#Now get the random words | |
$out = "" | |
foreach($i in 1..$numWords){ | |
$randomBytes = new-object byte[] 4 | |
(new-object System.Security.Cryptography.RNGCryptoServiceProvider).GetBytes($randomBytes) | |
$out += $lines[[BitConverter]::ToInt32($randomBytes, 0) % $numlines] + " " | |
} | |
echo "Your generated passphrase, with $rounded bits of entropy, is:" | |
echo ' ' | |
echo $out | |
echo ' ' | |
echo "A shorter, 96-bit-entropy password:" | |
echo ' ' | |
$randomBytes = new-object byte[] 12 | |
(new-object System.Security.Cryptography.RNGCryptoServiceProvider).GetBytes($randomBytes) | |
$shortpw=[System.Convert]::ToBase64String($randomBytes) | |
echo $shortpw | |
echo ' ' | |
cmd /c pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment