module Data.PEM.Writer
( pemWriteLBS
, pemWriteBS
) where
import Data.PEM.Types
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as L
import Data.ByteArray.Encoding (Base(Base64), convertToBase)
pemWrite :: PEM -> L.ByteString
pemWrite pem = L.fromChunks $ ([begin,header]++section++[end])
where begin = B.concat ["-----BEGIN ", sectionName, "-----\n"]
end = B.concat ["-----END ", sectionName, "-----\n" ]
section :: [ByteString]
section = map encodeLine $ splitChunks $ pemContent pem
header :: ByteString
header = if null $ pemHeader pem
then B.empty
else B.concat ((concatMap toHeader (pemHeader pem)) ++ ["\n"])
toHeader :: (String, ByteString) -> [ByteString]
toHeader (k,v) = [ BC.pack k, ":", v, "\n" ]
sectionName = BC.pack $ pemName pem
encodeLine l = convertToBase Base64 l `B.append` "\n"
splitChunks b
| B.length b > 48 = let (x,y) = B.splitAt 48 b in x : splitChunks y
| otherwise = [b]
pemWriteBS :: PEM -> ByteString
pemWriteBS = B.concat . L.toChunks . pemWrite
pemWriteLBS :: PEM -> L.ByteString
pemWriteLBS = pemWrite