From 595e1bec3dda5f04d58a4c94d1f3c6da0e256061 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 2 Sep 2016 01:01:34 -0400 Subject: randomS2K utility. --- Data/OpenPGP/Util.hs | 1 + Data/OpenPGP/Util/DecryptSecretKey.hs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/Data/OpenPGP/Util.hs b/Data/OpenPGP/Util.hs index 19d98ec..6b1ebb1 100644 --- a/Data/OpenPGP/Util.hs +++ b/Data/OpenPGP/Util.hs @@ -6,6 +6,7 @@ module Data.OpenPGP.Util , pgpSign , GenerateKeyParams(..) , generateKey + , randomS2K ) where import Data.OpenPGP.Util.Fingerprint diff --git a/Data/OpenPGP/Util/DecryptSecretKey.hs b/Data/OpenPGP/Util/DecryptSecretKey.hs index 1c519ae..1188f3e 100644 --- a/Data/OpenPGP/Util/DecryptSecretKey.hs +++ b/Data/OpenPGP/Util/DecryptSecretKey.hs @@ -4,6 +4,7 @@ module Data.OpenPGP.Util.DecryptSecretKey where import qualified Data.OpenPGP as OpenPGP +import Data.OpenPGP.Internal (decode_s2k_count) import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as LZ import Data.Word (Word16) @@ -19,6 +20,7 @@ import Data.Binary.Get (runGet) #endif import Control.Exception as Exception (IOException(..),catch) import Data.Binary.Put (runPut) +import qualified Data.Serialize as Cereal import Control.Applicative ( (<$>) ) import qualified Crypto.Cipher.AES as Vincent @@ -231,3 +233,15 @@ encryptSecretKey passphrase s2k salgo plain = do -- k = string2key s2k passphrase -- OpenPGP.string2key hashBySymbol s2k passphrase + +randomS2K :: OpenPGP.HashAlgorithm -> IO OpenPGP.S2K +randomS2K hash = do + g <- makeGen Nothing +#if defined(VERSION_cryptonite) + let (saltbs,g') = Vincent.randomBytesGenerate 9 g +#else + let (saltbs,g') = Vincent.cprgGenerate 9 g +#endif + let Right salt = Cereal.decode (BS.drop 1 saltbs) + return $ OpenPGP.IteratedSaltedS2K hash salt (decode_s2k_count $ BS.head saltbs) + -- cgit v1.2.3