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 Data.Text (Text)
26 import Control.Monad.State
27 import Crypto.Random (cprgGenerate)
28 import Crypto.Random.AESCtr
29 import Data.Binary (decode)
31 import qualified Data.ByteString.Lazy as B
32 import Gargantext.Prelude (cs)
33 import Data.ByteString as S (ByteString, unpack)
34 import Data.ByteString.Char8 as C8 (pack)
35 import Data.Char (chr)
37 strToBS :: String -> S.ByteString
40 bsToStr :: S.ByteString -> String
41 bsToStr = map (chr . fromEnum) . S.unpack
44 keysChar, keysNum, keysPunc, keysCharNum, keysAll, keysHex :: String
45 keysChar = ['a'..'z'] ++ ['A'..'Z']
48 keysPunc = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? "
49 keysCharNum = keysChar ++ keysNum
50 keysAll = keysChar ++ keysNum ++ keysPunc
52 giveKey :: String -> Char -> Int -> Char
53 giveKey keysCustom c n = extractChar $ case c of
54 'i' -> (keysNum ++ keysHex)
59 'h' -> (keysCharNum ++ keysCustom)
63 extractChar xs = xs!!mod n (length xs)
65 showRandomKey :: Int -> String -> StateT AESRNG IO ()
66 showRandomKey len keysCustom = handleKey =<< liftIO getChar
68 handleKey key = case key of
69 '\n' -> liftIO (putChar '\n') >> showRandomKey len keysCustom
70 'q' -> (liftIO $ putStrLn "\nBye!") >> return ()
71 _ -> mapM_ f [0..len] >> (liftIO $ putStrLn []) >> showRandomKey len keysCustom
75 . giveKey keysCustom key
76 . (\n -> mod n (length (keysAll ++ keysCustom) - 1))
80 aesRandomInt :: StateT AESRNG IO Int
83 -- aesState <- liftIO makeSystem
85 let (bs, aesState') = cprgGenerate 64 aesState
87 return (decode $ B.fromChunks [bs])
89 printPass :: Int -> IO ()
91 let as = ["alphanumeric","punctuation"]
92 let as' = filter (\c -> elem c keysAll) . nub $ unwords as
93 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
94 _ <- runStateT (showRandomKey len as') aesState -- enter loop
97 gargPass :: IO (Int, AESRNG)
99 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
100 pass <- runStateT aesRandomInt aesState -- enter loop
105 aesState <- makeSystem
106 let (bs, _aesState') = cprgGenerate 15 aesState
107 return (cs $ bsToStr bs)
112 hSetBuffering stdin NoBuffering -- disable buffering from STDIN
113 hSetBuffering stdout NoBuffering -- disable buffering from STDOUT
114 hSetEcho stdin False -- disable terminal echo
116 let as' = filter (\c -> elem c keysAll) . nub $ unwords as
122 , " 'l' alphanumeric"
124 , " 'h' alphanumeric" ++ (if null as' then [] else " + " ++ as')
130 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
131 _ <- runStateT (showRandomKey as') aesState -- enter loop