summaryrefslogtreecommitdiffhomepage
path: root/SecretKey.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-17 15:52:08 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-17 15:52:08 -0400
commit19e3dc5541a74fe1c323c629bdf214b8690640e5 (patch)
treed4c16b9bb027887e9b7ea323997d30e00ba9ebdb /SecretKey.hs
parented8d1ed03c3520ccd4b3c775817bef122457f224 (diff)
downloadkeysafe-19e3dc5541a74fe1c323c629bdf214b8690640e5.tar.gz
save restored secret key to gpg or file
Diffstat (limited to 'SecretKey.hs')
-rw-r--r--SecretKey.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/SecretKey.hs b/SecretKey.hs
new file mode 100644
index 0000000..45d9680
--- /dev/null
+++ b/SecretKey.hs
@@ -0,0 +1,26 @@
+{- Copyright 2016 Joey Hess <id@joeyh.name>
+ -
+ - Licensed under the GNU AGPL version 3 or higher.
+ -}
+
+module SecretKey where
+
+import Types
+import qualified Gpg
+import qualified Data.ByteString as B
+import System.IO
+import System.Posix.IO
+
+getSecretKey :: SecretKeySource -> IO SecretKey
+getSecretKey (GpgKey kid) = Gpg.getSecretKey kid
+getSecretKey (KeyFile f) = SecretKey <$> B.readFile f
+
+-- | Can throw exception if the secret key already exists.
+writeSecretKey :: SecretKeySource -> SecretKey -> IO ()
+writeSecretKey (GpgKey _) secretkey = Gpg.writeSecretKey secretkey
+writeSecretKey (KeyFile f) (SecretKey b) = do
+ fd <- openFd f WriteOnly (Just 0o666)
+ (defaultFileFlags { exclusive = True } )
+ h <- fdToHandle fd
+ B.hPut h b
+ hClose h