aboutsummaryrefslogtreecommitdiff
path: root/src/Web/ZeroBin/Utils.hs
blob: 559d9ecdf7a83bd65216671524578f92151432ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{-|
Various utility functions
-}

module Web.ZeroBin.Utils (
  toWeb
, makePassword
) where

import Crypto.Random.Entropy (getEntropy)
import Data.ByteString (ByteString)
import Data.ByteString.Base64 (encode)
import Data.ByteString.Char8 (unpack)

-- | Encodes to base64 and drops padding '='.
toWeb :: ByteString -- ^ the data to encode
      -> String     -- ^ base64 string without padding
toWeb = takeWhile (/= '=') . unpack . encode

-- | Makes a random password
makePassword :: Int       -- ^ the number of bytes of entropy
             -> IO String -- ^ random byte-string encoded by 'toWeb'
makePassword n = toWeb `fmap` getEntropy n