Skip to content

Instantly share code, notes, and snippets.

@astanin
Created September 12, 2011 09:39
Show Gist options
  • Save astanin/1210932 to your computer and use it in GitHub Desktop.
Save astanin/1210932 to your computer and use it in GitHub Desktop.
A password generator inspired by XKCD #936
#!/usr/bin/env runhaskell
A password generator inspired by XKCD #936: http://xkcd.com/936/
Passwords which are easy for humans to remeber and hard for computers to guess.
> import Control.Monad (liftM, when)
> import Data.List (intercalate, isPrefixOf)
> import System.Random (getStdGen, randomRs)
> import System.Environment (getArgs)
> import System.Exit (exitWith, ExitCode(..))
> dict_file = "/usr/share/dict/words"
> default_words = "4"
> usage= "Usage: passphrase.lhs [-v|--verbose] " ++
> "[word_count (default=" ++ default_words ++ ")]"
> main = do
> args <- getArgs
> when ("-h" `elem` args || "--help" `elem` args) $ do
> putStrLn usage
> exitWith ExitSuccess
> let args' = filter (not . ("-" `isPrefixOf`)) args
> let n = read . head $ args' ++ [default_words]
> dict <- (filter (not.null) . lines) `liftM` readFile dict_file
> let dsize = length dict
> ixs <- (take n . randomRs (0, dsize-1)) `liftM` getStdGen
> putStr $ intercalate " " . map (dict!!) $ ixs
> when ("-v" `elem` args || "--verbose" `elem` args) $ do
> let bits = logBase 2 ((fromIntegral dsize)^n)
> putStrLn ""
> putStrLn $ show (round bits) ++ " bits of entropy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment