2 Module : Gargantext.Prelude.Crypto.Pass
4 Copyright : (c) CNRS, 2017-Present
5 License : Public Domain
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 To avoid weak password, just offer an easy way to make "good" one and
11 let user add his own entropy.
14 https://zuttobenkyou.wordpress.com/2011/12/23/simple-password-generation-with-haskell/
19 module Gargantext.Prelude.Crypto.Pass
22 -- import Data.List (nub)
23 -- import System.Environment (getArgs)
24 -- import System.IO (hSetEcho)
25 import Control.Monad.State
26 import Crypto.Random (cprgGenerate)
27 import Crypto.Random.AESCtr
28 import Data.Binary (decode)
30 import qualified Data.ByteString.Lazy as B
33 keysChar, keysNum, keysPunc, keysCharNum, keysAll, keysHex :: String
34 keysChar = ['a'..'z'] ++ ['A'..'Z']
37 keysPunc = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? "
38 keysCharNum = keysChar ++ keysNum
39 keysAll = keysChar ++ keysNum ++ keysPunc
41 giveKey :: String -> Char -> Int -> Char
42 giveKey keysCustom c n = extractChar $ case c of
43 'i' -> (keysNum ++ keysHex)
48 'h' -> (keysCharNum ++ keysCustom)
52 extractChar xs = xs!!mod n (length xs)
54 showRandomKey :: Int -> String -> StateT AESRNG IO ()
55 showRandomKey len keysCustom = handleKey =<< liftIO getChar
57 handleKey key = case key of
58 '\n' -> liftIO (putChar '\n') >> showRandomKey len keysCustom
59 'q' -> (liftIO $ putStrLn "\nBye!") >> return ()
60 _ -> mapM_ f [0..len] >> (liftIO $ putStrLn []) >> showRandomKey len keysCustom
64 . giveKey keysCustom key
65 . (\n -> mod n (length (keysAll ++ keysCustom) - 1))
68 aesRandomInt :: StateT AESRNG IO Int
71 -- aesState <- liftIO makeSystem
73 let (bs, aesState') = cprgGenerate 64 aesState
75 return (decode $ B.fromChunks [bs])
77 gargPass :: IO (Int, AESRNG)
79 -- let as = ["alphanumeric","punctuation"]
80 -- let as' = filter (\c -> elem c keysAll) . nub $ unwords as
81 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
82 --_ <- runStateT (showRandomKey len as') aesState -- enter loop
84 pass <- runStateT aesRandomInt aesState -- enter loop
90 hSetBuffering stdin NoBuffering -- disable buffering from STDIN
91 hSetBuffering stdout NoBuffering -- disable buffering from STDOUT
92 hSetEcho stdin False -- disable terminal echo
94 let as' = filter (\c -> elem c keysAll) . nub $ unwords as
100 , " 'l' alphanumeric"
102 , " 'h' alphanumeric" ++ (if null as' then [] else " + " ++ as')
108 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
109 _ <- runStateT (showRandomKey as') aesState -- enter loop