{- Copyright 2016 Joey Hess - - Licensed under the GNU AGPL version 3 or higher. -} module SecretKey where import Types import Share import qualified Gpg import qualified Data.ByteString as B import System.IO import System.Posix.IO getSecretKey :: SecretKeySource -> IO (SecretKeySource, SecretKey) getSecretKey sks = do sk <- case sks of GpgKey kid -> Gpg.getSecretKey kid KeyFile f -> SecretKey <$> B.readFile f return (sks, sk) -- | Can throw exception if the secret key already exists. writeSecretKey :: Distinguisher -> SecretKey -> IO () writeSecretKey (Distinguisher (GpgKey _)) secretkey = Gpg.writeSecretKey secretkey writeSecretKey AnyGpgKey secretkey = Gpg.writeSecretKey secretkey writeSecretKey (Distinguisher (KeyFile f)) (SecretKey b) = do fd <- openFd f WriteOnly (Just 0o666) (defaultFileFlags { exclusive = True } ) h <- fdToHandle fd B.hPut h b hClose h