From 56358a7fe23d0813f875f0f210de2eb4c8241153 Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Thu, 4 Jul 2019 13:02:38 -0400 Subject: ASCII-armor support. New dependency: openpgp-asciiarmor. --- lib/KeyRing/BuildKeyDB.hs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'lib/KeyRing') diff --git a/lib/KeyRing/BuildKeyDB.hs b/lib/KeyRing/BuildKeyDB.hs index f5b09ca..234d2ef 100644 --- a/lib/KeyRing/BuildKeyDB.hs +++ b/lib/KeyRing/BuildKeyDB.hs @@ -17,6 +17,8 @@ import qualified Data.ByteString as S import qualified Codec.Binary.Base32 as Base32 import qualified Codec.Binary.Base64 as Base64 #endif +import qualified Codec.Encryption.OpenPGP.ASCIIArmor as ASCIIArmor +import Codec.Encryption.OpenPGP.ASCIIArmor.Types import Control.Applicative (liftA2) import Control.Arrow (first, second) import Control.Exception (catch) @@ -299,22 +301,29 @@ isring :: FileType -> Bool isring (PGPPackets {}) = True isring _ = False -readPacketsFromFile :: InputFileContext -> InputFile -> IO (PacketsCodec, Message) -readPacketsFromFile ctx fname = do - -- warn $ fname ++ ": reading..." - input <- readInputFileL ctx fname - return $ (,) BinaryPackets $ +decodePacketList :: L.ByteString -> [Packet] +decodePacketList some = #if MIN_VERSION_binary(0,7,0) - Message $ flip fix input $ \again some -> case decodeOrFail some of - Right (more,_,msg ) -> msg : again more - Left (_,_,_) -> - -- TODO: try ascii armor - [] + Right (more,_,msg ) -> msg : decodePacketList more + Left (_,_,_) -> [] #else - decode input + either (const []) (\(Message xs) -> xs) $ decode input + +decodeOrFail bs = Right (L.empty,1,decode bs) #endif + +readPacketsFromFile :: InputFileContext -> InputFile -> IO (PacketsCodec, Message) +readPacketsFromFile ctx fname = do + -- warn $ fname ++ ": reading..." + input <- readInputFileL ctx fname + return $ case decodeOrFail input of + Right (more,_,pkt) -> (,) BinaryPackets $ Message $ pkt : decodePacketList more + Left (_,_,_) -> case ASCIIArmor.decodeLazy input of + Right (Armor pubOrSec headers bs:_) -> (,) AsciiArmor $ Message $ decodePacketList bs + Left errmsg -> (,) DetectAscii $ Message [] + readPacketsFromWallet :: Maybe Packet -> InputFile -- cgit v1.2.3