{"id":23,"date":"2016-03-18T20:04:35","date_gmt":"2016-03-19T03:04:35","guid":{"rendered":"http:\/\/www.timmevans.net\/blog\/?p=23"},"modified":"2016-03-18T20:04:35","modified_gmt":"2016-03-19T03:04:35","slug":"generating-diceware-passphrases-with-powershell","status":"publish","type":"post","link":"https:\/\/www.timmevans.net\/blog\/generating-diceware-passphrases-with-powershell\/","title":{"rendered":"Generating Diceware Passphrases with Powershell"},"content":{"rendered":"<p>Diceware is a method to create a passphrase by rolling dice to select random words from a word list. Diceware passphrases are easy to remember, but hard to crack. Micah Lee has a good article <a href=\"https:\/\/theintercept.com\/2015\/03\/26\/passphrases-can-memorize-attackers-cant-guess\/\" target=\"_blank\">here <\/a>that explains all the details about what they are and how to use them. The Diceware <a href=\"http:\/\/world.std.com\/~reinhold\/diceware.html\" target=\"_blank\">home page<\/a> has more technical details on how the system works and has several word lists available for download. The basic idea is that you roll 5 dice to get a 5 digit number and then look that number up in the word list to select each word. Repeat until your passphrase has as many words as you want.<\/p>\n<p>To truly get the benefit of a Diceware passphrase, you need to roll physical dice to get true randomness. However, spend a little time with Google and you will see that there are many computer implementations that generate Diceware passphrases. I decided to write one in Powershell.<\/p>\n<p>The first thing I wanted to do was to come up with a better random number generator than Get-Random. See my blog post <a href=\"http:\/\/www.timmevans.net\/blog\/?p=5\">here <\/a>for a discussion on how I came up with Get-RandomInt.<\/p>\n<p>Once I had a decent random number generator, the biggest question I had was on how to store the word list. I decided to use a Powershell hash table. A Powershell hash table is similar to an array, but consists of multiple key-value pairs. Rather than looking up a value by indexing on the element number like you would in an array, you look up values in a has table by indexing on the key. So <strong>$hash[$key]<\/strong> will return the value associated with <strong>$key<\/strong>. I set up the word list with the words as values and the dice rolls as keys. So it is very easy to look up a word like this: $<strong>word = $wordlist[$roll]<\/strong>.<\/p>\n<p>I added another feature. Because the words in the word list vary in length from 1 to 6 characters, it is possible that, even with a 7 or 8 word passphrase (which should be very secure), the passphrase generated could be as short as 7 or 8 characters. To prevent this problem, I provided an option to specify a minimum length for the passphrase.<\/p>\n<pre style=\"padding-left: 30px;\">function New-DicewarePassword {\r\n[CmdletBinding()]\r\n[OutputType([String])]\r\nParam (\r\n   # Word list to use\r\n   [Parameter(Position = 0)]\r\n   [ValidateNotNullOrEmpty()]\r\n   [String]$WordList = '.\\diceware.wordlist.asc',\r\n\r\n   # number of words in the passphrase\r\n   [ValidateRange(3,999)]\r\n   [int]$NumWords = 5,\r\n\r\n   # minimum length of the passphrase to generate\r\n   [Int]$MinLength = 15\r\n)\r\n\r\n   #read the words file into a hashtable\r\n   if (Test-Path $WordList) {\r\n      $WordsList = @{}\r\n      Get-Content $WordList |\r\n      Where-Object -FilterScript {\r\n         $_ -match '^\\d.*\\s.*'\r\n      } |\r\n      ForEach-Object -Process {\r\n         $key, $value = -split $_\r\n         $WordsList[$key] = $value\r\n      }\r\n   } else {\r\n      Write-Warning -Message \"File $WordList not found\"\r\n     Break\r\n   }\r\n   #initialize other variables\r\n   $NumDice = 5 #Diceware word list is based on rolling 5 dice\r\n\r\n   #main loop starts here\r\n   Do {\r\n      $PassPhrase = '' #start with a new passphrase each attempt\r\n      for ($Words = 1;$Words -le $NumWords; $Words++) {\r\n         $RollResult = ''\r\n         for ($RollNum = 1;$RollNum -le $NumDice; $RollNum++) {\r\n            $RollResult = $RollResult += [string](Get-RandomInt -min 1 -max 6)\r\n         }\r\n         $PassPhrase += $WordsList[$RollResult]+' '\r\n      }\r\n   } Until ($PassPhrase.trim().Length -ge $MinLength)\r\n   $PassPhrase, '(Length: {0} chars)' -f $PassPhrase.trim().Length\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Diceware is a method to create a passphrase by rolling dice to select random words from a word list. Diceware passphrases are easy to remember, but hard to crack. Micah Lee has a good article here that explains all the details about what they are and how to use them. The Diceware home page has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":3,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}