From 0cd3f3346319fc4bafa55a5c593a42ae6675eb48 Mon Sep 17 00:00:00 2001 From: joe Date: Sun, 20 Apr 2014 14:13:33 -0400 Subject: minor refactor --- KeyRing.hs | 78 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/KeyRing.hs b/KeyRing.hs index 49ddec4..8571482 100644 --- a/KeyRing.hs +++ b/KeyRing.hs @@ -1177,33 +1177,46 @@ writePEMKeys doDecrypt db exports = do try pun $ \pun -> do return $ KikiSuccess (fname,pun) -doDecrypt :: IORef (Map.Map KeyKey Packet) - -> Map.Map FilePath (IO S.ByteString) - -> MappedPacket - -> IO (KikiCondition Packet) -doDecrypt unkeysRef pws mp = do - unkeys <- readIORef unkeysRef - let wk = packet mp - kk = keykey wk - fs = Map.keys $ locations mp - - decryptIt [] = return BadPassphrase - decryptIt (getpw:getpws) = do - pw <- getpw - let wkun = maybe wk id $ decryptSecretKey pw wk - case symmetric_algorithm wkun of - Unencrypted -> do - writeIORef unkeysRef (Map.insert kk wkun unkeys) - return $ KikiSuccess wkun - _ -> decryptIt getpws - - getpws = mapMaybe (flip Map.lookup pws) fs - - case symmetric_algorithm wk of - Unencrypted -> return (KikiSuccess wk) - _ -> maybe (decryptIt getpws) - (return . KikiSuccess) - $ Map.lookup kk unkeys +makeMemoizingDecrypter :: KeyRingOperation -> FilePath -> FilePath + -> IO (MappedPacket -> IO (KikiCondition Packet)) +makeMemoizingDecrypter operation secring pubring = do + pws <- + -- TODO: head will throw an exception if a File Descriptor operation + -- file is present. We probably should change OriginMap to use InputFile + -- instead of FilePath. + Traversable.mapM (cachedContents secring pubring . fromJust . pwfile . snd) + (Map.mapKeys (head . resolveInputFile secring pubring) + $ Map.filter (isJust . pwfile . snd) $ kFiles operation) + unkeysRef <- newIORef Map.empty + return $ doDecrypt unkeysRef pws + where + doDecrypt :: IORef (Map.Map KeyKey Packet) + -> Map.Map FilePath (IO S.ByteString) + -> MappedPacket + -> IO (KikiCondition Packet) + doDecrypt unkeysRef pws mp = do + unkeys <- readIORef unkeysRef + let wk = packet mp + kk = keykey wk + fs = Map.keys $ locations mp + + decryptIt [] = return BadPassphrase + decryptIt (getpw:getpws) = do + pw <- getpw + let wkun = maybe wk id $ decryptSecretKey pw wk + case symmetric_algorithm wkun of + Unencrypted -> do + writeIORef unkeysRef (Map.insert kk wkun unkeys) + return $ KikiSuccess wkun + _ -> decryptIt getpws + + getpws = mapMaybe (flip Map.lookup pws) fs + + case symmetric_algorithm wk of + Unencrypted -> return (KikiSuccess wk) + _ -> maybe (decryptIt getpws) + (return . KikiSuccess) + $ Map.lookup kk unkeys performManipulations :: (MappedPacket -> IO (KikiCondition Packet)) @@ -1287,16 +1300,7 @@ runKeyRing operation = do else do -- memoizing decrypter - decrypt <- do - pws <- - -- TODO: head will throw an exception if a File Descriptor operation - -- file is present. We probably should change OriginMap to use InputFile - -- instead of FilePath. - Traversable.mapM (cachedContents secring pubring . fromJust . pwfile . snd) - (Map.mapKeys (head . resolveInputFile secring pubring) - $ Map.filter (isJust . pwfile . snd) $ kFiles operation) - unkeysRef <- newIORef Map.empty - return $ doDecrypt unkeysRef pws + decrypt <- makeMemoizingDecrypter operation secring pubring -- merge all keyrings, PEM files, and wallets bresult <- buildKeyDB decrypt secring pubring grip0 operation -- cgit v1.2.3