summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Paul Weber <singpolyma@singpolyma.net>2013-01-02 00:14:20 -0500
committerStephen Paul Weber <singpolyma@singpolyma.net>2013-01-02 00:14:20 -0500
commitd9651545a5e0f4b4f6498caa3f4a1c9e2a1f3be2 (patch)
tree6b6492f02a3767bcb1cdcad4ba1fb0fd48f544db
parente2f93583af0942b855b451c74ffe11dcf702ae7e (diff)
string2key utility function
Interpret the S2K systems, modulo the actual hashing.
-rw-r--r--Data/OpenPGP.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/Data/OpenPGP.hs b/Data/OpenPGP.hs
index e71e48a..9a50fa0 100644
--- a/Data/OpenPGP.hs
+++ b/Data/OpenPGP.hs
@@ -50,6 +50,7 @@ module Data.OpenPGP (
50 Message(..), 50 Message(..),
51 SignatureSubpacket(..), 51 SignatureSubpacket(..),
52 S2K(..), 52 S2K(..),
53 string2key,
53 HashAlgorithm(..), 54 HashAlgorithm(..),
54 KeyAlgorithm(..), 55 KeyAlgorithm(..),
55 SymmetricAlgorithm(..), 56 SymmetricAlgorithm(..),
@@ -71,6 +72,7 @@ import Data.Word
71import Data.Char 72import Data.Char
72import Data.List 73import Data.List
73import Data.OpenPGP.Internal 74import Data.OpenPGP.Internal
75import qualified Data.ByteString as BS
74import qualified Data.ByteString.Lazy as LZ 76import qualified Data.ByteString.Lazy as LZ
75 77
76#ifdef CEREAL 78#ifdef CEREAL
@@ -735,6 +737,26 @@ instance BINARY_CLASS S2K where
735 3 -> IteratedSaltedS2K <$> get <*> get <*> (decode_s2k_count <$> get) 737 3 -> IteratedSaltedS2K <$> get <*> get <*> (decode_s2k_count <$> get)
736 _ -> S2K t <$> getRemainingByteString 738 _ -> S2K t <$> getRemainingByteString
737 739
740-- | Take a hash function and an 'S2K' value and generate the bytes
741-- needed for creating a symmetric key.
742--
743-- Return value is always infinite length.
744-- Take the first n bytes you need for your keysize.
745string2key :: (HashAlgorithm -> LZ.ByteString -> BS.ByteString) -> S2K -> LZ.ByteString -> LZ.ByteString
746string2key hsh (SimpleS2K halgo) s = infiniHashes (hsh halgo) s
747string2key hsh (SaltedS2K halgo salt) s =
748 infiniHashes (hsh halgo) (encode salt `LZ.append` s)
749string2key hsh (IteratedSaltedS2K halgo salt count) s =
750 infiniHashes (hsh halgo) $
751 LZ.take (max (fromIntegral count) (LZ.length s))
752 (LZ.cycle $ encode salt `LZ.append` s)
753string2key _ s2k _ = error $ "Unsupported S2K specifier: " ++ show s2k
754
755infiniHashes :: (LZ.ByteString -> BS.ByteString) -> LZ.ByteString -> LZ.ByteString
756infiniHashes hsh s = LZ.fromChunks (hs 0)
757 where
758 hs c = hsh (LZ.replicate c 0 `LZ.append` s) : hs (c+1)
759
738data HashAlgorithm = MD5 | SHA1 | RIPEMD160 | SHA256 | SHA384 | SHA512 | SHA224 | HashAlgorithm Word8 760data HashAlgorithm = MD5 | SHA1 | RIPEMD160 | SHA256 | SHA384 | SHA512 | SHA224 | HashAlgorithm Word8
739 deriving (Show, Read, Eq) 761 deriving (Show, Read, Eq)
740 762