From a15059b0452ba9d730404ce5c5a6b91a3828fdbc Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 27 Aug 2016 04:29:39 -0400 Subject: Replaced dataenc with memory. --- lib/KeyRing.hs | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'lib/KeyRing.hs') diff --git a/lib/KeyRing.hs b/lib/KeyRing.hs index bc881f2..ae2d14d 100644 --- a/lib/KeyRing.hs +++ b/lib/KeyRing.hs @@ -145,12 +145,15 @@ import Data.Time.Clock ( UTCTime ) import Data.Bits ( Bits, shiftR ) import Data.Text.Encoding ( encodeUtf8 ) import qualified Data.Map as Map -import qualified Data.ByteString.Lazy as L ( unpack, null, readFile, writeFile - , ByteString, toChunks, hGetContents, hPut, concat, fromChunks, splitAt - , index, break, pack, empty ) -import qualified Data.ByteString as S ( ByteString, unpack, splitAt, concat, cons, spanEnd, hGetContents, readFile, breakSubstring, drop, length, null, hPutStr, singleton, unfoldr, reverse ) +import qualified Data.ByteString.Lazy as L +import qualified Data.ByteString as S +#if defined(VERSION_memory) +import qualified Data.ByteString.Char8 as S8 +import Data.ByteArray.Encoding +#elif defined(VERSION_dataenc) import qualified Codec.Binary.Base32 as Base32 import qualified Codec.Binary.Base64 as Base64 +#endif #if !defined(VERSION_cryptonite) import qualified Crypto.Hash.SHA1 as SHA1 import qualified Crypto.Types.PubKey.ECC as ECC @@ -1795,14 +1798,19 @@ torhash :: Packet -> String torhash key = fromMaybe "" $ derToBase32 <$> derRSA key derToBase32 :: ByteString -> String -#if !defined(VERSION_cryptonite) -derToBase32 = map toLower . Base32.encode . S.unpack . SHA1.hashlazy -#else -derToBase32 = map toLower . Base32.encode . S.unpack . sha1 +derToBase32 = map toLower . base32 . sha1 where sha1 :: L.ByteString -> S.ByteString +#if !defined(VERSION_cryptonite) + sha1 = SHA1.hashlazy +#else sha1 x = convert (Vincent.hashlazy x :: Vincent.Digest Vincent.SHA1) #endif +#if defined(VERSION_memory) + base32 = S8.unpack . convertToBase Base32 +#elif defined(VERSION_dataenc) + base32 = Base32.encode . S.unpack +#endif derRSA :: Packet -> Maybe ByteString derRSA rsa = do @@ -1930,11 +1938,18 @@ extractRSAKeyFields kvs = do , rsaCoefficient = u } where parseField blob = MPI <$> m +#if defined(VERSION_memory) + where m = bigendian <$> functorToMaybe (convertFromBase Base64 $ Char8.toStrict blob) + bigendian bs = snd $ S.foldl' (\(c,a) w8 -> (c-1, a + 256^c * fromIntegral w8)) (nlen-1,0) bs + where + nlen = S.length bs +#elif defined(VERSION_dataenc) where m = bigendian <$> Base64.decode (Char8.unpack blob) - bigendian bs = snd $ foldl' (\(c,a) w8 -> (c-1, a + 256^c * fromIntegral w8)) (nlen-1,0) bs where nlen = length bs +#endif + rsaToPGP stamp rsa = SecretKeyPacket { version = 4 @@ -2422,7 +2437,11 @@ pemFromPacket Sec packet = rsa <- rsaPrivateKeyFromPacket packet -- RSAPrivateKey let asn1 = toASN1 rsa [] bs = encodeASN1 DER asn1 +#if defined(VERSION_memory) + dta = S8.unpack $ convertToBase Base64 (L.toStrict bs) +#elif defined(VERSION_dataenc) dta = Base64.encode (L.unpack bs) +#endif output = writePEM "RSA PRIVATE KEY" dta Just output algo -> Nothing @@ -2432,7 +2451,11 @@ pemFromPacket Pub packet = rsa <- rsaKeyFromPacket packet let asn1 = toASN1 (pkcs8 rsa) [] bs = encodeASN1 DER asn1 +#if defined(VERSION_memory) + dta = S8.unpack $ convertToBase Base64 (L.toStrict bs) +#elif defined(VERSION_dataenc) dta = Base64.encode (L.unpack bs) +#endif output = writePEM "PUBLIC KEY" dta Just output algo -> Nothing @@ -2464,7 +2487,11 @@ writeKeyToFile StreamInfo { typ = DNSPresentation } fname packet = do let -- asn1 = toASN1 rsa [] -- bs = encodeASN1 DER asn1 -- dta = Base64.encode (L.unpack bs) +#if defined(VERSION_memory) + b64 ac rsa = S8.unpack $ convertToBase Base64 $ i2bs_unsized i +#elif defined(VERSION_dataenc) b64 ac rsa = Base64.encode (S.unpack $ i2bs_unsized i) +#endif where MPI i = ac rsa i2bs_unsized :: Integer -> S.ByteString -- cgit v1.2.3