From 7936cc646d8d34003066ce9dc66789cfe2a08d67 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 16 Sep 2013 13:22:43 -0400 Subject: [PATCH] gpg secret key generation --- Utility/Gpg.hs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Utility/Gpg.hs b/Utility/Gpg.hs index ba391ef2a1..594cc562d7 100644 --- a/Utility/Gpg.hs +++ b/Utility/Gpg.hs @@ -168,10 +168,37 @@ secretKeys = M.fromList . parse . lines <$> readStrict params extract c k (_:rest) = extract c k rest +type Passphrase = String +type Size = Int +data KeyType = Algo Int | DSA | RSA + +{- Generates a secret key using the experimental batch mode. + - The key is added to the secret key ring. + - Can take a very long time, depending on system entropy levels. + -} +genSecretKey :: KeyType -> Passphrase -> UserId -> Size -> IO () +genSecretKey keytype passphrase userid keysize = + withHandle StdinHandle createProcessSuccess (proc gpgcmd params) feeder + where + params = ["--batch", "--gen-key"] + feeder h = do + hPutStr h $ unlines + [ "Key-Type: " ++ + case keytype of + DSA -> "DSA" + RSA -> "RSA" + Algo n -> show n + , "Key-Length: " ++ show keysize + , "Name-Real: " ++ userid + , "Expire-Date: 0" + , "Passphrase: " ++ passphrase + ] + hClose h + {- Creates a block of high-quality random data suitable to use as a cipher. - It is armored, to avoid newlines, since gpg only reads ciphers up to the - first newline. -} -genRandom :: Bool -> Int -> IO String +genRandom :: Bool -> Size -> IO String genRandom highQuality size = checksize <$> readStrict [ Params params , Param $ show randomquality