summaryrefslogtreecommitdiffhomepage
path: root/SecretKey.hs
blob: 45d9680e6d6b99b5981692dbb9d704a47bd97f56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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