Commits

Luke Plant committed 01d5ecc

Added randomStr utility for password salt generation

Comments (0)

Files changed (1)

src/Blog/Utils.hs

 {-# LANGUAGE BangPatterns, FlexibleContexts  #-}
 module Blog.Utils where
 
+import Control.Monad (liftM)
 import Data.Char
-import Control.Monad (liftM)
+import Random (randomRs, newStdGen)
 import System.Environment(getArgs)
 import System.Posix.Time (epochTime)
 import System.Posix.Types
 -- | Return current time as a UNIX timestamp
 getTimestamp :: IO Int
 getTimestamp = liftM (floor . toRational) epochTime
+
+-- | Returns a randomly generated string of length n
+randomStr :: Int -> IO String
+randomStr n = do
+    g <- newStdGen
+    return $ take n (randomRs chars g)
+  where chars = ('a','z')