2 Module : Gargantext.Prelude.Crypto.Pass.Machine
4 Copyright : (c) CNRS, 2017-Present
5 License : Public Domain
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Random Text generator (for machines mainly)
13 https://zuttobenkyou.wordpress.com/2011/12/23/simple-password-generation-with-haskell/
18 module Gargantext.Prelude.Crypto.Pass.Machine
21 import Data.List (nub)
22 -- import System.Environment (getArgs)
23 -- import System.IO (hSetEcho)
24 import Data.Text (Text)
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
31 import Gargantext.Prelude (cs)
32 import Data.ByteString as S (ByteString, unpack)
33 import Data.ByteString.Char8 as C8 (pack)
34 import Data.Char (chr)
36 strToBS :: String -> S.ByteString
39 bsToStr :: S.ByteString -> String
40 bsToStr = map (chr . fromEnum) . S.unpack
43 keysChar, keysNum, keysPunc, keysCharNum, keysAll, keysHex :: String
44 keysChar = ['a'..'z'] ++ ['A'..'Z']
47 keysPunc = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? "
48 keysCharNum = keysChar ++ keysNum
49 keysAll = keysChar ++ keysNum ++ keysPunc
51 giveKey :: String -> Char -> Int -> Char
52 giveKey keysCustom c n = extractChar $ case c of
53 'i' -> (keysNum ++ keysHex)
58 'h' -> (keysCharNum ++ keysCustom)
62 extractChar xs = xs!!mod n (length xs)
64 showRandomKey :: Int -> String -> StateT AESRNG IO ()
65 showRandomKey len keysCustom = handleKey =<< liftIO getChar
67 handleKey key = case key of
68 '\n' -> liftIO (putChar '\n') >> showRandomKey len keysCustom
69 'q' -> (liftIO $ putStrLn "\nBye!") >> return ()
70 _ -> mapM_ f [0..len] >> (liftIO $ putStrLn []) >> showRandomKey len keysCustom
74 . giveKey keysCustom key
75 . (\n -> mod n (length (keysAll ++ keysCustom) - 1))
79 aesRandomInt :: StateT AESRNG IO Int
82 -- aesState <- liftIO makeSystem
84 let (bs, aesState') = cprgGenerate 64 aesState
86 return (decode $ B.fromChunks [bs])
88 printPass :: Int -> IO ()
90 let as = ["alphanumeric","punctuation"]
91 let as' = filter (\c -> elem c keysAll) . nub $ unwords as
92 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
93 _ <- runStateT (showRandomKey len as') aesState -- enter loop
96 gargPassMachine :: IO (Int, AESRNG)
98 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
99 pass <- runStateT aesRandomInt aesState -- enter loop
106 hSetBuffering stdin NoBuffering -- disable buffering from STDIN
107 hSetBuffering stdout NoBuffering -- disable buffering from STDOUT
108 hSetEcho stdin False -- disable terminal echo
110 let as' = filter (\c -> elem c keysAll) . nub $ unwords as
116 , " 'l' alphanumeric"
118 , " 'h' alphanumeric" ++ (if null as' then [] else " + " ++ as')
124 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
125 _ <- runStateT (showRandomKey as') aesState -- enter loop