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 Control.Monad.State
25 import Crypto.Random (cprgGenerate)
26 import Crypto.Random.AESCtr
27 import Data.Binary (decode)
29 import qualified Data.ByteString.Lazy as B
30 import Data.ByteString as S (ByteString, unpack)
31 import Data.ByteString.Char8 as C8 (pack)
32 import Data.Char (chr)
34 strToBS :: String -> S.ByteString
37 bsToStr :: S.ByteString -> String
38 bsToStr = map (chr . fromEnum) . S.unpack
41 keysChar, keysNum, keysPunc, keysCharNum, keysAll, keysHex :: String
42 keysChar = ['a'..'z'] ++ ['A'..'Z']
45 keysPunc = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? "
46 keysCharNum = keysChar ++ keysNum
47 keysAll = keysChar ++ keysNum ++ keysPunc
49 giveKey :: String -> Char -> Int -> Char
50 giveKey keysCustom c n = extractChar $ case c of
51 'i' -> (keysNum ++ keysHex)
56 'h' -> (keysCharNum ++ keysCustom)
60 extractChar xs = xs!!mod n (length xs)
62 showRandomKey :: Int -> String -> StateT AESRNG IO ()
63 showRandomKey len keysCustom = handleKey =<< liftIO getChar
65 handleKey key = case key of
66 '\n' -> liftIO (putChar '\n') >> showRandomKey len keysCustom
67 'q' -> (liftIO $ putStrLn "\nBye!") >> return ()
68 _ -> mapM_ f [0..len] >> (liftIO $ putStrLn []) >> showRandomKey len keysCustom
72 . giveKey keysCustom key
73 . (\n -> mod n (length (keysAll ++ keysCustom) - 1))
77 aesRandomInt :: StateT AESRNG IO Int
80 -- aesState <- liftIO makeSystem
82 let (bs, aesState') = cprgGenerate 64 aesState
84 return (decode $ B.fromChunks [bs])
86 printPass :: Int -> IO ()
88 let as = ["alphanumeric","punctuation"]
89 let as' = filter (\c -> elem c keysAll) . nub $ unwords as
90 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
91 _ <- runStateT (showRandomKey len as') aesState -- enter loop
94 gargPassMachine :: IO (Int, AESRNG)
96 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
97 pass <- runStateT aesRandomInt aesState -- enter loop
104 hSetBuffering stdin NoBuffering -- disable buffering from STDIN
105 hSetBuffering stdout NoBuffering -- disable buffering from STDOUT
106 hSetEcho stdin False -- disable terminal echo
108 let as' = filter (\c -> elem c keysAll) . nub $ unwords as
114 , " 'l' alphanumeric"
116 , " 'h' alphanumeric" ++ (if null as' then [] else " + " ++ as')
122 aesState <- makeSystem -- gather entropy from the system to use as the initial seed
123 _ <- runStateT (showRandomKey as') aesState -- enter loop