summaryrefslogtreecommitdiffhomepage
path: root/Gpg.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Gpg.hs')
-rw-r--r--Gpg.hs26
1 files changed, 22 insertions, 4 deletions
diff --git a/Gpg.hs b/Gpg.hs
index 9794395..7002e16 100644
--- a/Gpg.hs
+++ b/Gpg.hs
@@ -7,14 +7,32 @@ module Gpg where
import Types
import System.Process
+import Data.List.Split
+import Data.Maybe
+import System.Exit
+import qualified Data.ByteString as B
+import qualified Data.ByteString.UTF8 as BU8
listSecretKeys :: IO [(Name, KeyId)]
-listSecretKeys = undefined
--- gpg --batch --with-colons --list-secret-keys
--- extract from eg, sec::4096:1:C910D9222512E3C7:...
+listSecretKeys = mapMaybe parse . lines <$> readProcess "gpg"
+ ["--batch", "--with-colons", "--list-secret-keys"] ""
+ where
+ parse l = case splitOn ":" l of
+ ("sec":_:_:_:kid:_:_:_:_:n:_) -> Just
+ (Name (BU8.fromString n), KeyId (BU8.fromString kid))
+ _ -> Nothing
getSecretKey :: KeyId -> IO SecretKey
-getSecretKey = undefined
+getSecretKey (KeyId kid) = do
+ (_, Just hout, _, ph) <- createProcess (proc "gpg" ps)
+ { std_out = CreatePipe }
+ secretkey <- SecretKey <$> B.hGetContents hout
+ exitcode <- waitForProcess ph
+ case exitcode of
+ ExitSuccess -> return secretkey
+ _ -> error "gpg --export-secret-key failed"
+ where
+ ps = ["--batch", "--export-secret-key", BU8.toString kid]
-- | Check if a given gpg key is present on the keyserver.
-- (Without downloading the key.)