Debian Codegraph @

Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs

raw
1 -- ASCIIArmor/Encode.hs: OpenPGP (RFC4880) ASCII armor implementation
2 -- Copyright © 2012-2018 Clint Adams
3 -- This software is released under the terms of the Expat license.
4 -- (See the LICENSE file).
5
6 module Codec.Encryption.OpenPGP.ASCIIArmor.Encode (
7 encode
8 , encodeLazy
9 ) where
10
11 import Codec.Encryption.OpenPGP.ASCIIArmor.Types
12 import Data.ByteString.Lazy (ByteString)
13 import qualified Data.ByteString as B
14 import qualified Data.ByteString.Lazy as BL
15 import qualified Data.ByteString.Lazy.Char8 as BLC8
16 import qualified Data.ByteString.Base64 as Base64
17 import Data.Digest.CRC24 (crc24Lazy)
18 import Data.Binary.Put (runPut, putWord32be)
19
20 encode :: [Armor] -> B.ByteString
21 encode = B.concat . BL.toChunks . encodeLazy
22
23 encodeLazy :: [Armor] -> ByteString
24 encodeLazy = BL.concat . map armor
25
26 armor :: Armor -> ByteString
27 armor (Armor atype ahs bs) = beginLine atype `BL.append` armorHeaders ahs `BL.append` blankLine `BL.append` armorData bs `BL.append` armorChecksum bs `BL.append` endLine atype
28 armor (ClearSigned chs ctxt csig) = BLC8.pack "-----BEGIN PGP SIGNED MESSAGE-----\n" `BL.append` armorHeaders chs `BL.append` blankLine `BL.append` dashEscape ctxt `BL.append` armor csig
29
30 blankLine :: ByteString
31 blankLine = BLC8.singleton '\n'
32
33 beginLine :: ArmorType -> ByteString
34 beginLine atype = BLC8.pack "-----BEGIN PGP " `BL.append` aType atype `BL.append` BLC8.pack "-----\n"
35
36 endLine :: ArmorType -> ByteString
37 endLine atype = BLC8.pack "-----END PGP " `BL.append` aType atype `BL.append` BLC8.pack "-----\n"
38
39 aType :: ArmorType -> ByteString
40 aType ArmorMessage = BLC8.pack "MESSAGE"
41 aType ArmorPublicKeyBlock = BLC8.pack "PUBLIC KEY BLOCK"
42 aType ArmorPrivateKeyBlock = BLC8.pack "PRIVATE KEY BLOCK"
43 aType (ArmorSplitMessage x y) = BLC8.pack $ "MESSAGE, PART " ++ show x ++ "/" ++ show y
44 aType (ArmorSplitMessageIndefinite x) = BLC8.pack $ "MESSAGE, PART " ++ show x
45 aType ArmorSignature = BLC8.pack "SIGNATURE"
46
47 armorHeaders :: [(String, String)] -> ByteString
48 armorHeaders = BLC8.unlines . map armorHeader
49 where
50 armorHeader :: (String, String) -> ByteString
51 armorHeader (k, v) = BLC8.pack k `BL.append` BLC8.pack ": " `BL.append` BLC8.pack v
52
53 armorData :: ByteString -> ByteString
54 armorData = BLC8.unlines . wordWrap 64 . BL.fromChunks . return . Base64.encode . B.concat . BL.toChunks
55
56 wordWrap :: Int -> ByteString -> [ByteString]
57 wordWrap lw bs
58 | BL.null bs = []
59 | lw < 1 || lw > 76 = wordWrap 76 bs
60 | otherwise = BL.take (fromIntegral lw) bs : wordWrap lw (BL.drop (fromIntegral lw) bs)
61
62 armorChecksum :: ByteString -> ByteString
63 armorChecksum = BLC8.cons '=' . armorData . BL.tail . runPut . putWord32be . crc24Lazy
64
65 dashEscape :: ByteString -> ByteString
66 dashEscape = BLC8.unlines . map escapeLine . BLC8.lines
67 where
68 escapeLine :: ByteString -> ByteString
69 escapeLine l
70 | BLC8.singleton '-' `BL.isPrefixOf` l = BLC8.pack "- " `BL.append` l
71 | BLC8.pack "From " `BL.isPrefixOf` l = BLC8.pack "- " `BL.append` l
72 | otherwise = l