Haskell/Password generator

Aus Franky
Wechseln zu: Navigation, Suche

Do you want to make random passwords? True random means security. Your mother's birth name or the name of your first pet is not safe, a sequence of dice rolls is, when long enough.

-- takes base and number and produces base-adic list,
-- e.g. toList 10 42 = [4,2]
--      toList 2 14 = [1,1,1,0]
toList :: Integer -> Integer -> [Integer]
toList = (reverse .) . toList' where
  toList' _ 0 = []
  toList' base num = (num `mod` base) : toList' base (num `div` base) 

-- reverse of above.
fromList :: Integer -> [Integer] -> Integer
fromList base = foldl ((+) . (* base) ) 0

-- the possible characters for passwords
myChars = ['a'..'z']++['A'..'Z']++['0'..'9']++"(.?)!"

-- for mapping a number to password
toChars = (!!) myChars . fromInteger

charCount = toInteger $ length myChars

-- takes a list of dice rolls (range 1 to 6) and returns a string for password.
makePW' :: [Integer] -> String
makePW' = map toChars . toList charCount . fromList 6 . map (subtract 1)

-- takes an Integer with digits 1 to 6 and returns a string for password.
makePW :: Integer -> String
makePW = makePW' . toList 10 

You may want to change the final section of myChars, (.?)!. I did before posting.

27 dice rolls grant just over 89 bits of information. For 5 special characters, this might yield up to 12 characters. Drop the first (or the first and second) characters of your password, they are more likely to be among the early characters in myChars.