diff options
author | joe <joe@jerkface.net> | 2016-08-27 04:29:39 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2016-08-27 04:29:39 -0400 |
commit | a15059b0452ba9d730404ce5c5a6b91a3828fdbc (patch) | |
tree | 0f9617558352dcb3e2ea6219cfa1d7f739b8d761 /lib/KeyRing.hs | |
parent | 067c3647ed02c24c08b17803e28679e69d2e6dd9 (diff) |
Replaced dataenc with memory.
Diffstat (limited to 'lib/KeyRing.hs')
-rw-r--r-- | lib/KeyRing.hs | 45 |
1 files changed, 36 insertions, 9 deletions
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 ) | |||
145 | import Data.Bits ( Bits, shiftR ) | 145 | import Data.Bits ( Bits, shiftR ) |
146 | import Data.Text.Encoding ( encodeUtf8 ) | 146 | import Data.Text.Encoding ( encodeUtf8 ) |
147 | import qualified Data.Map as Map | 147 | import qualified Data.Map as Map |
148 | import qualified Data.ByteString.Lazy as L ( unpack, null, readFile, writeFile | 148 | import qualified Data.ByteString.Lazy as L |
149 | , ByteString, toChunks, hGetContents, hPut, concat, fromChunks, splitAt | 149 | import qualified Data.ByteString as S |
150 | , index, break, pack, empty ) | 150 | #if defined(VERSION_memory) |
151 | import qualified Data.ByteString as S ( ByteString, unpack, splitAt, concat, cons, spanEnd, hGetContents, readFile, breakSubstring, drop, length, null, hPutStr, singleton, unfoldr, reverse ) | 151 | import qualified Data.ByteString.Char8 as S8 |
152 | import Data.ByteArray.Encoding | ||
153 | #elif defined(VERSION_dataenc) | ||
152 | import qualified Codec.Binary.Base32 as Base32 | 154 | import qualified Codec.Binary.Base32 as Base32 |
153 | import qualified Codec.Binary.Base64 as Base64 | 155 | import qualified Codec.Binary.Base64 as Base64 |
156 | #endif | ||
154 | #if !defined(VERSION_cryptonite) | 157 | #if !defined(VERSION_cryptonite) |
155 | import qualified Crypto.Hash.SHA1 as SHA1 | 158 | import qualified Crypto.Hash.SHA1 as SHA1 |
156 | import qualified Crypto.Types.PubKey.ECC as ECC | 159 | import qualified Crypto.Types.PubKey.ECC as ECC |
@@ -1795,14 +1798,19 @@ torhash :: Packet -> String | |||
1795 | torhash key = fromMaybe "" $ derToBase32 <$> derRSA key | 1798 | torhash key = fromMaybe "" $ derToBase32 <$> derRSA key |
1796 | 1799 | ||
1797 | derToBase32 :: ByteString -> String | 1800 | derToBase32 :: ByteString -> String |
1798 | #if !defined(VERSION_cryptonite) | 1801 | derToBase32 = map toLower . base32 . sha1 |
1799 | derToBase32 = map toLower . Base32.encode . S.unpack . SHA1.hashlazy | ||
1800 | #else | ||
1801 | derToBase32 = map toLower . Base32.encode . S.unpack . sha1 | ||
1802 | where | 1802 | where |
1803 | sha1 :: L.ByteString -> S.ByteString | 1803 | sha1 :: L.ByteString -> S.ByteString |
1804 | #if !defined(VERSION_cryptonite) | ||
1805 | sha1 = SHA1.hashlazy | ||
1806 | #else | ||
1804 | sha1 x = convert (Vincent.hashlazy x :: Vincent.Digest Vincent.SHA1) | 1807 | sha1 x = convert (Vincent.hashlazy x :: Vincent.Digest Vincent.SHA1) |
1805 | #endif | 1808 | #endif |
1809 | #if defined(VERSION_memory) | ||
1810 | base32 = S8.unpack . convertToBase Base32 | ||
1811 | #elif defined(VERSION_dataenc) | ||
1812 | base32 = Base32.encode . S.unpack | ||
1813 | #endif | ||
1806 | 1814 | ||
1807 | derRSA :: Packet -> Maybe ByteString | 1815 | derRSA :: Packet -> Maybe ByteString |
1808 | derRSA rsa = do | 1816 | derRSA rsa = do |
@@ -1930,11 +1938,18 @@ extractRSAKeyFields kvs = do | |||
1930 | , rsaCoefficient = u } | 1938 | , rsaCoefficient = u } |
1931 | where | 1939 | where |
1932 | parseField blob = MPI <$> m | 1940 | parseField blob = MPI <$> m |
1941 | #if defined(VERSION_memory) | ||
1942 | where m = bigendian <$> functorToMaybe (convertFromBase Base64 $ Char8.toStrict blob) | ||
1943 | bigendian bs = snd $ S.foldl' (\(c,a) w8 -> (c-1, a + 256^c * fromIntegral w8)) (nlen-1,0) bs | ||
1944 | where | ||
1945 | nlen = S.length bs | ||
1946 | #elif defined(VERSION_dataenc) | ||
1933 | where m = bigendian <$> Base64.decode (Char8.unpack blob) | 1947 | where m = bigendian <$> Base64.decode (Char8.unpack blob) |
1934 | |||
1935 | bigendian bs = snd $ foldl' (\(c,a) w8 -> (c-1, a + 256^c * fromIntegral w8)) (nlen-1,0) bs | 1948 | bigendian bs = snd $ foldl' (\(c,a) w8 -> (c-1, a + 256^c * fromIntegral w8)) (nlen-1,0) bs |
1936 | where | 1949 | where |
1937 | nlen = length bs | 1950 | nlen = length bs |
1951 | #endif | ||
1952 | |||
1938 | 1953 | ||
1939 | rsaToPGP stamp rsa = SecretKeyPacket | 1954 | rsaToPGP stamp rsa = SecretKeyPacket |
1940 | { version = 4 | 1955 | { version = 4 |
@@ -2422,7 +2437,11 @@ pemFromPacket Sec packet = | |||
2422 | rsa <- rsaPrivateKeyFromPacket packet -- RSAPrivateKey | 2437 | rsa <- rsaPrivateKeyFromPacket packet -- RSAPrivateKey |
2423 | let asn1 = toASN1 rsa [] | 2438 | let asn1 = toASN1 rsa [] |
2424 | bs = encodeASN1 DER asn1 | 2439 | bs = encodeASN1 DER asn1 |
2440 | #if defined(VERSION_memory) | ||
2441 | dta = S8.unpack $ convertToBase Base64 (L.toStrict bs) | ||
2442 | #elif defined(VERSION_dataenc) | ||
2425 | dta = Base64.encode (L.unpack bs) | 2443 | dta = Base64.encode (L.unpack bs) |
2444 | #endif | ||
2426 | output = writePEM "RSA PRIVATE KEY" dta | 2445 | output = writePEM "RSA PRIVATE KEY" dta |
2427 | Just output | 2446 | Just output |
2428 | algo -> Nothing | 2447 | algo -> Nothing |
@@ -2432,7 +2451,11 @@ pemFromPacket Pub packet = | |||
2432 | rsa <- rsaKeyFromPacket packet | 2451 | rsa <- rsaKeyFromPacket packet |
2433 | let asn1 = toASN1 (pkcs8 rsa) [] | 2452 | let asn1 = toASN1 (pkcs8 rsa) [] |
2434 | bs = encodeASN1 DER asn1 | 2453 | bs = encodeASN1 DER asn1 |
2454 | #if defined(VERSION_memory) | ||
2455 | dta = S8.unpack $ convertToBase Base64 (L.toStrict bs) | ||
2456 | #elif defined(VERSION_dataenc) | ||
2435 | dta = Base64.encode (L.unpack bs) | 2457 | dta = Base64.encode (L.unpack bs) |
2458 | #endif | ||
2436 | output = writePEM "PUBLIC KEY" dta | 2459 | output = writePEM "PUBLIC KEY" dta |
2437 | Just output | 2460 | Just output |
2438 | algo -> Nothing | 2461 | algo -> Nothing |
@@ -2464,7 +2487,11 @@ writeKeyToFile StreamInfo { typ = DNSPresentation } fname packet = do | |||
2464 | let -- asn1 = toASN1 rsa [] | 2487 | let -- asn1 = toASN1 rsa [] |
2465 | -- bs = encodeASN1 DER asn1 | 2488 | -- bs = encodeASN1 DER asn1 |
2466 | -- dta = Base64.encode (L.unpack bs) | 2489 | -- dta = Base64.encode (L.unpack bs) |
2490 | #if defined(VERSION_memory) | ||
2491 | b64 ac rsa = S8.unpack $ convertToBase Base64 $ i2bs_unsized i | ||
2492 | #elif defined(VERSION_dataenc) | ||
2467 | b64 ac rsa = Base64.encode (S.unpack $ i2bs_unsized i) | 2493 | b64 ac rsa = Base64.encode (S.unpack $ i2bs_unsized i) |
2494 | #endif | ||
2468 | where | 2495 | where |
2469 | MPI i = ac rsa | 2496 | MPI i = ac rsa |
2470 | i2bs_unsized :: Integer -> S.ByteString | 2497 | i2bs_unsized :: Integer -> S.ByteString |