aboutsummaryrefslogtreecommitdiff
path: root/Utility/Gpg.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-09-16 13:22:43 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-09-16 13:22:43 -0400
commit7655974b46696b89b8c68494f663168d2b606d5b (patch)
tree36c09218acc85703f1b42da92917613a6aea6b78 /Utility/Gpg.hs
parent1ffd9ec9830df8200cc0e0baad11dcf86387e508 (diff)
gpg secret key generation
Diffstat (limited to 'Utility/Gpg.hs')
-rw-r--r--Utility/Gpg.hs29
1 files changed, 28 insertions, 1 deletions
diff --git a/Utility/Gpg.hs b/Utility/Gpg.hs
index ba391ef2a..594cc562d 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